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.

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <ctime>
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 "<<reptiles[i].strength;
        reptiles[i].description=reptileDescriber.str();
    }
    return;
}
void assignWays()
{
    for(int i=0; i<1000; i++)
    {
        ways[i].location1=i;
        if(rand()%100<40)
        {
            ways[i].location2=ways[i].location1+1;
            ways[i].description1="door to the east";
            ways[i].description2="door to the west";
        }
        else if(rand()%100<80)
        {
            ways[i].location2=ways[i].location1+10;
            ways[i].description1="door to the north";
            ways[i].description2="door to the south";
        }
        else
        {
            ways[i].location2=ways[i].location1+100;
            ways[i].description1="ascending spiral staircase";
            ways[i].description2="descending spiral staircase";
        }
    }
    return;
}
void assignImplements()
{
    for(int i=0; i<1000; i++)
    {
        implements[i].location=rand()%1000;
        switch(rand()%5+1)
        {
            case 1:
                implements[i].description="broom";
                implements[i].strength=10;
            case 2:
                implements[i].description="polished stone ithyphallus";
                implements[i].strength=30;
            case 3:
                implements[i].description="blood-stained ceremonial dagger";
                implements[i].strength=40;
            case 4:
                implements[i].description="anthropodermic grimoire";
                implements[i].strength=5;
            case 5:
                implements[i].description="bleached infant skull";
                implements[i].strength=20;
        }
    }
    return;
}
void describeRoom()
{
    cout<<"You stand in a 10\'x10\'x10\' room. It contains:\n";
    for(int i=0; i<666; i++)
    {
        if(reptiles[i].location==player.location)
        {
            cout<<reptiles[i].description<<'\n';
        }
    }
    for(int i=0; i<1000; i++)
    {
        if(ways[i].location1==player.location)
        {
            cout<<ways[i].description1<<'\n';
        }
        if(ways[i].location2==player.location)
        {
            cout<<ways[i].description2<<'\n';
        }
        if(implements[i].location==player.location)
        {
            cout<<implements[i].description<<'\n';
        }
    }
    cout<<"(Enter help to view helpscreen.)";
    return;
}
void help()
{
    cout<<"To view the status of your character, enter the word character.\n";
    cout<<"To view a list of your equipment, enter the word equipment.\n";
    cout<<"To attack, enter the word attack.\n";
    cout<<"(If there is more than one potential target present,\n";
    cout<<"you will be prompted with a choice.)\n";
    cout<<"To go through a door or use stairs, enter north, south, east, west, up, or down.\n";
    return;
}
void go(string direction)
{
    cout<<"You go ";
    cout<<direction;
    cout<<'\n';
    if(direction=="north")player.location++;
    if(direction=="south")player.location--;
    if(direction=="east")player.location+10;
    if(direction=="west")player.location-10;
    if(direction=="up")player.location+100;
    if(direction=="down")player.location-100;
    return;
}
void interpret()
{
    string input;
    cin>>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?
if you add the code:
1
2
3
4
5
    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.