Question

Implement the following problem as a self-contained Python program (module). Please write in beginner level code!...

Implement the following problem as a self-contained Python program (module). Please write in beginner level code! Thanks!

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. After each roll:

a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn.

b) If the player rolls 2-6 then they can either roll again or hold. If the player holds, the sum of all rolls is added to his/her score and the turn passes to the other player.

Write a program that plays the game of Pig where one player is human and the other is the computer. When it is the human’s turn the program should show the score of both players and the previous roll. Allow the human to input “r” for roll again and “h” for hold. (Hint: use the input function).

Homework Answers

Answer #1

Note:

All the details of code has been given inside the code as comments. It is one of the easiest way to implement this game. Though it looks larger it is too simple. If you go throught the code 2 to three times you will get the clear view of code.

Code

import random
#True defines current player's' turn
player = True
computer=False
pv=0
cv=0
while(True): #infinite loop till any of one wins
        
        if(player): #player turn
                print()
                dv=random.randint(1,6) # roll the dice
                if(dv==1): # player rolled.1,hence becomes computer turn
                        print("player rolled 1")
                        player=False
                        computer=True
                else: #player rolled between 2-6
                        print("player rolled ",dv)
                        print("enter choice r or h :")
                        ch=input() #get input r or h
                        if(ch=='r'): # roll again
                                print("player chose",ch)
                                player=True
                                compuyer=False
                        elif(ch=='h'): # hold the value and pass the turn to computer
                                print("player chose",ch)
                                player=False
                                computer=True
                                pv+=dv
                                print("player score is ",pv)
                                if(pv>100): #player wins
                                        print("player won")
                                        break #exit the loop
        elif(computer): #computer turn
                print()
                dv=random.randint(1,6) # roll the dice
                if(dv==1): # computer rolled 1,becomes player turn
                        print("computer rolled 1")
                        player=True
                        computer=False
                else: # computer rolled between 2-6
                        print("computer rolled ",dv)
                        print("enter choice r or h :")
                        ch=random.choice(['r','h'])
                        print("computer chose",ch)
                        if(ch=='r'): # roll again
                                player=False
                                compuyer=True
                        elif(ch=='h'): #hold the value and pass to the player
                                player=True
                                computer=False
                                cv+=dv
                                print("computer score is",cv)
                                if(cv>=100): #computer wins 
                                        print("computer won")
                                        break #exit the loop
                

Terminal work

.

Know the answer?
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
Similar Questions
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py...
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py that has the following functions in the following order: in python Write a function roll that takes an int n and returns a random number between 1 and n inclusive. Note: This function represents a n sided die so it should produce pseudo random numbers. You can accomplish this using the random module built into python.         (3 pts) Write a function scoreRound that...
One of the most popular games of chance is a dice game known as “craps”, played...
One of the most popular games of chance is a dice game known as “craps”, played in casinos around the world. Here are the rules of the game: A player rolls two six-sided die, which means he can roll a 1, 2, 3, 4, 5 or 6 on either die. After the dice come to rest they are added together and their sum determines the outcome. If the sum is 7 or 11 on the first roll, the player wins....
Bunco is a group dice game that requires no skill. The objective of the game is...
Bunco is a group dice game that requires no skill. The objective of the game is to accumulate points by rolling certain combinations. The game is played with three dice, but we will consider a simpler version involving only two dice. How do you play two dice Bunco? There are six rounds, one for each of the possible outcomes in a die, namely the numbers one through six. Going clockwise, players take turns rolling two dice trying to score points....
Write a Python program to manage league matches. Different teams can participate in a league. A...
Write a Python program to manage league matches. Different teams can participate in a league. A player belongs to a team and a team can play many games in a league. For each team the league wants to track the name of the team and the number of players in each team. The league also wants to track the number of games the team has played, and the total numbers of points scored across all games played. During each game...
Use python to write the code You’re going to program a simulation of the following game....
Use python to write the code You’re going to program a simulation of the following game. Like many probability games, this one involves an infinite supply of ping-pong balls. No, this game is "not quite beer pong." The balls are numbered 1 through N. There is also a group of N cups, labeled 1 through N, each of which can hold an unlimited number of ping-pong balls (all numbered 1 through N). The game is played in rounds. A round...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...