Python Forum
Two Dice Pig Game in Python 3.6 Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Two Dice Pig Game in Python 3.6 Help
#1
Here are the instructions...


Write a Python program to simulate the Two Dice Pig game. Two Dice Pig is played by 2 players who roll 2 standard dice to earn points.
The object of the game is to be the first player to score 100 or more points.
For our game, player 1 will be a human player, and the 2nd player will be the computer. Each player's roll will be randomly generated by the program. The human player (player 1) gets to choose whether they want to roll again or not after each roll time they roll. When they choose
not to roll again, it is called a "hold". The computer (player 2) doesn't get that choice and automatically rolls again every time until a 1 is rolled by the computer.
Here are the detailed game rules: For each player's turn, a player repeatedly rolls two standard dice until a 1 is rolled, or in the case
of the human player (player 1) that player decides to hold. After each roll of the two dice, the roll determines what happens next based on the following rules:

If neither die shows a 1, the sum of the two dice is added to the player's total. The computer automatically rolls again. The human player (player 1) rolls again if they decide they don't want to hold.

If a single 1 is rolled, the player scores nothing for this roll and the turn ends.

If two 1s are rolled, the player’s entire score is lost, and the turn ends.
Hint:
You will want to have a turn variable that will keep up with if it is player 1's turn or player
2's turn. For example, turn should equal 1 when it is player 1's turn and then change to 2 when it's player 2's turn and vice versa.





I have wrote some code and it isn't correct.
It plays the first round, but then when you roll again, it generates the same random numbers. I am not sure how to fix this.
I am also not sure how to keep track of the total score after every round.

I also haven't been able to figure out how to deal with the 1's being rolled and losing the scores when they are rolled.


Here is my code as of now...


import random
def main():
    
    
    print("Welcome to the Two Dice Pig Game. You are Player 1!")
    
    
    p1dice = random.randrange(1,7)
    p1dice2 = random.randrange(1,7)
    p2dice = random.randrange(1,7)
    p2dice2 = random.randrange(1,7)
    Player1 = p1dice+p1dice2
    Player2 = p2dice+p2dice2
    
    while(Player1<100 or Player2<100):
        
        
        print("Player 1 dice 1 =",p1dice)
        print("Player 1 dice 2 =",p1dice2)
        print("Player 1 dice total =",Player1)
        print("Does player 1 want to hold?")
        choose1 = input("Enter y for yes or n for no.")
        if(choose1=="n"):
            print("Player 1 dice 1 =",p1dice)
            print("Player 1 dice 2 =",p1dice2)
            print("Player 1 dice total =",Player1)
            print("Does player 1 want to hold?")
            choose1 = input("Enter y for yes or n for no.")
        elif(choose1=="y"):
            
            print("It's player 2's turn.")
            print("Player 2 dice 2 =",p2dice)
            print("Player 2 dice 2 =",p2dice2)
            print("Player 2 dice total =",Player2)
            print("Does player 2 want to hold?")
            choose2 = input("Enter y for yes or n for no.")
        
    
    
    
    
main()
Reply
#2
Everything inside your while loop is a single round. Everything before the while loop will only happen once. If you want to roll dice in each round, you need to roll dice inside the while loop.
Reply
#3
(Oct-10-2018, 05:03 AM)nilamo Wrote: Everything inside your while loop is a single round. Everything before the while loop will only happen once. If you want to roll dice in each round, you need to roll dice inside the while loop.

I tried removing everything above the while loop, but it tells me the variables are referenced before assignment.
How would you begin this program?
Reply
#4
The error is there to help. If it's referenced before you define it, then you can either define it before you use it, or you can stop using it.

In this case, it's because you're setting Player1 = p1dice+p1dice2. Since you haven't rolled any dice yet, that doesn't make sense. Both player's starting scores should probably be zero.
Reply
#5
You really need two while loops. The first/outer while loop is for the game, and continues until one player gets over 100. Note that your current while loop doesn't do that. Your loop will keep playing until both players get over 100.

The second/inner loop is for the player's turn. That keeps going until they say "stop" or roll a 1. You want to roll the dice at the top of that loop, then check for the 1 and either break or add it to a variable keeping track of the turn score, then ask if they want to keep going. At the end of the loop update that player's total turn score.

How you handle the two players is up to you. You could have a copy of the second loop that handles the second player, or a for loop between the two while loops that loops through the two players. Although based on the instructions your teacher is expecting a for loop. Of course, you could eliminate the for loop and just change the turn variable each time through the outer while loop. You would then need a conditional to see whether to ask (for the human player) or just keep going (for the computer player).

Personally, I don't like this version of two-die pig. You have a 10 in 36 chance of losing the turn score, compared to a 6 in 36 chance in standard one-die pig. Plus the 1 in 36 chance of losing your whole score. But it's what your teacher chose.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some help with Dice Game for daughter in school OptX 2 1,923 Feb-12-2021, 08:43 PM
Last Post: BashBedlam
  regarding dice game hola123 4 2,330 Feb-10-2021, 11:00 PM
Last Post: hola123
  Two dice Game of Pig help elliemehl 2 4,193 Feb-14-2019, 01:19 AM
Last Post: woooee
  help with while loop on dice game sean5 2 4,165 Dec-14-2017, 07:24 PM
Last Post: nilamo
  dice game im stuck sylerr 3 4,408 May-12-2017, 10:50 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020