In: Computer Science
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.
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))