Write a C++ program that simulates a game of Craps , which is played with two di
ID: 3736691 • Letter: W
Question
Write a C++ program that simulates a game of Craps, which is played with two dice. Please split into 3 files: Main.cpp, Craps.cpp, and Craps.h.
Create a class called Die. The class should have one private member called value, (we don't need the member “sides” - our die will have 6 sides), a constructor, one mutator function (roll to set the value) and one accessor function to return the value. You will use two different die objects to simulate rolling two dice.
On the first roll, the player wins if the sum of the dice is 6 or 7 and they loose if the sum is 2 or 12. Any other sum that is rolled is called the point. On each subsequent roll: the player wins if they roll that sum or point again, and the player loses by rolling either a 6 or 7. Any other roll is ignored and the game continues.
Once the player wins or loses, your program is to ask the player if they wish to play again. If they enter Y or y: play the game again. If they enter N or n, the game ends and your program will print the number of wins and the number of loses. If the user enters any other value, your program is to prompt them again. The game needs to pause after every roll and wait for the user to hit enter to simulate a real game where the player waits while the dice are rolled.
Explanation / Answer
******craps.cpp*********
#include"Craps.h"
#include <stdlib.h>
#include<stdio.h>
#include <time.h>
Craps::Craps()
{
point = -1;
wins = 0;
loses = 0;
};
int Craps::handleRoll(int roll)
{
if(roll == 6 || roll == 7)
{
wins++;
cout << " Player Wins ";
if(askUser() == 1)
{
point = -1;
return 1;
}
}
else if(roll == 2 || roll == 12)
{
loses++;
cout << " Player Loses ";
if(askUser() == 1)
{
point = -1;
return 1;
}
}
else
{
if(roll == point)
{
wins++;
cout << " Player Wins ";
if(askUser() == 1)
{
point = -1;
return 1;
}
}
point = roll;
return 1;
}
cout << " You won : " << wins << " You Loses : " << loses ;
return 0;
};
int Craps::askUser()
{
cout << "Do you want to play again ? Y/N : ";
char in = '-';
while(!(in == 'n' || in == 'N' || in == 'Y' || in == 'y'))
{
cin >> in;
if(in == 'y' || in == 'Y' ) return 1;
if(in == 'n' || in == 'N') return 0;
cout << "Invalid Input Enter Again : ";
}
return 0;
};
Die::Die()
{
value = -1;
};
void Die::roll()
{
char ch;
scanf("%c",&ch);
int roll1 = rand() % 6 + 1;
cout << roll1 ;
value = roll1;
};
int Die::getvalue()
{
return value;
};
*****craps.h ***********
#ifndef CRAPS_H
#define CRAPS_H
#include<iostream>
#include<vector>
using namespace std;
class Die
{
private :
int value;
public :
Die();
void roll();
int getvalue();
};
class Craps
{
private:
int point;
int wins;
int loses;
public:
Craps();
int handleRoll(int roll);
int askUser();
};
#endif
********main.cpp**********
#include<iostream>
#include "Craps.h"
using namespace std;
int main()
{
Craps game;
Die d1;
Die d2;
int crapHandle = 0;
cout << "Starting game .... ";
do
{
cout << "Press Enter to Roll.. ";
d1.roll();
d2.roll();
int roll = d1.getvalue();
roll += d2.getvalue();
cout << " turn score is " << roll << " ";
crapHandle = game.handleRoll(roll);
}
while(crapHandle == 1);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.