I'm having a little trouble with lists.
I'm doing a random walk with self-avoing walk, i.e. you cannot walk over the same place two times.
I am recording the coodinates in two lists, x and y so I have something like this: (0,0),(1,0),(2,0),(2,1),(2,0).... However this is just a normal random walk, because I can have repeated coordiates. My idea is to put a condition in the loop, but here is where I have problems.
How can I tell my code to avoid repeated coordinates? For example, I have
coords=zip(x,y)
for i in coords:
if i "is repeated":
"dont go to this coord"
else:
"go there".
This is just an example, I have the code written for the "dont go to this coord" and "go there", I just need help to write the condition "if i is repeted".
Thanks
This is the concept:
coords=zip(x,y)
used_coords = []
for i in coords:
if i in used_coords:
"dont go to this coord"
else:
"go there".
used_coords.append(i)
Related
I'm trying to write a simple text adventure game in C++. I want to allow the user to be able to type in phrases such as "GET THE DOG" where the code would ignore 'THE' and just give me the important things like 'GET' and 'DOG'. I also want the game to support movement, so another example of a phrase could be something like "MOVE TO THE LEFT" where the game would ignore 'TO' and 'THE' and only pay attention to 'MOVE' 'LEFT'.
Anyone have any tips on how to write a function to do this? I thought at first I could use getline, but the only way I think I can get that to work, is if I already know the position of the important words. My friend suggested using substr to put the strings into a vector, then iterating over that. But even that way I'm not too sure how I'd use substr to do such a thing.
Thanks!
char str[100];
cin.getline(str,100);
char* point;
pint = strtok(str, " ");
while(piont != NULL){
cout<<point<<endl;
point = strtok(NULL, " ");
}
}
here is something I've divvied up while trying to figure out how to do this. I'm not really sure why it works, but its doing something right. Its pointing to full on words, because whenever i print the pointer, its printing the word before the whitespace.
The usual approach would be to split the input up into words (probably in a std::vector<std::string>), and filter (std::remove_if) the words using a set (probably a std:: unordered_set<std::string>) of "stop words". Then you can try to make sense of what's left.
Technically, a stop word is a word so common that it is pointless to use it in a search. I don't know why they are called "stop words", but it is definitely the usual term and you can use it to find some common lists. Not all of them are "noise", in your sense, but I think all your noise words will be on common stop word lists.
I'd like to make a list with some phrases, and then using the randint to print one of them randomly with powershell.
I can do it, but it'll look really bad.
My problem is, it is really big lines so it won't look "decent" at it, I want to break the lines somehow so I wouldn't have to scroll sideways to read everything.
scene = ["lots of stuff", "lots of stuff 2", "lots of stuff 3"]
So let's say I wanna break each of those elements after 'of', but still want to keep scene[0] = "lots of stuff" only with the line break when running the program at powershell. Is it possible?
I am running into problems accomplishing another iteration after using continue. The goal is to get an integer input that is greater than or equal to 3. I dont want the script to error out on the user, instead I would like to ask for another input.
while True:
try:
sides = int(raw_input("Marty wants to draw you a shape. How many sides will the shape have?"))
except ValueError:
print "Marty needs an integer, try again."
continue
if sides < 2:
print "Marty needs a number greater than 2, try again."
continue
else:
break
Does the issue come when using continue twice? Any advice in the proper use of continue would be great. As it stands now, it asks the user for an input. If given anything other than an integer, it asks for another input. If given 2 it does nothing, not even print, let alone try again for an input.
The problem isn't with your use of continue, it's with your evaluation of the input. Rather than what you have, try:
if sides <= 2:
print 'Marty needs a number greater than 2, try again.'
continue
or:
if sides < 3:
if(isEventMoveRight() == true)
{
if((playerX != 70) && (playerX+11 != 'T'))
{
playerX = playerX+1;
}
}
'T' being the different block of characters from the player. Granted I'm not using an array for the block of 'T's or the background I'm only using one for the player, I think. I'm not sure if it's even using the array I set for the player correctly. In fact... I've got no idea how to use an array for a gameMap full stop.
I'll give you a run down of what I've got to do. Inside a console I have to use chars to create a game. I plan on having something pokemon-like with a guy being able to run round a screen and move the window when he reaches the edge of it like Zelda. Obviously I'm not making the entire game I'm just make one tiny part of it but that is all proving too much for me. I'm using Visual Studio 2010 but I have to use some guy's code that he's done to help us but to be honest it seems more of a hindrance than help because all the tutorials online about this are for just using C++ in VS2010 from scratch not from his code.
I first thought I could create an array called gameMap and populate it full of background spaces using two for loops. Then on top of that have an array for player, an array for obstacles and arrays for enemies using the same tactic. But for some bizarre reason I have to set the type of char the two for loops will print to the array[i][j] inside the two for loops even though beforehand in the code I have explicitly set what's in the array.
Example,
char array[2][2] = {
{ '*', '*',},
{ '*', '*',},
};
But as I said when I'm trying to just print out the array it's like it completely ignores what I've set to be in the array already and just prints out a bunch of 'asterisks'. It's fine for the background but when I want to print the player instead of printing a bunch of asterisks in a square it prints them in a long line and then the only way to make it so it appears right is to when printing out the player array, I have another for loop below which prints out the rest of the console window's worth of X, so in this case, 70 characters of blank spaces but that is of course completely wrong and not even a proper solution.
Any help would be highly appreciated.
While I'm not completely clear on your question, I have some thoughts...
if(isEventMoveRight() == true)
{
if((playerX != 70) && (playerX+11 != 'T'))
{
playerX = playerX+1;
}
}
Do you mean playerX+1? Either way though, it appears you are not comparing to an array, but the value inside the array that represents a space you can't go. It seems like it should be something like this:
if(isEventMoveRight() == true)
{
if((playerX < maximumX) && worldGrid(playerX+1, playerY).isWalkable)
{
playerX = playerX+1;
}
}
where worldGrid() is a function that returns a reference to a struct that tells us the attributes of the given grid space.
As for your printing/debugging issue, note that many char values do not print letters or numbers. For example you might print a backspace character or tab character which changes the cursor position and may or may not overwrite stuff you wanted to see. So simply printing chars might not be the best way to debug char values, you might want to print the char value as int. Unless you are specifically using alphabet characters or other printable characters only.
You can convert char to an unsigned short like this:
(unsigned short)(0x00FF & charVariable);
Other thoughts based on what I understand from the question...
It sounds like you want one 2D array to essentially be the map where certain characters in the array represent different game assets such as a brick (can't walk through it) and a floor (can walk through it). These are probably stored as char so you get a maximum of 256 different types of panels/walls/stairs/etc. These are the things that do not move in the scene, only determine what the floor looks like underneath all the assets/items/characters.
(You might use a std::map along with a struct or a single 16bit int or some other int type that could be used as flags. Basically you want to be able to quickly look-up the attributes of each block of space in the world, such as whether or not you can walk through it or is it slippery, is it water, etc.)
Then you would probably want a second array to represent item placement and type. These might be treasure chests, destructible doors/caves, or movable bricks. These are items that the player can interact with, pick up, and/or use in puzzle solving.
I don't think you'd want the characters (both hero and villain) to be in a 2D array. In particular you don't want to traverse a huge array just to figure out where the character is. You probably also don't want any characters locked to grid spaces, unless you are making a text adventure, you would likely want them to be able to be in-between blocks on the map (like Zelda). With the hero you probably just have variables that say what the position and orientation are and other character attributes such as gold and health. No need for a 2D array.
With enemies, it would probably be good to use a std::vector or similar container because the number of them will change if one dies or spawns. And because you can iterate through a vector similar to how you might an array you can update all your enemies each time-step relatively easily and set a flag on them if they get killed so you can remove them from the vector after the update loop.
If you want to be able to have more than 1 hero, such as in local multi-player, you could always use a std::vector for heroes as well.
Suppose, we have input strings in a form I have/had alot/none/zero money.
I would like to have a set of output strings as follows (example 1):
I have alot money
I have none money
I have zero money
I had alot money
I had none money
I had zero money
But then, real task here, is to be able to choose one or more, or none input substrings to ignore. So, the output strings would look like this:
I money
or
first example
or
I alot money
I none money
I zero money
or
I
or
money
I hope you got the point.
How can i do this in the way, friendliest to cpu cycles ?
Ok, to break the ice, this is what im Not willing to do, but considering until brighter ideas:
generate all the output strings (mentioned, example 1).
iterating through strings, i filter out ones that meet my criteria, replace unwanted substrings with "".
put the resulting string into final output array only if it is not already there.
Also, the answer to why do i care for cpu cycles, is simple : the longer this task takes, the longer it will block worker thread.
This simplest way is to find all the spaces and / character and put individual word into a two-level list, you then get a structure like this:
I
have, had
a lot, none, zero
money
.
Now you just loop thru the tree and generate a result string.
To push it to an overkill, you may also write a token parser instead of doing strchr.
How can i do this in the way, friendliest to cpu cycles ?
Why do you care?