Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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: In the class definition, define the symbolic constant with the keyword "static" before the definition. So: static const int NUM_SIDES; Before main, initialize the symbolic constant with its value. So: 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. 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 Extra Credit For up to 5 points of extra credit, add a method called draw to the Die class. The draw method should "draw" the side of the die that is currently up. For example, if 3 is the side that is up, then the draw method should "draw" something like: ----- |O | | O | | O| ----- on the screen. In main, use the draw method to display the two Die objects rather than having that value displayed in text. For reference, the sides of the die should resemble: Image courtesy of http://www.clker.com/clipart-7188.html. Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don't take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded. Extra Credit Output Player rolled: 3 ----- | | | O | | | ----- ----- |O | | | | O| ----- Craps! You lost! Would you like to play again (y for yes)? y Player rolled: 9 ----- |O O| | | |O O| ----- ----- |O O| | O | |O O| ----- The point is 9 Player rolled: 4 ----- |O | | | | O| ----- ----- |O | | | | O| ----- Player rolled: 8 ----- |O O| | O | |O O| ----- ----- |O | | O | | O| ----- Player rolled: 8 ----- |O O| | O | |O O| ----- ----- |O | | O | | O| ----- Player rolled: 5 ----- |O | | | | O| ----- ----- |O | | O | | O| ----- Player rolled: 3 ----- |O | | | | O| ----- ----- | | | O | | | ----- Player rolled: 10 ----- |O O| | O | |O O| ----- ----- |O O| | O | |O O| ----- Player rolled: 5 ----- | | | O | | | ----- ----- |O O| | | |O O| ----- Player rolled: 5 ----- |O O| | | |O O| ----- ----- | | | O | | | ----- Player rolled: 7 ----- |O | | O | | O| ----- ----- |O O| | | |O O| ----- You seven'd out and lost! Would you like to play again (y for yes)? y Player rolled: 4 ----- | | | O | | | ----- ----- |O | | O | | O| ----- The point is 4 Player rolled: 11 ----- |O O| | O | |O O| ----- ----- |O O| |O O| |O O| ----- Player rolled: 8 ----- |O O| |O O| |O O| ----- ----- |O | | | | O| ----- Player rolled: 7 ----- |O | | | | O| ----- ----- |O O| | O | |O O| ----- You seven'd out and lost! Would you like to play again (y for yes)? y Player rolled: 10 ----- |O O| | O | |O O| ----- ----- |O O| | O | |O O| ----- The point is 10 Player rolled: 7 ----- |O O| |O O| |O O| ----- ----- | | | O | | | ----- You seven'd out and lost! Would you like to play again (y for yes)? y Player rolled: 7 ----- |O O| | | |O O| ----- ----- |O | | O | | O| ----- You won! Would you like to play again (y for yes)? y Player rolled: 4 ----- |O | | | | O| ----- ----- |O | | | | O| ----- The point is 4 Player rolled: 7 ----- |O | | O | | O| ----- ----- |O O| | | |O O| ----- You seven'd out and lost! Would you like to play again (y for yes)? y Player rolled: 9 ----- |O O| | O | |O O| ----- ----- |O O| | | |O O| ----- The point is 9 Player rolled: 10 ----- |O O| | | |O O| ----- ----- |O O| |O O| |O O| ----- Player rolled: 6 ----- |O | | O | | O| ----- ----- |O | | O | | O| ----- Player rolled: 4 ----- |O | | O | | O| ----- ----- | | | O | | | ----- Player rolled: 9 ----- |O | | O | | O| ----- ----- |O O| |O O| |O O| ----- 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

Students also viewed these Databases questions