[homework] Craps simulation

I've been learning C++ for the past 2 weeks and this is my assignment:

Craps is a simple dice game often played in casinos. A player throws a pair of dice (often repeatedly), with the following win/loss conditions:

A 7 or 11 on the first roll wins.
A 2, 3, or 12 on the first roll loses.
Under all other circumstances, rolling will continue.
The total on the first roll is called the point. Rolling will continue until either:
The point is rolled (win)
A 7 is rolled (lose)


KEEP IN MIND: My C++ is VERY basic, i've only learned the basics of boolean and while + if/else. Please try to keep the help basic if possible as I don't want to be too confused.


This is what I have so far:


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
#include <iostream>

using namespace std;

// Beginning User Commands

int main ()
{

int dice1;
int dice2;
int point;
char R;
char response;

dice1 = 1 + rand() % 6;
dice2 = 1 + rand() % 6;
point = dice1 + dice2;



cout << "Let's Play Craps!" << endl;

cout << "Enter R to roll : ";

cin >> R;

cout << "Your Roll is "<< dice1 <<" and " << dice2 << " which gives a total roll of " << point << "." << endl;

if (point == 7 || point == 11)
{
	cout<< "You Win!!"<< endl;
}
else if (point == 2 || point ==3 || point == 12)
{

	cout << "You Lose!!"<< endl;
	cout << "Would you like to play again? (Y/N)";
	cin >> response;
			while (response =='y' || response=='Y');

}
else 
{
	cout << "You did not Win or Lose! Do you want to roll again? (Y/N)";
	cin >> response;

	while (response =='y' || response=='Y');


}


cout << endl;



}




What I am trying to figure out is how to make the program repeat. Right now, I can get the 2 random numbers to generate (It always comes out to 6 and 6, but the professor said that's okay) and then I can ask the user if he wants to play again.

They can press Y but nothing happens, how can I repeat the program?


I just learned 'char' on my own today and tried to implement it into my program, so please tell me if I did something wrong.


Thank you!
The easiest way to make it repeat is wrap the whole thing in a loop.

Create a variable. Call it running or something of that nature. Initialise it to true.

Then, encompass all of the code you want to repeat in a loop that is dependent on running being true. Ask the user if they want to play again at the end of the loop. If they answer anything other than 'y', set running to false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool running = true;

do
{
   // All of your game logic here...

   // At the end
   std::cout << "Do you want to play again? (Y/N)";
   std::cin >> response;

   if( response != 'y' && response != 'Y' )
   {
      running = false;
   }

} while( running );


EDIT: Just in case the do-while loop confuses you, it's very similar to a while loop. The major difference being that the do-while will always run the code once, regardless of the while condition.
Last edited on
Thanks for the help!

I tried this, but when I enter Y it seems to not repeat. It does not do anything after I enter Y. However, when I press N, it will stop the program.
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
#include <iostream>

using namespace std;

// Beginning User Commands

int main ()
{

int dice1;
int dice2;
int	point;
char R;
char response;

dice1 = 1 + rand() % 6;
dice2 = 1 + rand() % 6;
point = dice1 + dice2;

bool keepPlaying = true;


do {

cout << "Let's Play Craps!" << endl;

cout << "Enter R to roll : ";

cin >> R;

cout << "Your Roll is "<< dice1 <<" and " << dice2 << " which gives a total roll of " << point << "." << endl;

if (point == 7 || point == 11)
{
	cout<< "You Win!!"<< endl;
}
else if (point == 2 || point ==3 || point == 12)
{

	cout << "You Lose!!"<< endl;
	
			
}
else 
{
	cout << "You did not Win or Lose! Do you want to roll again? (Y/N)";
	cin >> response;


}

cout << "Do you want to play again? (Y/N)" 
cin >> response

if (response != 'y' && response != 'Y')
{
	keepPlaying = false;

}

} while (keepPlaying);

}
	


This is what I have now.

EDIT: Is there a way to use a while loop at the beginning to make it repeat?

Thank you
Last edited on
Worked fine for me apart from a complication error from missing terminators at the end of lines 52 and 53.

I would also advise not asking if the user wants to play again in the else block where they neither win or lose. Just ask it once at the very end.

It repeats fine for me when y or Y is entered.

One thing I will say - you need to generate some new random numbers at each iteration of the loop, otherwise you'll get the same numbers ever time. Move the dice1 and dice2 random assignments plus the point calculation so they're inside the loop.

Finally, since you're using random numbers you'll want to see the random number generator. To do this, you need to include the time header and call srand:

1
2
3
4
#include <ctime>

// Then, at the top of main
srand( time( NULL ) );
Okay I am a beginner at C++ and I have to do the same thing. But I am not sure if I am doing it write. My teacher wants a function and I'm not certain if mine contains it.


//Christy Windham
#include <iostream>
#include <ctime>
#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((unsigned int)time(NULL));



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

cout<<dieOne<<" plus "<<dieTwo<<" = "<<roll<<endl; //Prints random dice numbers to screen

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

}
ckw77 wrote:
My teacher wants a function and I'm not certain if mine contains it.


Nope, there's no function in there aside from main.

Did your teacher specify as to what the function should do?
Show that you can create a function that generates a random number from
1 to n and return the generated random number. Also show that you can display
the random number returned by this function to the console window.

I don't Understand!!
I'm going to move this to the thread you've created, so as to not hijack the OP's thread.
Topic archived. No new replies allowed.