blank

question removed
Last edited on
With a few of changes, your code works.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <ctime>
using namespace std;

const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int WINNING_SCORE = 100;

    void printIntro(){
    cout << "Welcome to the dice game Pig!\nThe objective is to be first to score 100 points." << endl;
    cout << endl;
    }
    int humanTurn(string playerName, int playerScore){
int diceRoll;
char rollAgain;
diceRoll = 1 + rand() % 6;
if(diceRoll != 1 && playerScore < WINNING_SCORE){
    cout << playerName << endl;
    cout << "You rolled a " << diceRoll << endl;
    playerScore += diceRoll;
    cout << "Your score: " << playerScore << endl;
    cout << "Do you want to roll again? (y/n): ";
    cin >> rollAgain;
    cout << endl;
    diceRoll = rand() % 6 + 1;
    while(rollAgain == 'y'){
        if(diceRoll != 1 && playerScore < WINNING_SCORE){
        cout << playerName << endl;
        cout << "You rolled a " << diceRoll << endl;
        playerScore += diceRoll;
        cout << "Your score: " << playerScore << endl;
        cout << "Do you want to roll again? (y/n): ";
        cin >> rollAgain;
        cout << endl;
        diceRoll = rand() % 6 + 1;
        }
        else if(diceRoll == 1 && playerScore < WINNING_SCORE){
        cout << playerName << endl;
        cout << "You rolled a " << diceRoll << " (PIG!)" << endl;
        cout << "Your turn is over" << endl; 
        cout << "Your score is: " << playerScore << endl;
        cout << endl;
	break;
        }
    }
}
else if(diceRoll == 1 && playerScore < WINNING_SCORE){
    cout << playerName << endl;
    cout << "You rolled a " << diceRoll << " (PIG!)" << endl;
    cout << "Your turn is over" << endl; 
    cout << "Your score is: " << playerScore << endl;
    cout << endl;
}
return playerScore;
}

int main() {
    srand(time(0));

    int turn = PLAYER1;
    int player1score = 0;
    int player2score = 0;
    string player1name;
    string player2name;
    
    printIntro();

    cout << "Player 1 - Enter your name: ";
    cin >> player1name;
    cout << endl;
    cout << "Player 2 - Enter your name: ";
    cin >> player2name;
    cout << endl;

    while (player1score < WINNING_SCORE && player2score < WINNING_SCORE) {
        if (turn == PLAYER1) {
            player1score = humanTurn(player1name, player1score);
            turn = PLAYER2;
        }
        else {
            player2score = humanTurn(player2name, player2score);
            turn = PLAYER1;
        }
    }

    if(player1score >= WINNING_SCORE){
    cout << player1name << " wins!" << endl;
    }
    else{
    cout << player2name << " wins!" << endl;
    }

    return 0;
}
Last edited on
@Fanboro
Thank you for the help! However, I have to keep the srand(4444), so that can not be changed.
it does not work and ends up being program timed out

What do you mean by "timed out"? I see no timers or timed operations in your program.
So I fixed parts of it and now the code is
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int WINNING_SCORE = 100;


//FIXME (1): Implement the printIntro function
    void printIntro(){
        cout << "Welcome to the dice game Pig!\nThe objective is to be first to score 100 points." << endl;
        cout << endl;
    }
//FIXME (4, 5, 6): Implement the humanTurn function
    int humanTurn(string playerName, int playerScore){
    int diceRoll;
    char rollAgain;
    diceRoll = 1 + rand() % 6;
if(diceRoll != 1 && playerScore < WINNING_SCORE){
    cout << playerName << endl;
    cout << "You rolled a " << diceRoll << endl;
    playerScore += diceRoll;
    cout << "Your score: " << playerScore << endl;
    cout << "Do you want to roll again? (y/n): ";
    cin >> rollAgain;
    cout << endl;
    while(rollAgain == 'y' && (diceRoll != 1 && playerScore < WINNING_SCORE)){
        diceRoll = rand() % 6 + 1;
        cout << playerName << endl;
        cout << "You rolled a " << diceRoll << endl;
        playerScore += diceRoll;
        cout << "Your score: " << playerScore << endl;
        cout << "Do you want to roll again? (y/n): ";
        cin >> rollAgain;
        cout << endl;
    }
    if(rollAgain == 'y' && (diceRoll == 1 && playerScore < WINNING_SCORE)){
        cout << playerName << endl;
        cout << "You rolled a " << diceRoll << " (PIG!)" << endl;
        cout << "Your turn is over" << endl; 
        cout << "Your score is: " << playerScore << endl;
        cout << endl;
    }
        
}
else if(diceRoll == 1 && playerScore < WINNING_SCORE){
    cout << playerName << endl;
    cout << "You rolled a " << diceRoll << " (PIG!)" << endl;
    cout << "Your turn is over" << endl; 
    cout << "Your score is: " << playerScore << endl;
    cout << endl;
}
return playerScore;
}

int main() {
    srand(4444);

    // setup and initialize variables
    int turn = PLAYER1;
    int player1score = 0;
    int player2score = 0;
    string player1name;
    string player2name;
    
    printIntro();

    // FIXME (2): get names of players
    cout << "Player 1 - Enter your name: ";
    cin >> player1name;
    cout << endl;
    cout << "Player 2 - Enter your name: ";
    cin >> player2name;
    cout << endl;


    //play game
    while (player1score < WINNING_SCORE && player2score < WINNING_SCORE) {

        //player 1's turn or player 2's turn
        if (turn == PLAYER1) {
            player1score = humanTurn(player1name, player1score);
            turn = PLAYER2;
        }
        else {
            player2score = humanTurn(player2name, player2score);
            turn = PLAYER1;
        }
    }
   
    // FIXME (7): Output who won the game
    if(player1score >= WINNING_SCORE){
    cout << player1name << " wins!" << endl;
    }
    else{
    cout << player2name << " wins!" << endl;
    }

    return 0;
}


How do I get out of the while loop so that when diceRoll == 1, it outputs correctly
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/203886/
OP: please reinstate your original post(s) if you want any further help
Topic archived. No new replies allowed.