assignment help: code gets thrown into infinite loop.

Im creating a dice game with 2 modes (play with 1 or 2 dice) with 2 human players. When i select 1 dice the program runs as it should, but when i select 2 dice it gets thrown into an infinite loop. Can someone point me in the right direction on where to start?

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int winningScore = 100;
int turn = PLAYER1;


void printIntro()
{
    cout << " Welcome to the dice game: Pig! "<< endl;
    cout << "The goal is to be the first player to reach 100. If playing with one die the rules are that each player can rol as many times as they choose. just dont roll a 1 or else you'll lose your turn AND all points accumulated in that round. If you're  playing with 2 dies the same rules applies, but if you roll snake eyes (double 1's) you'll not only lose your turn but you'll also loose all your points. good luck and may the best player win!"<< endl;
}


int game1(string playerName, int playerScore)
{
    int roll = rand() % 6 + 1;
    cout << playerName << " You rolled: " << roll <<endl;
    
    if(roll == 1)
    {
        cout << " OH NO! You rolled a 1. "<< endl;
        cout << " Your turn is over. " << endl;
        playerScore = 0;
    }
    
    else
    {
        playerScore +=roll;
        cout << playerName << " Your score: " << playerScore <<endl;
    }
    
    if(roll == 1)
    {
        if(turn == PLAYER1)
            turn = PLAYER2;
        
        else
            turn = PLAYER1;
    }
    else
    {
        char choice;
        
        cout << " Would you like to roll again? (y/n): ";
        cin >> choice;
        
        if(choice != 'y')
        {
            if (turn == PLAYER1)
                turn = PLAYER2;
            else
                turn = PLAYER1;
        }
    }
    
    return playerScore;
    
}

int game2(string playerName, int playerScore)
{
    int roll1 = rand() % 6 + 1;
    int roll2 = rand() % 6 + 1;
    
    cout << playerName << " You rolled: " << roll1 << " and " << roll2 <<endl;
    
    if(roll1 || roll2 == 1)
    {
        cout << " OH NO! You rolled a 1. " << endl;
        cout << " Your turn is over. " << endl;
        playerScore = 0;
    }
    
    else if (roll1 && roll2 == 1)
    {
        cout << "OH CRAP! You rolled snake eyes!" << endl;
        cout << " Your turn is over. " << endl;
        playerScore == 0;
    }
    
    else
    {
        playerScore += roll1 + roll2 ;
        cout << playerName << " Your score: " << playerScore <<endl;
    }
    
    if(roll1 || roll2 == 1)
    {
        if(turn == PLAYER1)
            turn = PLAYER2;
        
        else
            turn = PLAYER1;
    }
    
    else if (roll1 && roll2 == 1)
    {
        if(turn == PLAYER1)
            turn = PLAYER2;
        
        else
            turn = PLAYER1;
    }
    
    else
    {
        char choice;
        
        cout << "Would you like to roll again? (y/n): ";
        cin >> choice;
        
        if(choice != 'y')
        {
            if (turn == PLAYER1)
                turn = PLAYER2;
            else
                turn = PLAYER1;
        }
    }
    
    return playerScore;
}


int main()
{
    srand(time(0));
    
    int player1score = 0;
    int player2score = 0;
    string player1name;
    string player2name;
    int dieRoll;
    
    printIntro();
    
    cout << " Player 1, Enter your name: ";
    cin >> player1name;
    cout << " Player 2, Enter your name: ";
    cin >> player2name;
    cout << "Wouild you like to roll with 1 or 2 dice?" << endl;
    cin >> dieRoll;
    
    if (dieRoll == 1)
    {
        while (player1score < winningScore && player2score < winningScore)
        {
        
        
            if (turn == PLAYER1)
            {
                player1score = game1(player1name, player1score);
            }
        
            else
            {
                player2score = game1(player2name, player2score);
            }
        
        }
    
        if(player1score >= winningScore)
        {
            cout << player1name <<endl;
            cout << " Your score is : " << player1score<<endl;
            cout << player1name << " WINS! " << endl;
        }
    
        else
        {
            cout << player2name << endl;
            cout <<" Your score: "<< player2score << endl;
            cout << player2name << " WINS!" << endl;
        }
    }

    else
    {
        while (player1score < winningScore && player2score < winningScore)
        {
            
            
            if (turn == PLAYER1)
            {
                player1score = game2(player1name, player1score);
            }
            
            else
            {
                player2score = game2(player2name, player2score);
            }
            
        }
        
        if(player1score >= winningScore)
        {
            cout << player1name <<endl;
            cout << " Your score is : " << player1score<<endl;
            cout << player1name << " WINS! " << endl;
        }
        
        else
        {
            cout << player2name << endl;
            cout <<" Your score: "<< player2score << endl;
            cout << player2name << " WINS!" << endl;
        }
    }
    
    return 0;
}
if (roll1 || roll2 == 1) and else if (roll1 && roll2 == 1) are the problem. The || and && cannot join together two objects to test their equality against one. They join together two different boolean expressions so in line 93 the program evaluates roll1 as its own boolean expression. And since the dice can never be zero it immediately evaluates to true so the code will always run as if the players rolled a one; therefore, the program continues to set the score to zero so it needs to switch turns then roll again but since that roll will also evaluate to true this continue to repeats ad infitum.
Topic archived. No new replies allowed.