Need help with craps game

I need to make a craps game for my programming class but there are a few issues when I try to execute it.

1. How do I calculate the win percentage?
2. When I ask if the user wants to play another game and I say no, it's supposed to show the total wins and losses as well as the win percentage. But it comes up as an infinite loop that keeps going until I terminate the program. How do I fix this?
3. When I display the score, it's incorrect.
4. When I ask if the user wants to play craps and I say yes, for the first roll it rolls twice. I just need it to roll once and then for the user to press enter for it to roll again.

If anyone can help me with these issues that would be great!

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

const int WIN1 = 7;
const int WIN2 = 11;
const int LOSE1 = 2;
const int LOSE2 = 3;
const int LOSE3 = 12;

void crapsGame();

int main()
{
    char play;
    
    cout << "Would you like to play craps? Y/N" << endl;
    cin >> play;
    do{
        switch(play)
        {
            case 'Y': crapsGame();
                break;
            case 'y': crapsGame();
                break;
            case 'N': cout << "Goodbye!" << endl;
                return EXIT_SUCCESS;
                break;
            case 'n': cout << "Goodbye!" << endl;
                return EXIT_SUCCESS;
                break;
            default: cout << "Invalid input, please try again." << endl;
                cout << "Would you like to play craps? Y/N" << endl;
                cin >> play;
        }
    } while(play != 'Y' || play != 'y' || play == 'N' || play == 'n');
    
    return EXIT_SUCCESS;
}

void crapsGame()
{
    int wins = 0;
    int losses = 0;
    
    default_random_engine randomEngine{static_cast<unsigned int> (time(0))};
    uniform_int_distribution<unsigned int> randomInt{1,6};

    unsigned int randomNumber1 = randomInt(randomEngine);
    unsigned int randomNumber2 = randomInt(randomEngine);
    unsigned int randomNumberTotal = randomNumber1 + randomNumber2;
    
    cout << randomNumber1 << " : " << randomNumber2 << " = " << randomNumberTotal << endl;
    
    if(randomNumberTotal == WIN1 || randomNumberTotal == WIN2)
    {
        char playAgainWin;
        
        cout << "Congratulations, you won!" << endl;
        ++wins;
        cout << "Would you like to play another game? Y/N" << endl;
        cin >> playAgainWin;
        do{
            switch(playAgainWin)
            {
                case 'Y': crapsGame();
                    break;
                case 'y': crapsGame();
                    break;
                case 'N':
                    cout << "Wins: " << wins << endl;
                    cout << "Losses: " << losses << endl;
                    break;
                case 'n':
                    cout << "Wins: " << wins << endl;
                    cout << "Losses: " << losses << endl;
                    break;
                default: cout << "Invalid input, please try again." << endl;
                    cout << "Would you like to play another game? Y/N" << endl;
                    cin >> playAgainWin;
            }
        } while(playAgainWin == 'Y' || playAgainWin == 'y' || playAgainWin == 'N' || playAgainWin == 'n');
        
    }
    else
    {
        if (randomNumberTotal == LOSE1 || randomNumberTotal == LOSE2 || randomNumberTotal == LOSE3)
        {
            char playAgainLost;
            
            cout << "You lost!" << endl;
            ++losses;
            cout << "Would you like to play another game? Y/N" << endl;
            cin >> playAgainLost;
            do{
                switch(playAgainLost)
                {
                    case 'Y': crapsGame();
                        break;
                    case 'y': crapsGame();
                        break;
                    case 'N':
                        cout << "Wins: " << wins << endl;
                        cout << "Losses: " << losses << endl;
                        break;
                    case 'n':
                        cout << "Wins: " << wins << endl;
                        cout << "Losses: " << losses << endl;
                        break;
                    default: cout << "Invalid input, please try again." << endl;
                        cout << "Would you like to play another game? Y/N" << endl;
                        cin >> playAgainLost;
                }
            } while(playAgainLost == 'Y' || playAgainLost == 'y' || playAgainLost == 'N' || playAgainLost == 'n');
        }
        else
        {
            cout << "Please roll again" << endl;
            cin.get();
        }
    }
}
> How do I calculate the win percentage?
number_of_wins * 100. / times_played;
or perhaps
number_of_wins * 100. / (number_of_wins + number_of_loses);

> When I ask if the user wants to play another game and I say no
first, you can't say `no'. either `y' or `n', just one character, because you used a char to hold the answer.

second, your logic is all over the place. limit your input/output to main()
you have three places when you ask the user if they want to play:
- the first time in main()
- when they win
- when they lose (¿why should this be different?)
also you use a loop but then launch the function through recursion
and once you enter a valid answer, you never change the variable anymore.

really, scratch all that, limit your input/output to main()

> When I display the score, it's incorrect.
¿incorrect how? be more descriptive
¿what's the result you are seeing? ¿what are you expecting?
the error is probably because of the recursive calls.

> When I ask if the user wants to play craps and I say yes, for the first roll it rolls twice.
¿you pressed <Return> after 'y' right?
cin >> play; cin.ignore();
Last edited on
Topic archived. No new replies allowed.