Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So I have a game of Craps created in C++ but now we have to branch off and create a game that will continue. this

So I have a game of Craps created in C++ but now we have to branch off and create a game that will continue. this is the code I have so far.

#include #include #include #include

using namespace std;

int main() { int die1; //The first die. int die2; //The second die. int rollingDice; //When the player throws dice int rollingDice2 = 0; //When the player throws the dice if the first throw didn't give a winning or losing outcome. const int SnakeEyes = 2; //If the player throws two ones. const int BoxCars = 12; //If the player throws two sixes. const int BigRed = 7; //If the player throws a three and four

srand(35);

die1 = rand() % 6 + 1; //Simulates a six sided die die2 = rand() % 6 + 1; rollingDice = die1 + die2; //Adds the dice together.

cout << "Player rolled: " << die1 << " + " << die2 << " = " << rollingDice << endl << endl; //Shows what the player rolled on first try.

if (rollingDice == BoxCars) //If the player rolls two sixes. { cout << "Boxcars!" << endl << endl; }

if (rollingDice == BigRed) { cout << "Big Red!" << endl << endl; //If the player's roll equals 7. }

if (rollingDice == SnakeEyes)//If the player rolls two ones. { cout << "Snake Eyes!" << endl << endl; }

if (rollingDice == 7 || rollingDice == 11) //Player wins if they get a 7 or 11. { cout << "You won!" << endl; cin.get(); return 0; }

if (rollingDice == 2 || rollingDice == 3 || rollingDice == 12) //Player loses if they get a 2, 3, or 12 on first try. { cout << "Craps! You lost!" << endl; cin.get(); return 0; }

else { cout << "The Point is " << rollingDice << endl << endl; //If the player rolled a 4, 5, 6, 8, 9, or 10 on first try the game continues. }

while (rollingDice2 != rollingDice && rollingDice2 != 7) //Attempts will be made until the player either rolls the same number or a 7. {

die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; rollingDice2 = die1 + die2;

cout << "Player rolled: " << die1 << " + " << die2 << " = " << rollingDice2 << endl << endl;

if (rollingDice2 == BoxCars) { cout << "Boxcars!" << endl << endl;//If the player rolls two sixes. }

if (rollingDice2 == BigRed) { cout << "Big Red!" << endl << endl;//If the player's roll equals 7. }

if (rollingDice2 == SnakeEyes) { cout << "Snake Eyes!" << endl << endl;//If the player rolls two ones. }

if (rollingDice2 == rollingDice) { cout << "You rolled your point! You won!" << endl; // The player matched their number they won. }

if (rollingDice2 == 7) { cout << "You seven'd out and lost!" << endl; // The player got a total of seven they lost. }

}

return 0;

}

The instructions on the assignment are below.

Overview

For this assignment, implement and use the methods for a class called Die.

Die class

The Die class is used to represent a 6-sided die.

Data Members

The class contains two data members.

An integer that holds the value of the side of the die that is up

An integer symbolic constant that holds the maximum number of sides on the die. Use a value of 6.

C++ 11 allows for symbolic constants to be initialized in the class definition. However, it's not something that is supported by all compilers because class definitions are treated as patterns for what the class should contain, and therefore, they don't take up any memory. To create a symbolic constant that will work for all compilers:

static const int NUM_SIDES;

const int Die::NUM_SIDES = 6;

Note the dropping of the keyword "static" and the inclusion of the "Die::" in front of the constant name. This lets the compiler know that the constant belongs to the Die class.

In the class definition, define the symbolic constant with the keyword "static" before the definition. So:

Before main, initialize the symbolic constant with its value. So:

Now, NUM_SIDES can be used in the class methods when needed.

Constructor

This class has one constructor, a default constructor (ie. one that takes no arguments). It should simply call the roll method that is described below to initialize the value of the side of the die that is up.

Methods

void roll()

This method simulates the rolling of the die. It takes no arguments and returns nothing.

The die will be "rolled" by using the rand() function to generate a random value for the side of the die that is up. The value should be between 1 and 6. Use the symbolic constant data member to help to limit the values. The result should be assigned to the integer data member.

DO NOT call the srand() function in this method.

int getValue()

This accessor method returns the current side of the die that is facing up. It takes no arguments and returns an integer.

Part 1: Testing the Die class

Before using the Die class as part of a larger project, it should be tested to make sure that the constructor and all of the methods work. A short program has been written that will test each of the methods individually.

The test program can be downloaded from Blackboard or the course website: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240test8.cpp

The output that is produced by the test program:

The initial side is 3 Roll 1: You rolled a 3 Roll 2: You rolled a 1 Roll 3: You rolled a 1 Roll 4: You rolled a 3 Roll 5: You rolled a 5 Roll 6: You rolled a 6 Roll 7: You rolled a 4 Roll 8: You rolled a 2 Roll 9: You rolled a 5 Roll 10: You rolled a 5 

If the output, using your Die class, matches the above output, move on to part 2 of this assignment. Otherwise, fix the constructor/methods until the output does match. (Note: keep in mind that it is possible that Mac's will produce a different set of values)

Part 2: Using the Die class

This is the part of the assignment that will be submitted for grading.

For this part of the program, re-do program 4, the Craps game, using two Die objects in place of the integers that were used to represent the dice in the original version.

As a reminder, Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately. If the sum of the first roll of the dice is equal to 2, 3, or 12, the player has rolled "craps" and loses immediately. If the sum of the first roll of the dice is equal to 4, 5, 6, 8, 9, or 10, the game will continue with the sum becoming the "point." The object of the game is now for the player to continue rolling the dice until they either roll a sum that equals the point or they roll a 7. If the player "makes their point," they win. If they roll a 7, they lose.

Implementing the game

Seed the random number generator with a value of 35 for this program. Note: other seed values may be used to produce different results. However, the version that is handed in for grading MUST use a seed value of 35.

Create two Die objects to represent the dice that will be rolled to play-during the game. Create any other variables that are needed to play the game. Note: it is important to create the objects AFTER the call to srand() since the Die class relies on the random number generator.

The remaining logic should be in a loop that executes as long as the user wants to play the game, which will be indicated by a character value of 'y' or 'Y' when the user is asked if they want to play the game. (Note: you may assume that the game will be played at least one time).

Next, call the roll() method for the two Die objects. The two sides of the Die objects should be added together and then displayed along with the sum.

If the sum of the dice is equal to 7 or 11, the game is over and the player has won. Display a congratulatory message.

If the sum of the die is equal to 2, 3, or 12, the game is over and the player has lost. Display a message indicating the player has lost because they rolled craps.

For any other sum, the sum is now the point and the game should continue until the user rolls the point again or rolls a 7. To do this:

Save the sum (the point) in a variable so it can be used for a later comparison

Display the point

Create a variable and initialize it to a value of your choice to indicate that the game should continue.

In a loop that should execute as long as the game should continue:

roll the two Die objects and display the two sides along with the sum

if the sum of the die is the same as the point, display a congratulatory message indicating the player has made their point and they won the game. Also change the variable that controls the loop to indicate the game should no longer continue.

otherwise, if the sum of the die is 7, display a message that the player has lost the game and change the variable that controls the loop to indicate the game should no longer continue.

Programming Requirements

Make sure to add #include statements for cstdlib (and ctime if time(0) is used while testing the program).

Each method must have a documentation box like a function.

Hand in a copy of the source code from part 2 of the assignment using Blackboard.

Output

srand(35):

Player rolled: 1 + 2 = 3 Craps! You lost! Would you like to play again (y for yes)? y Player rolled: 4 + 5 = 9 The point is 9 Player rolled: 2 + 2 = 4 Player rolled: 5 + 3 = 8 Player rolled: 5 + 3 = 8 Player rolled: 2 + 3 = 5 Player rolled: 2 + 1 = 3 Player rolled: 5 + 5 = 10 Player rolled: 1 + 4 = 5 Player rolled: 4 + 1 = 5 Player rolled: 3 + 4 = 7 You seven'd out and lost! Would you like to play again (y for yes)? y Player rolled: 1 + 3 = 4 The point is 4 Player rolled: 5 + 6 = 11 Player rolled: 6 + 2 = 8 Player rolled: 2 + 5 = 7 You seven'd out and lost! Would you like to play again (y for yes)? y Player rolled: 5 + 5 = 10 The point is 10 Player rolled: 6 + 1 = 7 You seven'd out and lost! Would you like to play again (y for yes)? y Player rolled: 4 + 3 = 7 You won! Would you like to play again (y for yes)? y Player rolled: 2 + 2 = 4 The point is 4 Player rolled: 3 + 4 = 7 You seven'd out and lost! Would you like to play again (y for yes)? y Player rolled: 5 + 4 = 9 The point is 9 Player rolled: 4 + 6 = 10 Player rolled: 3 + 3 = 6 Player rolled: 3 + 1 = 4 Player rolled: 3 + 6 = 9 You rolled your point! You won! Would you like to play again (y for yes)? n 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago