145k views
16 votes
For this program you will build a simple dice game called Pig. In this version of Pig, two players alternate turns. Players each begin the game with a score of 0. During a turn a player will roll a six-sided die one or more times, summing up the resulting rolls. At the end of the player's turn the sum for that turn is added to the player's total game score If at any time a player rolls a 1, the player's turn immediately ends and he/she earns O points for that turn (i.e. nothing is added to the player's total game score). This is called "pig". After every roll that isn't a 1, the player may choose to either end the turn, adding the sum from the current turn to his/her total game score, or roll again in an attempt to increase the sum. The first player to 50 points wins the game

Details
Open a text editor and create a new file called pig.py. In the file
Write a function called print_scores that has four parameters - these hold, in this order, the name of the first player (a string), his/her score (an int), the second player's name (a string), and score (an int).
The function will
Print the following message, using the current parameter values for the player names and scores (NOT necessarily Ziggy, 18, Elmer, and 23) -SCORES Ziggy:18 Elmer 23--
Print an empty line before this line
There is a single tab between SCORES and the name of player 1 .
There is a single tab between player 1's score and player 2's name .
Every other gap is a single space

User Econoclast
by
4.7k points

1 Answer

5 votes

Answer:

In Python:

import random

p1name = input("Player 1: "); p2name = input("Player 2: ")

p1score = 0; p2score = 0

playerTurn = 1

print(p1name+" starts the game")

roll = True

while(roll):

if playerTurn == 1:

p1 = random.randint(1,6)

print(p1name+"'s roll: "+str(p1))

if p1 == 1:

playerTurn = 2

roll = True

else:

p1score+=p1

another = input("Another Roll (y/n): ")

if another == "y" or another == "Y":

roll = True

else:

playerTurn = 2

roll = True

else:

p2 = random.randint(1,6)

print(p2name+"'s roll: "+str(p2))

if p2 == 1:

playerTurn = 1

roll = True

else:

p2score+=p2

another = input("Another Roll (y/n): ")

if another == "y" or another == "Y":

roll = True

else:

playerTurn = 1

roll = True

if p1score >= 50 or p2score >= 50:

roll = False

print(p1name+":\t"+str(p1score)+"\t"+p2name+":\t"+str(p2score))

Step-by-step explanation:

Note that the dice rolling is simulated using random number whose interval is between 1 and 6

This imports the random module

import random

This line gets the name of both players

p1name = input("Player 1: "); p2name = input("Player 2: ")

This line initializes the scores of both players to 0

p1score = 0; p2score = 0

This sets the first play to player 1

playerTurn = 1

This prints the name of player 1 to begin the game

print(p1name+" starts the game")

This boolean variable roll is set to True.

roll = True

Until roll is updated to False, the following while loop will be repeated

while(roll):

If player turn is 1

if playerTurn == 1:

This rolls the dice

p1 = random.randint(1,6)

This prints the outcome of the roll

print(p1name+"'s roll: "+str(p1))

If the outcome is 1, the turn is passed to player 2

if p1 == 1:

playerTurn = 2

roll = True

If otherwise

else:

Player 1 score is updated

p1score+=p1

This asks if player 1 will take another turn

another = input("Another Roll (y/n): ")

If yes, the player takes a turn. The turn is passed to player 2, if otherwise

if another == "y" or another == "Y":

roll = True

else:

playerTurn = 2

roll = True

If player turn is 2

else:

This rolls the dice

p2 = random.randint(1,6)

This prints the outcome of the roll

print(p2name+"'s roll: "+str(p2))

If the outcome is 1, the turn is passed to player 1

if p2 == 1:

playerTurn = 1

roll = True

If otherwise

else:

Player 2 score is updated

p2score+=p2

This asks if player 2 will take another turn

another = input("Another Roll (y/n): ")

If yes, the player takes a turn. The turn is passed to player 1, if otherwise

if another == "y" or another == "Y":

roll = True

else:

playerTurn = 1

roll = True

If either of both players has scored 50 or above

if p1score >= 50 or p2score >= 50:

roll is updated to False i.e. the loop ends

roll = False

This prints the name and scores of each player

print(p1name+":\t"+str(p1score)+"\t"+p2name+":\t"+str(p2score))

User Mancristiana
by
4.5k points