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?

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
  #include <iostream>
#include <cmath>
#include <cstdlib>

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,

A couple of comments:

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.