Question

In: Computer Science

Using Python, Assignment Write a program that plays a game of craps. The program should allow...

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.

Solutions

Expert Solution

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()


Related Solutions

For this assignment, write a program that uses functions to simulate a game of Craps. Craps...
For this assignment, write a program that uses functions to simulate a game of Craps. 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 (known as the come-out roll) is equal to 7 or 11, the player...
Overview For this assignment, write a program that uses functions to simulate a game of Craps....
Overview For this assignment, write a program that uses functions to simulate a game of Craps. 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 (known as the come-out roll) is equal to 7 or 11, the...
Write a program in Python to simulate a Craps game: 1. When you bet on the...
Write a program in Python to simulate a Craps game: 1. When you bet on the Pass Line, you win (double your money) if the FIRST roll (a pair of dice) is a 7 or 11, you lose if it is ”craps” (2, 3, or 12). Otherwise, if it is x ∈ {4, 5, 6, 8, 9, 10}, then your point x is established, and you win when that number is rolled again before ’7’ comes up. The game is...
Write a C++ program to play the dice game "Craps". Craps is one of the most...
Write a C++ program to play the dice game "Craps". Craps is one of the most popular games of chance and is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins,...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out the state of the board, asks the user where they would like to place their mark, and implements this decision. The program then places its own mark on a randomly chosen available position. Once one of the player won, the program declares the result and asks if the user would like to continue. The first player is selected at random.
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
Write a program that plays an addition game with the user (imagine the user is a...
Write a program that plays an addition game with the user (imagine the user is a 5th grade student). First ask the user how many addition problems they want to attempt. Next, generate two random integers in the range between 10 and 50 inclusive. You must use the Math.random( ) method call.   Prompt the user to enter the sum of these two integers. The program then reports "Correct" or "Incorrect" depending upon the user's "answer".  If the answer was incorrect, show...
Task: Craps is a popular game played in casinos. Design a program using Raptor Flowcharts to...
Task: Craps is a popular game played in casinos. Design a program using Raptor Flowcharts to play a variation of the game, as follows: Roll two dice. Each dice has six faces representing values 1, 2, 3, 4, 5, and 6, respectively. Check the sum of the two dice. If the sum is 2, 3, or 12(called craps), you loose; if the sum is 7 or 11(called natural), you win; if the sum is another value(i.e., 4, 5, 6, 8,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT