Question

In: Computer Science

PYTHON GAME OF PIG The game of Pig is a simple two player dice game in...

PYTHON GAME OF PIG

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 their 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.

When it is the computer’s turn you need not do any prompting. Simply keep rolling until the computer has earned 20 or more points and then hold. If the computer rolls a 1 at any time then the turn is lost and no points are added to the score. If at any point the computer has enough points to win the game then the computer holds and the game ends. Allow the human player to roll first. A random roll can be simulated with a call to random.randint(1,6) which generates a uniform random number in [1,6]. Make sure to import the random module (ie. import random).

* **The key thing is that I want help writing this program without the use of function definitions like def print_pizza_area(): ... I would also appreciate comments describing the steps if possible.

Solutions

Expert Solution

Algorithm :

1. Start

2. Create two variables player1 and player2

3. choose the condition

4. condition retained.

5. stop.

Code :

import random
def roll():
die = random.randint(1,6)
return die
def com(p1,p2):
while True:
dice = roll()
print('Dice rolled : ' , dice)
p2 += dice
if p2 > 20:
print('Current points are {} \nChoose 1. Hold 2. Skip : 1'.format(p2))
print('Player out')
return p2
break
elif dice == 1 :
print('Player out with score : ',p2)
break
return p2
  
if __name__ == '__main__':
p1 = 0
p2 = 0
while True:
temp = roll()
p1 += temp
if temp == 1:
print('Dice rolled 1 , Player out')
break
else:
print(' Player 1 turn \n Player 1 got {} points'.format(temp))
co = int(input(' choose \n 1. Hold \n 2. Skip : '))
if co == 1:
print('Game hold, Current score is : ',p1)
print('shift player')
pl2 = com(p1,p2)
break
elif co == 2 :
continue
  
print('Player 1 score is {} \nPlayer 2 score is {}'.format(p1,pl2))


Related Solutions

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...
In a dice game a player first rolls two dice. If the two numbers are l...
In a dice game a player first rolls two dice. If the two numbers are l ≤ m then he wins if the third roll n has l≤n≤m. In words if he rolls a 5 and a 2, then he wins if the third roll is 2,3,4, or 5, while if he rolls two 4’s his only chance of winning is to roll another 4. What is the probability he wins?
Two players A and B play a dice game with a 6-face fair dice. Player A...
Two players A and B play a dice game with a 6-face fair dice. Player A is only allowed to roll the dice once. Player B is allowed to roll the dice maximally twice (that is, Player B can decide whether or not she or he would roll the dice again after seeing the value of the first roll). If the player with the larger final value of the dice wins, what is the maximal probability for Player B to...
PROGRAMMING IN C-DICE GAME Q1. A player rolls two dice at the same time. Each die...
PROGRAMMING IN C-DICE GAME Q1. A player rolls two dice at the same time. Each die has six faces, which 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. (i) If the sum is 2 or 10 on the first throw, the player wins. (ii) If the sum is 3, 7 or 12 on the first throw, the player loses. (iii)...
In order to win a game, a player must throw two fair dice and the sum...
In order to win a game, a player must throw two fair dice and the sum of the dice needs to be either 4 or less or 10 or more for the player to win. What is the probability that the sum of the dice is 4 or less? What is the probability that the sum of the dice is 10 or more? What is the probability that the player will win the game?
In the game of craps, a player (known as the shooter) rolls two fair six-sided dice....
In the game of craps, a player (known as the shooter) rolls two fair six-sided dice. The shooter immediately loses if the sum of the dice is 2, 3, or 12 and immediately wins if the sum of the dice is 7 or 11 on the first roll. If the sum is anything else (4, 5, 6, 8, 9, or 10), that number becomes the point and the shooter rolls again. The shooter now wins by rolling that same point...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player,...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player, one hidden, so that only the player who gets it knows what it is, and one face up, so that everyone can see it. There are four players: one human player (user) and three computer players. The players take turns requesting cards, trying to get as close to 21 as possible, but not going over 21. A player may pass. Once a player has...
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots...
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots add up to 7, the player wins $4; otherwise, the player loses $1. Suppose that, to entice the gullible, a casino tells players that there are lots of ways to win: (1, 6), (2, 5), and so on. A little mathematical analysis reveals that there are not enough ways to win to make the game worthwhile; however, because many people’s eyes glaze over at...
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots...
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots add up to 7, the player wins $4; otherwise, the player loses $1. Suppose that, to entice the gullible, a casino tells players that there are many ways to win: (1, 6), (2, 5), and soon. A little mathematical analysis reveals that there are not enough ways to win to make the game worthwhile; however, because many people's eyes glaze over at the first...
You make a carnival game, where the player rolls two fair dice (in a single roll)...
You make a carnival game, where the player rolls two fair dice (in a single roll) and attempts to roll doubles (meaning both dice show the same number). The player puts down a dollar to play the game. If the player loses, they lose their dollar. If the player wins, they win $3 (and do not lose their original dollar). Answer the following (5 pts total). If you are running the game, what is the expected value of how much...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT