Question: Using python: Craps is a popular dice game in which players bet on the outcomes of the roll of a pair of dice. Players can

Using python: Craps is a popular dice game in which players bet on the outcomes of the roll of a pair of dice. Players can wager money against each other (playing "street craps") or against a bank ("casino craps"). In this assignment you will implement a program to play craps, though without any betting.
Required Classes: This assignment is primarily about defining and using classes. You must define two simple classes: one representing a single 6-sided die and another representing a pair of dice. Yes, "dice" is the plural of "die."
A Die object contains a value, which is a number initially set to a random integer between 1 and 6. You should have a function to roll the die (set a new random value) and a getter for the value. You should also be able to print the Die, i.e., print it's current value.
The PairOfDice class should represent (not surprisingly) a pair of dice, which should contain two Die objects and the sum of their values. You should have a getter that returns a pair (tuple) of the individual die values and a getter for their sum. We won't cover tuples until week 11, but it's simple. To return a pair of values x and y, just return (x, y).
You must also have a function to roll both dice, updating the sum. Finally, you should be able to print the dice, printing the two values and the sum. For example, if object dc is a PairOfDice object containing individual dice with values 3 and 6, you should be able to do the following:
>>> print( dc )(3,6)9
I would strongly suggest testing your two classes thoroughly before you continue with the rest of the assignment. Here's what that might look like:
> python3>>> from Craps import *>>> d1= Die() # single die >>> print(d1) # initialized randomly from [1..6]5>>> d1.roll() # roll die >>> print(d1) # new value 3>>> d1.getValue() # getter for die value 3>>> dc = PairOfDice() # pair of dice >>> print(dc) # each initialize randomly (2,4)6>>> dc.rollPair() # roll dice >>> print(dc) # new values, sum (3,2)5>>> dc.getValues() # getter for values (3,2)>>> dc.getSum() # getter for sum 5>>> dc.rollPair() # roll 'em again >>> print(dc)(1,1)2 # snake eyes >>>
Make all class data private. That is, the class fields must not be accessible outside the class directly, but only through the setters and getters.
Playing Craps: The rules of craps are very simple. A player rolls a pair of dice. Add the values and keep track of this initial sum. If the initial sum is 2,3, or 12, the player loses. If the initial sum is 7 or 11, the player wins. Otherwise, the player continues to roll until the sum is 7, indicating a loss, or the sum equals the initial sum, indicating a win.
Define a function playCraps() that does the following:
define a PairOfDice object (the initial values count as the initial roll);
print it and print the initial sum separately; store the initial sum;
see if the initial sum give a win or loss and print an appropriate message;
if not, keep rolling the dice until there is a win or loss;
at that point, print the appropriate message;
finally, print a count of the number of rolls.
Make sure that your file calls playCraps(); it is your main function.
See the sample output below for the expected output, including the messages to print.
Sample Output
Please make the behavior of your class conform to these examples. Note that these examples are run from the command line, but you can also run it from the Python interactive loop. However, make sure that the TA can run your program from the command line.
> python Craps.py (6,6)12 Initial sum: 12 Sorry! You lost. Total rolls: 1> python Craps.py (5,2)7 Initial sum: 7 Congratulations! You won. Total rolls: 1> python Craps.py (4,5)9 Initial sum: 9(1,4)5(1,2)3(3,5)8(5,3)8(1,1)2(1,1)2(4,3)7 Sorry! You lost. Total rolls: 8> python Craps.py (2,2)4 Initial sum: 4(4,2)6(3,6)9(2,4)6(4,4)8(1,1)2(1,3)4 Congratulations! You won. Total rolls: 7> python Craps.py (4,4)8 Initial sum: 8(4,4)8 Congratulations! You won. Total rolls: 2> python Craps.py (5,1)6 Initial sum: 6(1,5)6 Congratulations! You won. Total rolls: 2> python Craps.py (2,2)4 Initial sum: 4(3,3)6(5,1)6(4,3)7 Sorry! You lost. Total rolls: 4>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!