S
Sonia
Hi, Please look at the following code. This is a simple program for a game
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there
is a better way of handling this (possibly via local variables)
I am using that variable to keep track of the status of the game and pass
it back to main() from craps() where I either increase or decrease a bank
balance.
Should this be handled with local parameters ? would that be a better way.
If so, how would I do that ?
Thanks
Following is the code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int rollDice();
int craps();
enum Status {CONTINUE, WON, LOST};
Status gameStatus;
int main(){
int bankBalance = 1000;
int wager;
char answer;
do {
cout<<"Place a wager: ";
cin>>wager;
while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}
craps();
if (gameStatus == WON) {
bankBalance = bankBalance + wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}
else {
bankBalance = bankBalance - wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}
cout<<"Do you want to play again? (y/n)";
cin>>answer;
} while( answer =='y' || answer == 'Y'); // end of while
return 0;
}
int craps() {
int sum, myPoint;
srand(time(0));
sum = rollDice();
switch (sum) {
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;
}
while(gameStatus==CONTINUE) {
sum=rollDice();
if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}
if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;
return(gameStatus);
}
int rollDice() {
int die1, die2, workSum;
die1 = 1+rand() % 6;
die2 = 1+rand() % 6;
workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;
return workSum;
}
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there
is a better way of handling this (possibly via local variables)
I am using that variable to keep track of the status of the game and pass
it back to main() from craps() where I either increase or decrease a bank
balance.
Should this be handled with local parameters ? would that be a better way.
If so, how would I do that ?
Thanks
Following is the code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int rollDice();
int craps();
enum Status {CONTINUE, WON, LOST};
Status gameStatus;
int main(){
int bankBalance = 1000;
int wager;
char answer;
do {
cout<<"Place a wager: ";
cin>>wager;
while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}
craps();
if (gameStatus == WON) {
bankBalance = bankBalance + wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}
else {
bankBalance = bankBalance - wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}
cout<<"Do you want to play again? (y/n)";
cin>>answer;
} while( answer =='y' || answer == 'Y'); // end of while
return 0;
}
int craps() {
int sum, myPoint;
srand(time(0));
sum = rollDice();
switch (sum) {
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;
}
while(gameStatus==CONTINUE) {
sum=rollDice();
if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}
if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;
return(gameStatus);
}
int rollDice() {
int die1, die2, workSum;
die1 = 1+rand() % 6;
die2 = 1+rand() % 6;
workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;
return workSum;
}