Question

Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer...

Game of Craps C++

#include <iostream>

using namespace std;
void roll (int *); // using pointer to catch the random variable value of two dicerolls at one time .

//these global variable with static variable win use to operate the working of code .
//static win, account because two times input given by user to add the diceroll with win number and
//account (means balance of user) .

static int account = 100;
static int win = 0;
int bet = 0;

int main ()
{
char c;
int diceroll1, diceroll2;
cout << "Welcome to the CRAPS game. ";
  
while(1){   
cout << "You have $"<<account<<". Do you want to roll (y/n)?";
cin >> c;

if (c == 'y')
{
cout << "How much do you want to bet? ";
cin >> bet;
diceroll1 = rand () % 6 + 1;
diceroll2 = rand () % 6 + 1; // rand()%6+1 get the value between 6 to 1 and also included 6 or 1 both .
roll (&diceroll1); // send the address of diceroll values .
roll (&diceroll2);
  
if (win == 7)
{
cout << "You rolled a " << diceroll1 << " and a " << diceroll2 <<
   ". Your total is 7. You win! ";
   account+=bet;
   cout<<"You have $"<<account<<" ";
}
else if (win == 11)
{
   cout << "You rolled a " << diceroll1 << " and a " << diceroll2 <<
   ". Your total is 11. You win! ";
account+=bet;
cout<<"You have $"<<account<<" ";
  
}
else if(win==2 ||win==3 ||win==12){
cout<<"You lose! ";
}
  
}
else if(c=='n'){
cout<<"Thank you for playing.";
break;
}
}
return 0;
}

void roll (int *a)
{
win = win + *a;
  
}

Modify the craps program to keep track of your bank. After each hand, you have “Do you want to roll again (y/n/s). If you select “s” you can save your amount of money. The game then repeats the question so you can continue or press n to quit. When you first start the program it asks, “Do you want to load a previous session?” If you say ‘y,’ your bank is updated to the amount it was when it was saved. If you say ‘n,’ the game proceeds as before. If there was no previous session, obviously the program won’t be able to find your save file, and should give an appropriate message like, “Sorry, no save file exists on this computer.”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the modified code

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void roll (int *);       // using pointer to catch the random variable value of two dicerolls at one time .

//these global variable with static variable win use to operate the working of code .
//static win, account because two times input given by user to add the diceroll with win number and
//account (means balance of user) .

static int account = 100;
static int win = 0;
int bet = 0;

int
main ()
{
char c;
int diceroll1, diceroll2;
cout << "Welcome to the CRAPS game. ";
cout << "Do you want to load a previous session (y/n)?";
cin >> c;
if (c == 'y')
{
// load the file from system and update win
ifstream crapfile;
string line;
crapfile.open("crap.txt", ios::in);
if (crapfile.is_open())
{
while ( getline (crapfile,line) )
{
//cout << line << '\n';
account = stoi(line);
}
crapfile.close();
}

else cout << "Sorry, no save file exists on this computer.\n";
crapfile.close();
}
else if(c != 'n')
{
cout << "Invalid input. You should enter y/n. Exiting..";
exit(1);
}
  
cout << "You have $" << account << ". Do you want to roll (y/n)?";
cin >> c;
while (1)
{
//cout << "You have $" << account << ". Do you want to roll (y/n)?";
//cin >> c;

if (c == 'y')
   {
   cout << "How much do you want to bet? ";
   cin >> bet;
   diceroll1 = rand () % 6 + 1;
   diceroll2 = rand () % 6 + 1;   // rand()%6+1 get the value between 6 to 1 and also included 6 or 1 both .
   //roll (&diceroll1);   // send the address of diceroll values .
   //roll (&diceroll2);
   // Find total value of dice by adding
   win = diceroll1 + diceroll2;

   if (win == 7)
   {
   cout << "You rolled a " << diceroll1 << " and a " << diceroll2
       << ". Your total is 7. You win! ";
   account += bet;
   cout << "You have $" << account << " ";
   }
   else if (win == 11)
   {
   cout << "You rolled a " << diceroll1 << " and a " << diceroll2
       << ". Your total is 11. You win! ";
   account += bet;
   cout << "You have $" << account << " ";

   }
   else if (win == 2 || win == 3 || win == 12)
   {
   cout << "You lose! ";
   }
   else {
   cout << "You rolled a " << diceroll1 << " and a " << diceroll2
       << ". Your total is " << win << endl;
   }

   }
   else if (c == 's')
   {
   cout << "Saving your balance in file .";
   ofstream outfile("crap.txt");
   outfile << account;
   outfile.close();
   cout << "Your balance has been saved successfully.\n";
   }
else if (c == 'n')
   {
   cout << "Thank you for playing.";
   break;
   }
   cout << "You have $" << account << ". Do you want to roll again(y/n/s)?";
cin >> c;
  
}
return 0;
}

