Craps game help

I think i'm pretty close, a few issues i see running it is 12 doesn't always end game with loss on first roll and probability does not calculate for some reason. Any direction would be appreciated. Leaving for phish fest tonight and need to get this figured out!

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
#include <iostream>
#include <ctime>    // for time()
#include <cstdlib>
using namespace std;


int main ()
{
    int dieOne=0;
    int dieTwo=0;
    int roll=0;
    int point=0;
    int win=0;
    int loss=0;
    double odds=0;
    srand(time(0));    //makes time random seed




    for(int game=1; game<4; game++)
    {
        dieOne=(rand()%6)+1;//creates dice
        dieTwo=(rand()%6)+1;
        roll=dieOne+dieTwo;

        cout<<dieOne<<" plus "<<dieTwo<<" = "<<roll<<endl;

        if(roll==7 || roll==11)//win on first roll
            win++;
        else if(roll==2 || roll==3 || roll==12)//lose on first roll
            loss++;
        else if(roll==4 || roll==5 || roll==6 || roll==8 || roll==9 || roll==10)
        {
            point=roll;//point set
            do
            {

                dieOne=(rand()%6)+1;//rolls dice again
                dieTwo=(rand()%6)+1;
                roll=dieOne+dieTwo;

                cout<<dieOne<<" plus "<<dieTwo<<" = "<<roll<<endl;

                if(roll==point)//wins if player rolls point
                    win++;
                else if(roll==7)//lose if player rolls 7
                    loss++;
                else if(roll!=7 && roll!=point) //nothing otherwise
                    odds=0;

            }while(roll!=point && roll!=7);//keep going until point or 7 is rolled

            cout<<"game over- game #: "<<game<<endl;

            }

    }
    odds=(win/(win+loss));  //calculate probability
    cout<<win<<" wins and "<<loss<<" loses"<<endl;
    cout<<"Probability to win is: "<<odds<<endl;
}
Last edited on
Calculating odds; you have an integer divided by another integer. That will give you an integer output. You don't want that. You must turn them into floating point values before you do the calculation.

Your result has to do with integer division. When you divide 1 / 3 (for example) you get 0, not 0.33. You need to force double division, like:
 
odds = ((double)win/(win+loss));


Happy coding!
ah, bonehead there ><, thanks!

anything on the logic error part of game? i've been trying to rework it with a combo of switch statements, but this seems much closer. also thinking of breaking it down into a few functions with switch statements that would do checks instead of if-else.

edit: i tried to set point to 0 at the end of loop once point had been checked and it really messes up program. i wouldn't think it would need to be reinitialized to 0 anywhere because point changes before it gets checked again anyway i think...
Last edited on
I'll preface this response in saying that I am a new student to C++ and very much a beginner in programming as a whole, but I like to review threads like this to try and problem solve for my own benefit as well.

When I ran the program myself, I noted that rolling an automatic win or automatic loss worked in all respects except notifying you that the game had ended. The next game just picked up without a visual cue that the prior game ended.

I tested this theory by altering the code so that for the first roll, both dies were equal to 6 rather than a random number. The output was:

6 plus 6 = 12
6 plus 6 = 12
6 plus 6 = 12
0 wins and 3 loses
Probability to win is: 0


Note that the same would happen if you rolled a winning roll. Everything would work correctly except for the output indicating that the game ended. This issue is caused because that line of output is nested within the "else if" statement for when you roll a point on the first roll.
Topic archived. No new replies allowed.