Question

In: Computer Science

Program #3 ● Filename: pig.py For this assignment, you’ll implement a dice game called PIG, pitting...

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

Solutions

Expert Solution

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:


Related Solutions

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...
The game of Pig is a simple two-player dice game in which the first player to...
The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. How to play: Players take turns rolling one six-sided dice and following these rules: If the player rolls 2 through 6, then he/she can either a. “Roll Again” or b. “Hold” At this point, the sum of all rolls is added to the player’s score, and it becomes the other player’s turn. If the player rolls 1 before...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
In Java.This program will translate a word into pig-latin. Pig-latin is a language game in which...
In Java.This program will translate a word into pig-latin. Pig-latin is a language game in which words in English are altered, usually by removing letters from the beginning of a word and arranging them into a suffix. The rules we will use for the pig-latin in this program are as follows: If a word starts with a consonant, split the word at the first instance of a vowel, moving the beginning consonants to the end of the word, following a...
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for...
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for this game are special oriental dice. Each die has six different patterns. The six possible patterns are: Fish, Shrimp, Crab, Chicken, goldenCoin, Barrel. This game has three dice. In this game, the player can place his or her bet on any die-pattern. The amount of the winning prize is directly proportional to the number of matched dice after the two dice have been tossed...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program which can take the input of a decimal number and a numerical base, and convert the decimal number to that base. For example, if given the decimal number seven and the base two, your program should output it as 111, which is how seven is represented in binary. Another example, 8,943 in base 10, is 13,236 in base 9. You’ll need to perform these...
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...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living neighbors, it...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living neighbors, it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT