### Trying to create the game sorry.

Can anyone help? I am trying to create a game that has the same basic rules as the board game SORRY. I keep getting stuck in an infinite loop as well as a few other problems. Any suggestions?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157`` `````` #include #include #include using namespace std; int randomNumber() { srand (time(NULL)); // only once. return rand() % 6 + 2; // two die roll sixes } bool checkWinner(int i) { return i == 50; } int main() { int numberPlayers = 4; int spaces[50]; int k = 0; int j = 0; cout <<"Enter number of players (2-4) "; cin >> numberPlayers; while (numberPlayers < 2 || numberPlayers > 4) { cout <<"Please enter a valid number " << endl; cin >> numberPlayers; } int playerPosition[numberPlayers]; for(int i = 0; i < numberPlayers; i++) playerPosition[i] =0; bool win = false; int winner; while(!win) { for (int j = 0; j < numberPlayers; j++) cout << "Player: " << j << " roll the dice" << endl; } int numbers = randomNumber(); switch (numbers) { case 2: if (playerPosition[j] + 2 <= 50) playerPosition[j] += 2; if (checkWinner(playerPosition[j])) win = true; winner = j; break; case 3: if(playerPosition[j] + 3 <= 50) playerPosition[j] += 3; if(checkWinner(playerPosition[j])) win = true; winner = j; break; case 4: if(playerPosition[j] - 1 >=0) playerPosition[j] -= 1; if(checkWinner(playerPosition[j])) win = true; winner = j; break; case 5: if(playerPosition[j] + 5 <= 50) playerPosition[j] += 5; if(checkWinner(playerPosition[j])) win = true; winner = j; break; case 6: if(playerPosition[j] + 6 <= 50) playerPosition[j] +=6; if(checkWinner(playerPosition[j])) win = true; winner = j; break; case 7: while (k < numberPlayers) if(k != j && playerPosition[k] > playerPosition[j]) playerPosition[j] = playerPosition[k]; k++; if(checkWinner(playerPosition[j])) win = true; winner = j; break; case 8: if(playerPosition[j] + 8 <= 50) playerPosition[j] += 8; if(checkWinner(playerPosition[j])) win = true; winner = j; break; case 9: if(playerPosition[j] + 9 <= 50) playerPosition[j] += 9; if(checkWinner(playerPosition[j])) win = true; winner = j; break; case 10: if (playerPosition[j] + 10 <=50) playerPosition[j] += 10; if(checkWinner(playerPosition[j])) win = true; winner = j; break; case 11: if(k != j && playerPosition[k] < playerPosition[j]) playerPosition[j] = playerPosition[k]; k++; if(checkWinner(playerPosition[j])) win = true; winner = j; break; case 12: playerPosition[j] = 0; if(checkWinner(playerPosition[j])) win = true; winner = j; break; } cout << "The winner is: " << winner << endl;Put the code you need help with here.``````
A quick glance shows that line 43-47 will loop. Notice where the braces are. Also may help to indent your code properly to see where the loops actually are.
Hello mjones382,

Line 9 `srand (time(NULL)); // only once. `. No this line will be executed each time the function is called. Line 8 is better placed at line 24 so that it is only done once when the program starts.

I think you want to put lines 49 - 155 inside the while loop.

Hope that helps,

Andy
This was very helpful! Thank you guys very much.
Topic archived. No new replies allowed.