Infinite Loop...sometimes?

It's a simple predator prey simulation, and checks to see if the positions of the predator and prey are the same. It catches it some times, and others infinitely loops. I'm not sure why it would catch some times and not others.

Any help is appreciated.

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  1 #include <iostream>
  2 using namespace std;
  3
  4 void printGrid(char grid[][5], int w, int h);
  5
  6 int main()
  7 {
  8    // Create a 5 by 5 grid array of chars
  9    char grid[5][5];
 10    srand(time(0));
 11    // Now loop through each row
 12    for (int i = 0; i < 5; i++)
 13    {
 14       //then each column and set each space to '+'
 15       for (int j = 0; j < 5; j++)
 16       {
 17          grid[i][j] = '+';
 18       }
 19    }
 20    int a, b, c , d;
 21    int count = 0;
 22    //variables a & b are the predator.
 23    //Starting location of predator.
 24    a = (rand()%5);
 25    b = (rand()%5);
 26
 27    //variables c & d are the prey.
 28    //Starting location of prey.
 29
 30    c = (rand()%5);
 31    if(c = a)
 32    {
 33       c = (rand()%5);
 34    }
 35    d = (rand()%5);
 36    if(d = b)
 37    {
 38       d = (rand()%5);
 39    }
 40
 41    //place the starting positions in the grid.
 42    //predator is X.
 43    grid[a][b] = 'X';
 44    //prey is 0.
 45    grid[c][d] = 'O';
 46    printGrid(grid,5,5);
 47    while(1)
 48    {
 49       if(a == c)
 50       {
 51          if(b == d)
 52          {
 53             cout << "That is the final move." << endl;
 54             cout << "It took " << count << " turns for the prey to be caught" << endl;
 55             return 0;
 56          }
 57       }
 58
 59       /*Next is movement, replacing the
 60         previous position with a + and
 61         moving the X and O to the new
 62         location*/
 63       int predMove, preyMove;
 64       predMove = (rand()%4);
 65
 66       preyMove = (rand()%4);
 67
 68       //predator movement based on rand.
 69       grid[a][b] = '+';
 70       //up
 71       if(predMove == 0)
 72       {
 73          if(a+1 <= 4)
 74          {
 75             a++;
 76          }
 77          else
 78          {
 79             a--;
 80          }
 81       }
 82       //down
 83       if(predMove == 1)
 84       {
 85          if(a-1 >= 0)
 86          {
 87             a--;
 88          }
 89          else
 90          {
 91             a++;
 92          }
 93       }
 94       //right
 95       if(predMove == 2)
 96       {
 97          if(b+1 <= 4)
 98          {
 99             b++;
100          }
101          else
102          {
103             b--;
104          }
105       }
106       //left
107       if(predMove == 3)
108       {
109          if(b-1 >= 0)
110          {
111             b--;
112          }
113          else
114          {
115             b++;
116          }
117       }
118       grid[a][b] = 'X';
119
120       //prey movement based on rand.
121       grid[c][d] = '+';
122       //up
123       if(preyMove == 0)
124       {
125          if(c+1 <= 4)
126          {
127             c++;
128          }
129          else
130          {
131             c--;
132          }
133       }
134       //down
135       if(preyMove == 1)
136       {
137          if(c-1 >= 0)
138          {
139             c--;
140          }
141          else
142          {
143             c++;
144          }
145       }
146       //right
147       if(preyMove == 2)
148       {
149          if(d+1 <= 4)
150          {
151             d++;
152          }
153          else
154          {
155             d--;
156          }
157       }
158       //left
159       if(preyMove == 3)
160       {
161          if(d-1 >= 0)
162          {
163             d--;
164          }
165          else
166          {
167             d++;
168          }
169       }
170       grid[c][d] = 'O';
171       count++;
172
173       // reprint the grid of 5 by 5 to show movements
174       printGrid(grid,5,5);
175       if(a == c)
176       {
177          if(b == d)
178          {
179             cout << "That is the final move." << endl;
180             cout << "It took " << count << " turns for the prey to be caught" << endl;
181             return 0;
182          }
183       }
184    }
185
186    return 0;
187 }
188
189 void printGrid(char grid[][5], int w, int h)
190 {
191    for (int i = 0; i < w; i++)
192    {
193       for (int j = 0; j < h; j++)
194       {
195          cout << grid[i][j] << " ";
196       }
197       cout << endl;
198    }
199    cout << "-----------------------------" << endl;
200 }
201
202
                   
On line 31 and 36 you are using = instead of ==.
cant be bothered to run your code cos those numbers up the sides are hard to move
Topic archived. No new replies allowed.