Dice Game. Trouble finishing it off.

Hi there,
Here is the problem:
I have to create a craps game:

Roll two dice. each dice has six faces representing values 1, 2, … and 6, respectively. check the sum of the two dice. if the sum is 2, 3, or 12(Called craps), you loose; if the sum is 7 or 11 (called natural) you win; if the sum is another value (i.e. 4, 5, 6, 8, 9, or 10) a point is established. continue until you roll either a 7(you lose) or the same point value(you win).


I am stuck finishing off this program.. i know i am close but i just can't seem to get to the end.. any ideas?

Thank you!

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
  1 #include <iostream>
  2 #include <ctime>
  3 #include <cstdlib>
  4 #include <unistd.h>
  5 using namespace std;
  6 
  7 int main()
  8 {
  9         int dice1, dice2, sum, point;
 10         srand(time(0));
 11         dice1 = (rand()% 6) + 1;
 12         dice2 = (rand()% 6) + 1;
 13         sum = dice1 + dice2;
 14 
 15         if (sum == 7 || sum == 11)
 16         {
 17                 cout << "win!" << "from " << dice1 <<" and " << dice2 <<endl;
 18         }
 19         else if (sum == 2 || sum == 3 || sum == 12)
 20         {
 21                 cout << "lose!" << "from " << dice1 << " and " <<dice2 << endl;
 22         }
 23         else
 24         {
 25         cout << "your point is: " << sum <<endl;
 26         point = sum;
 27 
 28         do {
 29                 dice1 = (rand()% 6) + 1;
 30                 dice2 = (rand()% 6) + 1;
 31                 sum = dice1 + dice2;
 32                 sleep(1);
 33                 cout << "your point is:  " << sum << endl;
 34         }while ( sum != 7 && sum != 11 && sum != point);
 35 
 36         if(sum == 11 || sum == 7 )
 37         {
 38                 cout<< "you lost" << endl;
 39         }
 40 
 41         else
 42         {
 43 
 44                 cout <<"you win!" << endl;
 45         }
 46         return 0 ;
 47 }
 48 }
 49                                                                                                                        
Last edited on
What is the actual problem with your code?

Once I took the line numbers out that you had included (you don't need them - the code tags will show them automatically) it seemed to compile and run OK.

Line 36 isn't quite what you said in the description, although it might have been what you intended.
You can't just say 'i am stuck to finish off this program'. At lease you need to tell us which part was not expected.
sorry about that.
I am stuck on the 2nd roll. it won't check properly for point equaling to dice sum.

my new code:

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

  1 #include <iostream>
  2 #include <ctime>
  3 #include <cstdlib>
  4 #include <unistd.h>
  5 using namespace std;
  6 
  7 int main()
  8 {
  9         int dice1, dice2, sum, point;
 10         srand(time(0));
 11         dice1 = (rand()% 6) + 1;
 12         dice2 = (rand()% 6) + 1;
 13         sum = dice1 + dice2;
 14 
 15         if (sum == 7 || sum == 11)
 16         {
 17                 cout << "win!" << "rolled " << dice1 <<" + " << dice2 << "=" << sum  <<endl;
 18         }
 19         else if (sum == 2 || sum == 3 || sum == 12)
 20         {
 21                 cout << "lose!" << "rolled " << dice1 << " + " <<dice2 << "=" << sum << endl;
 22         }
 23         else
 24         {
 25         cout << "you rolled" << dice1 << "+" << dice2 << "=" << sum <<endl;
 26         cout << "your point is: " << sum <<endl;
 27         point = sum;
 28 
 29         do {
 30                 dice1 = (rand()% 6) + 1;
 31                 dice2 = (rand()% 6) + 1;
 32                 sum = dice1 + dice2;
 33                 sleep(1);
 34         }while ( sum != 7 && sum != point);
 35 
 36         if(sum == point && sum != 7 )
 37         {
 38                 cout <<"you rolled" << dice1 << "+" << dice2  << "=" << sum <<endl;
 39                 cout<< "you win" << endl;
 40         }
 41 
 42         else
 43         {
 44                 cout <<"you rolled" << dice1 << "+" << dice2 << "=" << sum << endl;
 45                 cout <<"you lost" << endl;
 46         }
 47         return 0 ;
 48 }
 49 }
 50 
                                                                                                                                                                                                                                             
Last edited on
Could you give us a series of dice rolls that you believe is wrong? When I try it, it seems to work as intended.


Minor points (none of which are strictly errors):
- You have put line numbers in yourself in the posted code. This is unnecessary and doesn't help because (a) we have to take them out in order to compile; (b) they don't tie up with the ones put to the side by the code tags;
- Your line (37 from the code tags, OR 36 from your code)
if(sum == point && sum != 7 )
is slightly redundant, because point can't equal 7 (or the game would have already ended) so if sum == point then certainly sum != 7; just
if(sum == point)
would work here.
- Your indentation for the else block is inconsistent with the rest of the if ..else .. groups (which threw me when I first looked at your code)
- It would be easier to see what was going on if you output the results of the second roll, even if they don't end the game.
Hi there,

Thank you for your help!
I've added your suggestions and It appears to be working correctly!

Here is the final code(with numbers removed):
let me know what you think.

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

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <unistd.h>
    using namespace std;
    
    int main()
    {
            int dice1, dice2, sum, point;
            srand(time(0));
            dice1 = (rand()% 6) + 1;
            dice2 = (rand()% 6) + 1;
            sum = dice1 + dice2;
    
            if (sum == 7 || sum == 11)
            {
                    cout << "win!" << "rolled " << dice1 <<" + " << dice2 << " = " << sum  <<endl;
             }
              else if (sum == 2 || sum == 3 || sum == 12)
             {
                    cout << "lose!" << "rolled " << dice1 << " + " <<dice2 << " = " << sum << endl;
             }
             else
             {
                     cout << " you rolled " << dice1 << " + " << dice2 << " = " << sum <<endl;
                     cout << "your point is: " << sum <<endl;
                      point = sum;
 
                      do {
                              dice1 = (rand()% 6) + 1;
                              dice2 = (rand()% 6) + 1;
                              sum = dice1 + dice2;
                              sleep(1);
                    }while ( sum != 7 && sum != point);
    
             if(sum == point)
             {
                     cout <<" you rolled " << dice1 << " + " << dice2  << " = " << sum <<endl;
                     cout<< "you win!" << endl;
             }
    
             else
             {
                     cout <<" you rolled " << dice1 << " + " << dice2 << " = " << sum << endl;
                     cout <<"you lost" << endl;
            }
            return 0 ;
     }
    } 
It seems to be working as intended. Well done!

Judging by my record whilst running it I won't be playing the tables in Monte Carlo just yet, though!
There are a lot of similar programs in the web. You can always search them for learning :)
Many thanks to all!
Topic archived. No new replies allowed.