void
roll (int *a)
{
win = win + *a;

}

Sample output of the program as below.

Welcome to the CRAPS game. Do you want to load a previous session (y/n)?y
You have $120. Do you want to roll (y/n)?y
How much do you want to bet? 23
You rolled a 2 and a 5. Your total is 7. You win! You have $143 You have $143. Do you want to roll again(y/n/s)?s
Saving your balance in file .Your balance has been saved successfully.
You have $143. Do you want to roll again(y/n/s)?y
How much do you want to bet? 10
You rolled a 4 and a 2. Your total is 6
You have $143. Do you want to roll again(y/n/s)?y
How much do you want to bet? 22
You rolled a 6 and a 2. Your total is 8
You have $143. Do you want to roll again(y/n/s)?n
Thank you for playing.

Add a comment
Know the answer?
Add Answer to:
Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() {...

    can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() { return (rand() % 6+1); } void askYoNs(){ cout<<"Do you want to roll a dice (Y/N)?:"<<endl; } void printScores(int turnTotal,int humanTotal,int compTotal){ int player; int human; if(player==human){ cout<<"Your turn total is "<<turnTotal<<endl; } else{ cout<<"computer turn total is "<<turnTotal<<endl; } cout<<"computer: "<<compTotal<<endl; cout<<"human: "<<humanTotal<<endl; cout<<endl; } int human; int changePlayer(int player){ if(player==human) return 1; return human; } int process(int& turnTotal,int roll,int curr_player,int& humanTotal,int& computerTotal){ if(roll==2...

  • This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the...

    This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the game of craps, a shooter rolls 2 dice and adds the dots on the upper most faces of the dice. 7 or 11 on the first roll wins, 2, 3, or 12 on the first roll loses, andthing else is call the point and the player rolls again The following program fragment uses 1-way if statements simulate the 1st roll of the dice. Replace...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0));...

    #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....

  • Problem 9 A single game of craps (a dice game) consists of at most two rolls...

    Problem 9 A single game of craps (a dice game) consists of at most two rolls of a pair of six sided dice. The ways to win are as follows: Win-the first roll of the pair of dice sums to either 7 or 1 (you win, game over, no second roll Win the first roll of the pair of dice does NOT sum to either 7 or 1 but the sum of the second roll is equal to the sum...

  • Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) {...

    Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: ";    cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2;    return 0; }

  • I'm trying to make a game of Craps in C++. This is how the teacher wants...

    I'm trying to make a game of Craps in C++. This is how the teacher wants the program to run. Player rolled: 6 + 4 = 10 The point is 10 Player rolled: 3 + 3 = 6 Player rolled: 6 + 3 = 9 Player rolled: 3 + 1 = 4 Player rolled: 3 + 4 = 7 You seven'd out and lost! Player rolled: 2 + 5 = 7 You won! Player rolled: 3 + 1 = 4...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • Craps

    Write a C++ game that plays Craps. Craps is a game played with a pair of dice. The shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the ‘come out roll’) is a 7 or 11, the shooter wins the game. If the opening roll results in a 2 (snake eyes), 3 or 12 (box cars), the shooter loses,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT