Pig Dice Game

The program works except the win lose condition does not trigger. If the player or computer reaches a score of 100 or more then the game is over. However this does not happen. Any ideas?

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int SCORE_LIMIT = 100;
int diceRoll();
int humanTurn(int& humanTotalScore);
int computerTurn(int& computerTotalScore);

int main()
{
    bool win = 1;
    int humanTotalScore = 0, computerTotalScore = 0;

    cout << "It is your turn. Press r to roll.\n";
    if ((humanTotalScore < SCORE_LIMIT) && (computerTotalScore < SCORE_LIMIT))
    {
        win = 1;
    }
    else
    {
        win = 0;
    }
    do
    {
        cout << "Computers Score: " << computerTotalScore << endl;
       
        humanTurn(humanTotalScore);
        computerTurn(computerTotalScore);
    }
    while(win = 1);
   
    if ((win = 0) && (humanTotalScore > computerTotalScore))
    {
        cout << "You win!";
    }
    else
    {
        cout << "You lost!";
    }
   
   
    return 0;
}


int humanTurn(int& humanTotalScore)
{
    int currentScore = 0;
    int lastRoll;
    char rollOrHold;

    cout << "Your total score is: " << humanTotalScore << "." << endl;
    cout << "Press r to roll again, or h to hold." << endl;
    cin >> rollOrHold;

    while (rollOrHold == 'r')
    {
        srand (time(NULL));
        lastRoll = diceRoll();
        if (lastRoll == 1)
        {
            cout << "You rolled a 1, ending your turn." << endl;
            break;
        }
        else
        {
            currentScore += lastRoll;
            cout << "You roll a " << lastRoll << ". Your score this turn is: " << currentScore << endl;
            cout << "Press r to roll again, or h to hold." << endl;
            cin >> rollOrHold;
        }
    }
    while (rollOrHold == 'h')
    {
        humanTotalScore += currentScore;
        break;
    }
   
    return humanTotalScore;
}

int computerTurn(int& computerTotalScore)
{
    int currentScore = 0;
    int lastRoll;

    cout << "Computer's total score is: " << computerTotalScore << "." << endl;
    while ((currentScore <= 20) && (currentScore != 1))
    {
        lastRoll = diceRoll();
        if (lastRoll == 1)
        {
            cout << "The computer rolled a 1, ending their turn." << endl;
            break;
        }
        else
        {
            currentScore += lastRoll;
            cout << "The computer rolls a " << lastRoll << ". Computer's score this turn is: " << currentScore << endl;
        }
    }
    if(currentScore >= 20)
    {
        computerTotalScore += currentScore;
        cout << "After the computer's turn, they have gained an additional " << lastRoll << " points." << endl;
    }
   
    return computerTotalScore;
}

int diceRoll()
{
    int roll;

    roll = (int)(rand()%6)+1;
   
    return roll;
}
Lines 32 and 34 are actually assignment statements and are not checking with the logical operator '==' like you want it to

Edit** relational not logical, my bad
Last edited on
Changed that, but still doesn't work :/
Your "game loop" is on lines 25-32, and it doesn't include the win/loss setters.
from what firedraco said it looks like you have an infinite loop. 17-24 should be inside 25-32
Topic archived. No new replies allowed.