### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203`` `````` 1 #include 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.