In: Computer Science
Program #3
● Filename: pig.py
For this assignment, you’ll implement a dice game called PIG, pitting two human players against each other. PIG is played with a standard six-sided die and proceeds as follows:
● Each player starts with zero points, and the first to 20 points wins.
● The current player chooses to roll or hold.
● If they choose roll:
○ The six-sided die is rolled
○ The value of the die is added to their round points.
○ The player goes again UNLESS...
○ ...If the value on the die is 1, then the round is over and the player has lost all the points they
accumulated in the round.
● If they choose hold:
○ The points accumulated in the round are added to the player’s overall score.
○ The round is over.
● When the round ends (either because the player rolled a 1 or they chose to hold), it becomes the other
player’s turn unless somebody has won.
● We check to see if someone won at the end of each round (which is kind of unfair, because if Player
One gets 20 points on the first round, then Player Two never gets a chance, but oh well tough for them).
Requirements:
● Prompt each player to either R (roll) or H (hold).
● If they enter anything else, continue prompting them until they enter a correct input.
● On a roll, randomly generate a value 1-6 to represent the die.
● End the round when the die roll is value 1 (round points are lost), or the player chooses Hold (round
points are added to that player’s overall score).
● End the game one either player has 20 or more points and announce the winner.
● Report everything as you go along -- what the die roll was, number of points so far, etc.
● As always with your CS5001 programs, your input/output must be friendly and informative.
Helpful hints
Python has a random module that can help you generate random numbers. You probably want to import random for this part of the assignment and call randint(...)
import random # near the top of your file random.randint(1, 6) # or something like this in your program
AMAZING points:
● Allow them to enter upper or lowercase letters. R/r for roll, and H/h for hold.
● Make your code extensible enough that we could add an arbitrary number of players with minimal
coding-pain. (Consider using a list to store the player names and another list for the number of points each player has.)
Note:- This program can handle any number of players and it can accept input as case insensitive.
Code:
import random
#To print the scores of players
def printScores(names, scores, tot_players):
print("\nPlayer:\t\tPoints:")
for i in range(0, tot_players):
print(names[i] + "\t\t" + str(scores[i]))
#main function
def main():
#getting total number of players
tot_players = int(input("Enter total players: "));
#list to store the names of players
names = []
#getting the names of all players
for i in range(0, tot_players):
names.append(input("Enter the name of player " + str(i + 1) + ":
"))
#list to store the score of all players
scores = []
#initializing scores of all players to zero
for i in range(0, tot_players):
scores.append(0)
#setting the turn of first player
turn = 0
#flag to indicate the game status. setting to false
initially
gameOver = False
#variable to store user input
choice = ""
#variable to store the die value
roll = 0
#variable to store the session points of a player
score = 0
#iterating until one player wins
while not gameOver:
#calling the function to display scores
printScores(names, scores, tot_players)
#displaying that which player's turn is this
print("\nCurrent player : " + names[turn])
print("Your current rount points: " + str(score))
#getting roll or hold input
choice = input("Roll/Hold (enter R/H): ")
#choice for rolling
if choice == "R" or choice == "r":
#getting and printing a random number between 1 and 6
roll = random.randint(1,6)
print("\nRolld the die and you got : " + str(roll))
#random number is 1
if(roll == 1):
#player lost this session score
print("You lost this round points: " + str(score))
#changing the turn to next player
turn = turn + 1
if turn == tot_players:
turn = 0
#setting session score to zero for new player
score = 0
else:
#adding random number to round score
score += roll
if scores[turn] + score >= 20:
#current player won the game as he got more than 20 points
print("\n" + names[turn] + " got " + str(scores[turn] + score) + "
points and won the game")
gameOver = True
elif choice == "H" or choice == "h":
#adding round points to total score
scores[turn] += score;
score = 0
#choice to hold. giving control to the next player
turn = turn + 1
if turn == tot_players:
turn = 0
else:
#invalid choice
print("Please enter a valid choice")
#calling the main function
if (__name__ == "__main__"):
main();
Code screenshot for better understanding:
Output: