### Rand() issue, game concept

Thanks for all the helpful answers you guys provided me with before I even registered. I have been lurking extensively, but am still quite the noob to programming.

Below are the beginnings of a game that I have been working on. I have already created it successfully in JavaScript, so I know the concept is sound. The concept is straightforward: I assign locations between 0 and 999 to most of my objects (the "ways" have two locations, some of them over 999). Objects exist in 3-d space: location 100's are the z axis, 10's are the y axis, and 1's are the x axis. So the floor of location 555 is 50' underground. All of the space is divided into 10'x10'x10' rooms.

The "way" objects represent stairways and doorways. When you climb a staircase, player.location+100, when you go east player.location++, etc. The goal is to get to the surface (player.location >= 1000) without being killed by any denizens. As aforementioned, way objects have two location member variables. Location1's are consecutive numbers 0 through 999, so way[0].location1==0, way[222].location1==222. Way objects are assigned a location2 which is offset from their location1 by a difference of either 1, 10, or 100.

Whether the way points north, south, east, west, up, or down depends on random numbers (so do a lot of other things). So the labyrinth is different every time you play.

I am nowhere near achieving this with C++ yet. Running the far from complete code below, the rooms' contents start to repeat after only a few location changes.

I have researched the rand() function pretty much all day today, and have encountered a consensus. Srand() should be invoked at the beginning of the main function one time only, passing time(NULL) or time(0).

I have tried many different things to solve the problem: using .h instead of c in my #includes, declaring my functions before the main and defining them afterwards so that srand() is invoked before any instance of rand() during compilation, using srand() with a different int before every instance of rand(), using different int's for each one of my for loops, assigning my rand()'s to variables before %'ing them, using different kinds of loops, using switches, using nested conditionals. I have gone through the whole code with cout<< to see where the problem lies. The only knowledge that has gleaned is that my location2's seem to be above 700 instead of randomly distributed between 1 (east of location 0) and 1099 (the top of the highest numbered stairway). Still, I cannot figure out why.

I am using the Code Blocks 12.11 compiler.

?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182`` ``````#include #include #include #include #include using namespace std; ostringstream numberToString; string vocab[100]={"help", "north", "south", "east", "west", "up", "down"}; class creature { public: string description; int location; int strength; }reptiles[666], player; class way { public: int location1; int location2; string description1; string description2; }ways[1000]; class implement { public: string description; int location; int strength; }implements[1000]; void assignReptiles() { for(int i=0; i<666; i++) { reptiles[i].location=rand()%1000; reptiles[i].strength=rand()%100+1; stringstream reptileDescriber; reptileDescriber<<"reptile with a strength of "<>input; for(int i=0; i<100; i++) { if(input.find(vocab[i])!=string::npos) { if(vocab[i]=="help") { help(); break; } if(vocab[i]=="north"||"south"||"east"||"west"||"up"||"down") { go(vocab[i]); break; } } } return; } int main() { srand(time(0)); player.location=0; player.strength=100; assignReptiles(); assignWays(); assignImplements(); while(player.strength>0&&player.location<1000) { describeRoom(); interpret(); } return 0; }``````
Line 140-143 doesn't do anything. If you want to add 10 to the location you should do `player.location = player.location + 10;` or `player.location += 10;`.

The if condition on line 159 will always be true. You have to repeat `vocab[i] ==` for all the things you want to compare it with.
`if(vocab[i] == "north" || vocab[i] == "south" || vocab[i] == "east" || vocab[i] == "west" || vocab[i] == "up" || vocab[i] == "down")`
Actually i could not understand your question?
 ``12345`` `````` assignWays(); for(int i=0; i<1000; i++){ cout << i << ": " << ways[i].location2 << endl; } assignImplements();``````

in main() function you can see location2's are i+1 or i+10 or i+100

can you explain what your question is more basicly?
@ Peter87

Problem solved! Thanks!
Topic archived. No new replies allowed.