game sorry

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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <time.h>
using namespace std;

int randomNumber()
{
    srand (time(NULL));  
    return rand() % 6 + 2; 
}

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;
}


please help me, always loop.
Last edited on
1
2
3
4
5
6
7
 bool win = false;
    int winner;

    while (!win) {
        for (int j = 0; j < numberPlayers; j++)
            cout << "Player: " << j << " roll the dice" << endl;
    }


This while loop. It will loop if win is false.

What is win? false.
Does win ever change? No.
Last edited on
Topic archived. No new replies allowed.