Python Forum
Two dice Game of Pig help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Two dice Game of Pig help
#1
I've been working on my python assignment and have been hitting a lot of road blocks when trying to make my game of pig. I know I need an outside loop to go back and forth for the turns of the computer and human but it hasn't been working and was hoping to get some help. I was told that I can write everything in a main function and that I only need two while loops.
these are the rules,

The first player to accumulate a score of 100 wins.
The human goes first.
After one roll (of both dice), a player has the choice to "hold" or to roll again unless he rolls doubles.
A player rolls two six sided dice. Certain conditions apply:
If both dice are ones, then you add 25 to your turn score and you must roll again.
If one dice is one, then your turn is over and your turn score is set to zero.
If both dice match ("doubles"), other than two ones, then you gain twice the sum of the dice, and you must roll again. For example, if you rolled two fours then you would gain 16 and have to roll again.
For any other dice combination, you just add the dice total to your turn score and you have the choice of rolling again.
If you complete your turn without rolling a one, that is to say that you chose not to roll again, then your turn score is added to your accumulated game score.

here is what my code looks like right now
import random

def main():
    player_score = 0
    computer_score = 0
    while player_score or computer_score < 100 :
        die1 = random.randint(1,6)
        die2 = random.randint(1,6)
        print("You rolled a {} and a {}".format(die1, die2))
        total = die1 + die2
        doubletotal = 2 * total
        yes = True
        no = False

        if die1 == die2 :
            if die1 != 1 :
                print("{} has been added to your score.".format(doubletotal))
                print("You must roll again. Press enter.")
                input()
            elif die1 == 1 :
                print("25 has been added to your score.")
                print("You must roll again. Press enter.")
                input()
        if die1 == 1 or die2 == 1 :
            if die1 != die2 :
                print("Your turn is over nothing has been added to you score.")
        else :
            print("{} has been added to your score.".format(total))
            again = str(input("Would you like to roll again, yes or no?"))
            if yes :
                print("Rolling...")
            else :
                print("It is now the computer's turn")
        if die1 == die2 :
            if die1 != 1 :
                new_score = player_score + doubletotal
            elif die1 == 1 :
                new_score = player_score + 25
        else :
            new_score = player_score + total
        print("Your new score is {}".format(new_score))
    player_score = player_score + new_score    

    die1 = random.randint(1,6)
    die2 = random.randint(1,6)
    print("The computer rolled a {} and a {}".format(die1, die2))
    total = die1 + die2
    doubletotal = total * 2
    if die1 == die2 :
        if die1 != 1 :
            final = computer_score + doubletotal
            print("{} has been added to the computer's score.".format(doubletotal))
            print("The computer's score is now, {}".format(final))
        elif die1 == 1 :
            final2 = computer_score + 25
            print("25 has been added to the computer's score.")
            print("The computer's score is now, {}".format(final2))
    if die1 == 1 or die2 == 1 :
        print("The computer's turn is over nothing has been added to the computer's score.")
    else :
        final3 = computer_score + total
        print("{} has been added to the computer's score.".format(total))
        print("The computer's score is now, {}".format(final3))
        print("The computer's score is now {}.".format(computer_score))
    player_score = player_score + 1
    computer_score = computer_score + 1
main()
Reply
#2
It would be helpful if you could describe the problems you are having. The main problem I see is that not everything is under the while loop. Everything from line 42 ib needs to be indented. Also the condition on your while loop is incorrectly using the or operator. You need to have an expression on each side of the or (see here for details).

I would also adjust the scores when you tell the user about the scores. That will clean up your code quite a bit, removing the duplication of the if/elif structures.

Personally I don't like this version of two-die pig. I normal pig you have a 1 in 6 chance of losing your points for the turn. In this version of two die pig, you have an 10 in 36 chance, which is two-thirds higher. I prefer using sevens or pairs to keep the odds similar, but obviously your teacher disagrees.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I assume you know that you print additions to total but no additions are made, i.e.
 
        if die1 == die2 :
            if die1 != 1 :
                print("{} has been added to your score.".format(doubletotal))
                print("You must roll again. Press enter.")
                input()
            elif die1 == 1 :
                print("25 has been added to your score.")
                print("You must roll again. Press enter.")
                input()
Also when die1 == die2 == 1
    if die1 == die2 :
    ...
        elif die1 == 1 :
            final2 = computer_score + 25
            print("25 has been added to the computer's score.")
            print("The computer's score is now, {}".format(final2))
    if die1 == 1 or die2 == 1 :
        print("The computer's turn is over nothing has been added to the computer's score.") 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some help with Dice Game for daughter in school OptX 2 1,894 Feb-12-2021, 08:43 PM
Last Post: BashBedlam
  regarding dice game hola123 4 2,290 Feb-10-2021, 11:00 PM
Last Post: hola123
  Two Dice Pig Game in Python 3.6 Help inspired22 4 11,535 Oct-10-2018, 01:17 PM
Last Post: ichabod801
  help with while loop on dice game sean5 2 4,144 Dec-14-2017, 07:24 PM
Last Post: nilamo
  dice game im stuck sylerr 3 4,388 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