Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Craps simulation

simulation

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 napalmgrenade

napalmgrenade

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 06 January 2013 - 02:44 PM

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.

#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<<"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;
}
}
}


Edited by Roger, 07 January 2013 - 09:31 AM.
remove font formatting


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 06 January 2013 - 03:59 PM

you need to move the intialization of variables (turn, num, etc) to between the first and second do
It will fix your problem

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts

Posted 06 January 2013 - 07:15 PM

The reason of that behavior is that the enter key (newline) you are pressing when the program is asking you with prompt 'Enter 0 to play again' and that newline is causing the cin.get() in method assign not to wait for another enter key press. So the solution is you need to discard one character from the cin stream after pressing a key following the 'Enter 0 to play again' prompt. So just add the cin.ignore() statement after the cin>>ans in the main function.

Note, as discarding (or reading the next character) after the prompt will solve this, you can use cin.get instead of cin.ingore to read that troubling 'newline'.




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download