In: Computer Science
Using Python,
Assignment
Write a program that plays a game of craps. The program should
allow the player
to make a wager before each “turn”. Before each turn, allow the
user to either
place a bet or exit the game. After each turn display the player’s
current balance.
Specifics
Each player will start with $500.00. Initially, and after each turn
give the user the
option of betting or leaving the program. Implement this any way
you wish, but
make it clear to the user what their options are. If the user
chooses to place a
bet, ask for the amount that will be wagered and start that “turn,”
or bet. Each
turn will consist of one or more rolls of the dice. For each roll,
the program should
display the result of both die and the total value of the roll.
Then indicate the
outcome from that roll (win/lose/continue rolling). Once a turn, or
bet is finished,
indicate the outcome of the bet and the updated balance. When the
user
chooses to exit the program display their final balance. The use of
vertical
whitespace in the output may be very useful in making the output
readable.
Rules for craps
Total value of dice after first roll:
7 or 11 – player wins
2, 3, or 12 – player loses
Any other value and the player rolls again – the total of the dice
is now their
“point”
Total value of dice following the initial roll:
The players “point” – player wins
7 – player loses
Any other value and the player rolls again until rolling a 7 or
their point
Requirements
Extra credit will be considered for text based displays that show
the face of each
die rather than just the value of the dice. Deductions will be
applied for poor or no
use of subroutines; there should be ample opportunities for
subroutines in this
program.
At the very least you should have a subroutine which returns a
random number between 1 and 6.
Thanks for the question, here is the complete program in python. I have modularised the code as much as possible and also written all kind of input validation.
Here is the complete code with screenshot for code indentation
============================================================================
import random
# ask user if he wants to play or leave
def play_again():
while True:
play = input('Do
you want to play (y for yes n for no): ').lower()
if play
== 'y':
return True
elif play ==
'n':
return False
else:
print('Invalid choice. Try again.')
# ask user for a bet amount, do validation and returns a valid
bet amount
def get_bet_amount(balance):
while True:
try:
bet = float(input('Enter your bet amount:
<={}'.format(balance)))
if bet <= 0 or bet >
balance:
print('Invalid number. Bet amount must be within $1 -
${}'.format(balance))
else:
return bet
except:
print('Invalid character entered. Try
again.')
# roll two dices return the value of each dice and its
total
def roll_dice():
dice_one = random.randint(1, 6)
dice_two = random.randint(1, 6)
return dice_one, dice_two,
dice_one + dice_two
# when user rolls for the first time
def
first_roll(bet_amount,dice_one,dice_two,total):
print('First Roll')
print('Dice1: {} Dice: {} Total:
{}'.format(dice_one, dice_two, total))
if total == 7
or total == 11:
print('Congratulations. You won the game in your first
roll')
print('You are
awarded:
${}\n'.format(bet_amount))
return
bet_amount
elif total == 2
or total == 3 or total ==
12:
print('Sorry.
You lost the game in your first roll')
print('Amount
deducted:
${}\n'.format(bet_amount))
return
-bet_amount
else:
return
0
# this function plays for the players point
def play_point(bet_amount,player_point):
while True:
print('You must
roll {} now to win the game.'.format(player_point))
input('Hit enter
to roll...')
dice_one, dice_two,
total = roll_dice()
print('You
rolled')
print('Dice1: {}
Dice: {} Total: {}'.format(dice_one, dice_two,
total))
if
total == player_point:
print('Congratulations, You rolled your
point!')
print('You are awarded:
${}\n'.format(bet_amount))
return bet_amount
break
elif total ==
7:
print('Sorry! You lost, you rolled 7')
print('Amount deducted:
${}\n'.format(bet_amount))
return -bet_amount
break
else:
print('Roll again..')
def main():
balance = 500 # initial balance
while True:
play =
play_again()
if
play:
print('Lets Play. Your balance is:
${}'.format(balance))
bet_amount = get_bet_amount(balance)
input('Hit enter to roll...')
dice_one, dice_two, total = roll_dice()
first_roll_amount =
first_roll(bet_amount,dice_one,dice_two,total)
if first_roll_amount != 0:
balance +=
first_roll_amount
else:
point_amount=play_point(bet_amount,total)
balance+=point_amount
else:
print('Thanks for playing.')
break
print('You take away
${}'.format(balance))
main()