Hi guys, I have an assignment to create a program that simulates the game "Craps" using enumeration.
Rules of Craps-Roll two dice.
If the shooter rolls a 7 or an 11 on the 1st roll, they win
If the shooter rolls a 2, 3, or 12 on the 1st roll, they lose
If the shooter rolls anything else, they continue to roll the dice until either of the following occurs:
- They roll their initial sum, which means they win.
-They roll a 7, which means they lose.
Basically my problem is that after a player wins and wants to play again by entering 0, my program outputs a message as if the computer had run a simulation on its own. Its kind of hard to explain so I will do it with screen shots.
Initial Screen:
http://i.imgur.com/TbWWq.png
After enter stroke:
http://i.imgur.com/eFIk6.png
After I entered 0:
http://i.imgur.com/OuNWW.png
As you can see, even though my code requires you to hit enter before the next function is called, somehow output gets called without the enter.
The code:
Code:
#include<iostream>#include<time.h>
#include <cstdlib>
using namespace std;
enum Status{WON,LOST,CONTINUE};
int RollDice(int &turn);
bool check(Status x, int turn,int val, int num);
void assign(Status &status, int &turn, int &val, int &num);
void output(Status status,int val);
int main(){
int val=0,num=0;
int turn=0,ans;
Status status;
srand(time(0));
cout<<"Welcome to Craps!"<<endl;
do{
do{
assign(status,turn,val,num);
output(status,val);
}while(check(status,turn,val,num));
turn=0;
num=0;
val=0;
num=0;
cout<<"Enter 0 to play again"<<endl;
cin>>ans;
system("cls");
}while(ans==0);
}
int RollDice(int &turn)
{
int roll=rand()%12+1;
return roll;
turn++;
}
bool check(Status status, int turn, int val, int num)
{
switch(status){
case WON:
return false;
break;
case LOST:
return false;
break;
case CONTINUE:
return true;
}
/*
if((turn==0)&&(num==0)){
switch(status)
{
case WON:
return false;
break;
case LOST:
break;
default:
status=CONTINUE;
num=val;
return true;
break;
}
}
else{
if(num==val){
status=WON;
return false;
}
else if(num==7){
status=LOST;
return false;
}
else{
status=CONTINUE;
return true;
}
}*/
}
void output(Status status,int val)
{
switch(status){
case WON:
cout<<"You win with "<<val<<endl;
break;
case LOST:
cout<<"You lose with "<<val<<endl;
break;
case CONTINUE:
cout<<"CONTINUE"<<endl;
}
}
void assign(Status &status, int &turn, int &val, int &num)
{
cout<<"LAST TURN's NUM: "<<num<<endl;
cout<<"What you rolled last turn: "<<val<<endl;
val=RollDice(turn);
cout<<"What you rolled this turn: "<<val<<endl;
cout<<"TURN="<<turn<<endl<<"NUM="<<num<<endl;
cout<<"Press enter to roll";
cin.get();
if((turn==0)&&(num==0))
{
turn++;
switch(val)
{
case 7:
case 11:
status=WON;
break;
case 2:
case 3:
case 12:
status=LOST;
break;
default:
status=CONTINUE;
num=val;
break;
}
}
else{
if(num==val)
status=WON;
else if(val==7)
status=LOST;
else{
status=CONTINUE;
num=val;
}
}
}