Question

python code( using functions please)! Rules of the game: - This game requires a dice. The...

python code( using functions please)!

Rules of the game: - This game requires a dice. The goal of the game is to collect the correct number of coins to create a dollar. Players take coins based on a roll of the dice. The numbers on the dice correlate to the coin values as follows:  1—penny  2—nickel  3—dime  4—quarter  5— ( pick a random card from 1 to 4): o 1 = penny o 2 = nickel o 3 = dime o 4 = quarter  6—lose a turn - Players take turns rolling the dice and adding coins. - The winner becomes the first player to reach exactly one dollar. - If taking a coin would put the player over a dollar, the player loses the turn.

requirements: 1- Need to have 2 players 2- Every turn the dice will be rolled randomly (value will vary between 1 – 6). 3- Depending on the dice value, you will update the total number of coins each player has. 4- Will keep rolling the dice and adding the coins, until one of the players wins. (Reach exactly $1). 5- By adding the coins each turn, if the total number of coins exceeds $1, then skip that turn and do not increase the coins total. Automatically it will be the other player’s turn. 6- Need to display the total coins for each player after each round. 7- After announcing the winner, ask players if they want to play again. (Yes/No). 8- Need to validate all user inputs.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Screen Shot:

Sample Output:

Program Code to Copy:

# 1 dollar considered as 100 cents to avoid floating point errors
import random

# returns one random integer 1 to 6 inclusive
def roll_dice():
   return random.randrange(1,7)

# converts and returns coin value in cents
def get_value(coin):
   value = [1, 5, 10, 25] # [penny,nickel,dime,quarter] in cents
   if coin == 6: # player loses turn
       return 0
   if coin == 5: # player picks a random card 1 to 4
       coin = random.randrange(1,5) # random 1 to 4
   return value[coin-1] # -1 because lists start at index 0

# exchanges player 0 to 1 or 1 to 0 where 0 means player 1 and 1 means player 2
def change_player(player):
   if player == 0:
       return 1 # player 2
   return 0 # player 1

# the engine that controls the entire game
def play_game():
player = 0 # start with player 1 by default for the first round
coin_value = [0,0] # initially 0 for both players
play_again = True

while play_again:
print('Player 1 total = ',coin_value[0],'cents, player 2 total = ',coin_value[1],'cents')
print('Player', player+1, 'enter P to roll the dice: ',end=' ')
play = input('').upper() == 'P'
if play: # user has entered P
coin = roll_dice() # get coin
print('\ndice shows: ',coin)
value = get_value(coin) # get value of coin in cents
if value == 0: # dice showed 6
print('Player', player+1, 'loses the turn!')
elif (coin_value[player] + value) > 100: # excess
print('Value exceeds $1, Player', player+1, 'loses the turn!')   
elif (coin_value[player] + value) <= 100: # correct roll
coin_value[player] = coin_value[player] + value # increase the coins
  
# ending the game or going for another round
if coin_value[player] == 100: # exactly $1
print('Congratulations!, player',player+1, 'has won the game.')
coin_value = [0,0] # reset the scores
play_again = input('Play Again? (Yes/No): ').upper() == 'YES' # players decide

# next turn or round starts with a different player
player = change_player(player) # next turn

# invoke the engine function start the game
play_game()


------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~

Add a comment
Know the answer?
Add Answer to:
python code( using functions please)! Rules of the game: - This game requires a dice. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • USING RECURSIONN!!! JAVA CODE Coin game: Alice and Bob are playing a game using a bunch...

    USING RECURSIONN!!! JAVA CODE Coin game: Alice and Bob are playing a game using a bunch of coins. The players pick several coins out of the bunch in turn. Each time a player is allowed to pick 1, 2 or 4 coins, and the player that gets the last coin is the winner. Assume that both players are very smart and he/she will try his/her best to work out a strategy to win the game. For example, if there are...

  • Create a Dice Game: PYTHON This game is between two people. They will roll a pair...

    Create a Dice Game: PYTHON This game is between two people. They will roll a pair of dice each 10 times. Each roll will be added to a total, and after 10 rolls the player with the highest total will be the Winner! You will use the RANDOM class RANDRANGE to create a random number from 1 to 6. You'll need a function for "rollDice" to roll a pair of dice and return a total of the dice. You must...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players...

    The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 (”pig”) is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player’s turn, the player is faced with two decisions: roll If the player rolls a 1: the player scores nothing and it becomes the...

  • The game of Pig is a simple two-player dice game in which the first player to...

    The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die: If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn. If the player rolls 2 through 6, then he or she can either ROLL AGAIN or HOLD:      At this point, the sum of all rolls...

  • C or C++ Project Overview Wild, Wild, West! Dice Game The Wild, Wild, West! is an...

    C or C++ Project Overview Wild, Wild, West! Dice Game The Wild, Wild, West! is an elimination dice game. It can be played by any number of players, but it is ideal to have three or more players. Wild, Wild, West! Requires the use of two dice. At the start of the game, each player receives a paper that will track the number of lives that player has. Each player starts the game with 6 lives. In the first round,...

  • A subtraction game Subtraction games are two-player games in which there is a pile of objects,...

    A subtraction game Subtraction games are two-player games in which there is a pile of objects, say coins. There are two players, Alice and Bob, who alternate turns subtracting 4.9. A SUBTRACTION GAME 19 from the pile some number of coins belonging to a set S (the subtraction set). Alice goes first. The first player who is unable to make a legal move loses. For example, suppose the initial pile contains 5 coins, and each player can, on his turn,...

  • Two player Dice game In C++ The game of ancient game of horse, not the basketball...

    Two player Dice game In C++ The game of ancient game of horse, not the basketball version, is a two player game in which the first player to reach a score of 100 wins. Players take alternating turns. On each player’s turn he/she rolls a six-sided dice. After each roll: a. If the player rolls a 3-6 then he/she can either Roll again or Hold. If the player holds then the player gets all of the points summed up during...

  • 16-17 A vending machine is programmed to count out the correct change for each transaction. Formulate...

    16-17 A vending machine is programmed to count out the correct change for each transaction. Formulate and solve an IP model that will determine how change is to be made for a purchase of $4.43, when a $10 bill should be based on the availability of coins in the machine, with the objective of minimizing the total number of coins used to make the change. DENOMINATION AVAILABILITY $1 coin Quarter ($0.25) Dime ($0.10) Nickel ($0.05) Penny ($0.01) Solve Problem 16-17...

  • Assignment 2 – The two player game of Poaka The game of Poaka is a dice...

    Assignment 2 – The two player game of Poaka The game of Poaka is a dice game played by two players. Each player takes turns at rolling the dice. At each turn, the player repeatedly rolls a dice until either the player rolls a 1 (in which case the player scores 0 for their turn) or the player chooses to end their turn (in which case the player scores the total of all their dice rolls). At any time during...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT