A popular game of chance frequently seen at casinos is a dice game known as "craps". The game is played as follows. A player rolls two dice. Each die is the familiar six-sided die with faces containing 1, 2, 3, 4, 5, and 6 dots. After the dice are rolled, the next step is determined by the sum of the dots on the two top faces.
If the sum is 7 or 11 on the first throw, the player wins.
If the sum is 2, 3, or 12 on the first throw, the player loses (called "craps").
If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then that sum becomes the player's "point". Now, the only way for the player to win is by continuing to roll the dice until she makes her point. If the player rolls a 7 before making her point, she loses. In other words, if the roll of the dice adds up to her point, she wins; if it adds up to 7, she loses; if it adds up to neither, she rolls the dice again.
Note that there are two ways for a player to win: Either by rolling a 7 or 11 in the first roll of the dice, or by rolling her point in a subsequent roll of the dice.
You are asked to write a program to play craps to allow wagering. The player starts with an initial bank balance of $1000. Each game starts with a wager (which of course must be no bigger than the current bank balance). After one game, the bank balance is updated and the player is allowed to play again, repeatedly, until she quits, or the bank balance falls to $0. You will accomplish all this by implementing the following functions.
1. Write a function called roll dice with no parameters. The function rolls two dice and return the sum of the result. You can simulate the roll of a die by using the randint(a, b) function from the random module. randint(a, b) returns a random integer N such that a <= N <= b.
2. The second function called play one game plays a single game of craps, using the rules described above. This function does not have parameters. It should print out sentences informing the player about the sequence of actions taking place in the game (see sample runs below - your code should mimic the output shown). It returns an integer value of 1 if the player wins the game, and in integer value of 0 if the player loses the game.
3. The third function called craps does not have any parameters. It uses a while loop to allow the player to play as many rounds of craps as she wants, as long as the bank balance is not $0. The function should first prompt the user to enter a wager. If the wager is greater than the bank balance, repeatedly prompt the player to re-enter the wager until a valid wager is entered. Then run one game of craps (using the play one game function), inform the player if she won or lost and update the bank balance accordingly. If the new balance is $0, print a message ("Sorry, you're broke!"), and quit. As long as the balance is greater than $0, the player may repeatedly choose to play again. If the player quits with a bank balance greater than $0, print a congratulatory message if money was made and a sympathetic message if money was lost.
A sample run is shown below.
---------------------------- Welcome to the Craps program ----------------------------
Your initial bank balance is $1000.
What is your wager? 100 Okay, let’s play.
You rolled 11 You win!!
Your new bank balance is $1100
Do you want to play again? [y/n] y
What is your wager? 500 Okay, let’s play.
You rolled 12 Sorry, you lose!
Your new bank balance is $600
Do you want to play again? [y/n] y
What is your wager? 700 Cannot wager more than $600. Re-enter wager: 800 Cannot wager more than $600. Re-enter wager: 400 Okay, let’s play. You rolled 4 Your point is 4
You rolled 5 You rolled 9 You rolled 10 You rolled 9 You rolled 3 You rolled 7 Sorry, you lose!
Your new bank balance is $200
Do you want to play again? [y/n] y
What is your wager? 500 Cannot wager more than $200. Re-enter wager: 200 Okay, let’s play.
You rolled 10 Your point is 10
You rolled 6 You rolled 11 You rolled 6 You rolled 10 You win!!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.