Question

In: Computer Science

Write the functions necessary to simulate a gambling game with the following rules: The winner is...

Write the functions necessary to simulate a gambling game with the following rules:

The winner is the player who has a total closest to 9 without going over.

1. The player "rolls" 2 dice (6 sided dice)

2. The computer "rolls" 2 dice (6 sided dice)

3. The user is asked whether they want to roll one more time. If they indicate they do, another dice is rolled and added to the player's total.

4. A message is printed indicating the winner. In the case of a tie, the player wins. If both the computer and player go over 9, the computer wins.

5. YOU MAY ASSUME THAT THE USER ENTERS A VALID INTEGER (1 or 2) AS THEIR CHOICE.

You may NOT change the main function except to insert the if statement and print statements as indicated.

# define functions
def rollDice():
# function returns a random number between 1 and 6


def userWon(t1, t2):
# function accepts player total and computer total
# function returns true if player wins
# function returns false if computer wins


def main():
# each player rolls two Dice
player = rollDice() + rollDice()
computer = rollDice() + rollDice()
# ask the player if they want to roll again
again = int(input("Please enter 1 to roll again. Enter 2 to hold."))
# roll again if needed and add to player's total
if again == 1:
player = player + rollDice()

# insert your if statement here to determine the winner
# and your appropriate print statements

main()

Solutions

Expert Solution

Here is the solution,

# your code starts here
"""
Rules of the Game (as per the question)
1) Both player(human) and Computer roll the dice twice.
2) Player(human) may roll the dice one more time
3) take total of computer roll dice values
4) take total of player(human) roll dice values
5) who evers total is closet to 9, wins
6) who ever goes over 9, will lose.
7) if it is a tie, then player wins
8) if both the player(human) and the computer goes over 9, computer wins
"""

import random
def rollDice():
return random.randint(1,6)


def userWon(player,computer):
# if it is a tie...player(human) wins
if player == computer:
print("Both player and computer have same values...")
return True
# if both the computer and the user goes over 9, computer wins
elif player > 9 and computer > 9:
print("Both player and computer goes over 9, Computer:",computer," , Player:", player)
return False
# if player goes over 9 and computer is below 9, then computer wins
elif player > 9 and computer < 9:
print("Computer :",computer,","," Player :",player)
return False
# if computer goes over 9 and player is below 9, then player wins
elif player < 9 and computer > 9:
print("Computer :",computer,","," Player :",player)
return True
# if player and computer are below 9, then check for closet to 9
elif player < 9 and computer < 9:
# if player's total is closet compared to computer then player wins
if(9-player) < (9-computer):
print("Computer :",computer,","," Player :",player)
return True
# else computer wins
else:
print("Computer :",computer,","," Player :",player)
return False

def main():
print()
print()
#each player rolls two Dice
player = rollDice() + rollDice()
computer = rollDice() + rollDice()
print("Player(human) and computer have rolled the dice twice.")
# ask the player if they want to roll again
again = int(input("Please enter 1 to roll again. Enter 2 to hold: "))
if again == 1:
player = player + rollDice()
# check who has won
if userWon(player, computer):
print("player(human) Won")
else:
print("Computer Won")

  
# function call
main()
# code ends here   
here is the sample output:-

here is the screenshot of the code for better understanding of indentation:-

Thank You.


Related Solutions

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...
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...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
Write a MATLAB program to simulate the Stuck in the Mud game, The following link contains...
Write a MATLAB program to simulate the Stuck in the Mud game, The following link contains the detail game description: https://www.activityvillage.co.uk/stuck-in-the-mud , with additional features that can: • Use five (5) 6-sided dice to automatically play the Stuck in the Mud game against a player. • Greet the player when the game starts. • Let the player to choose the number of rounds to play. Take care of the user input to ensure the program will not crash with inputs...
Write a mini essay on the following: (a) “Culture defines the rules of the game.” (Robbins...
Write a mini essay on the following: (a) “Culture defines the rules of the game.” (Robbins and Judge, 2018, pp 298). Examine the extent in which culture in an organisation impacts (i) employee engagement and (ii) diversity and inclusion strategies. (b) Discuss how an organisation uses socialisation processes/tactics to socialise new employees and help them through the adjustment process.
Write a Java class that determines the winner of a rock, paper scissors game. Assume the...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the input from the user is always valid (so no need to check), that is it contains either one of `R`, `P`, or `S` as a single character, or has matching parenthesis, like, `(S&P)` or `((R&P)&S)`, and the `&` character. So for example, the user inputs `(P&R)` and the program will output `P` since paper beats rock. Or if the user inputs `((S&R)&(S&S))` the output...
Python: Write a program that keeps track of a game score and declares the winner when...
Python: Write a program that keeps track of a game score and declares the winner when the game is over. A game is over when either one player scores 10 or more points with at least 2 more points than the opponent. A game is also over when one player scores 7 points and the opponent scores none. The program should begin by asking the names of the two players. Then, it should keep asking who won the point till...
Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both...
Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both player and dealer are taking cards off the same deck (the same 52 values). 2) display BOTH cards of the players, and ONE card of the computer. 3) Check for blackjack (starting value of 21 for computer or player) -if either side has a blackjack end of the game, next hand of blackjack 4) Computer must hit if 16 or below. 5) Computer must...
What are the necessary functions of financial markets? Explain the essence of bank capital rules.
What are the necessary functions of financial markets? Explain the essence of bank capital rules.
Write a modular program to simulate the Game of Life and investigate the patterns produced by...
Write a modular program to simulate the Game of Life and investigate the patterns produced by various initial configurations. Some configurations die off rather rapidly; others repeat after a certain number of generations; others change shape and size and may move across the array; and still others may produce ‘gliders’ that detach themselves from the society and sail off into space! Since the game requires an array of cells that continually expands/shrinks, you would want to use multi-dimensional arrays and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT