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 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 and 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, ie 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 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 and you should be able to do the following:
print dc
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:
python from Craps import d Die # single die printd # initialized randomly from droll # roll die printd # new value dgetValue # getter for die value dc PairOfDice # pair of dice printdc # each initialize randomly dcrollPair # roll dice printdc # new values, sum dcgetValues # getter for values dcgetSum # getter for sum dcrollPair # roll em again printdc # 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 or the player loses. If the initial sum is or the player wins. Otherwise, the player continues to roll until the sum is 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 Initial sum: Sorry! You lost. Total rolls: python Craps.py Initial sum: Congratulations! You won. Total rolls: python Craps.py Initial sum: Sorry! You lost. Total rolls: python Craps.py Initial sum: Congratulations! You won. Total rolls: python Craps.py Initial sum: Congratulations! You won. Total rolls: python Craps.py Initial sum: Congratulations! You won. Total rolls: python Craps.py Initial sum: Sorry! You lost. Total rolls:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
