Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Yahtzee
#1
The following program is a simplified version of Yahtzee, using only 3 die, and keeps a running total of your score vs the computer. Can anybody tell me why it is that when a Chance! occurs it isn't giving me the correct total (the sum of the 3 die faces)? Appreciated.


# @author Stephen Dickey


from random import randint


YAHTZEE = 50
TWO_OF_A_KIND = 25
NUM_DIE_SIDES = 6
die1 = randint(1, NUM_DIE_SIDES)
die2 = randint(1, NUM_DIE_SIDES)
die3 = randint(1, NUM_DIE_SIDES)
die = die1, die2, die3
CHANCE = 0
totalPointsPlayer = 0
totalPointsComputer = 0
count = 0

rollDice = input("Roll the dice (Y or N)? ")

while (rollDice.upper() == "Y" and count < 2):
    die1 = randint(1, NUM_DIE_SIDES)
    die2 = randint(1, NUM_DIE_SIDES) 
    die3 = randint(1, NUM_DIE_SIDES)
    CHANCE = die1 + die2 + die3
    if count == 0:
        print("Player rolls: ", die)
    else:
        print("Computer rolls: ", die)
    
    
    if count == 0:
        if (die1 == die2 == die3) :
            totalPointsPlayer += YAHTZEE
            print("Yahtzee! (+ %d)" % (YAHTZEE))
        elif ((die1 == die2) or (die1 == die3) or (die2 == die3)) :
            totalPointsPlayer += TWO_OF_A_KIND
            print("Two of a kind!(+ %d)" % (TWO_OF_A_KIND))
        elif ((die1 != die2) and (die1 != die3) and (die2 != die3)) :
            totalPointsPlayer += CHANCE
            print("Chance! (+ %d)" % (CHANCE))
        
    
    else: 
        if (die1 == die2 == die3) :
            totalPointsComputer += YAHTZEE
            print("Yahtzee! (+ %d)" % (YAHTZEE))
        elif ((die1 == die2) or (die1 == die3) or (die2 == die3)) :
            totalPointsComputer += TWO_OF_A_KIND
            print("Two of a kind!(+ %d)" % (TWO_OF_A_KIND))
        elif ((die1 != die2) and (die1 != die3) and (die2 != die3)) :
            totalPointsComputer += CHANCE
            print("Chance! (+ %d)" % (CHANCE))
        
        print()
        print("===========================")
        print("Player total points: %d " % (totalPointsPlayer))
        print("Computer total points: %d " % (totalPointsComputer))     
        print("===========================")
        print()    
        
    count = count + 1
    
    if count == 2:
        rollDice = input("Roll the dice again (Y or N)? ")
        if (rollDice.upper() == "Y"):
            count = 0
        print()     
           
Reply
#2
That's not really the problem here. The problem is line 13: die = die1, die2, die3. Making that tuple is fine in and of itself, but later on (lines 27 and 29), you assume that as die1, die2, and die3 change, die will also change. This is not the case. Whatever the values of the three dice are at the beginning of the program, that's what they are for the rest of the program. Or maybe you're not making that assumption, and you just forgot to copy line 13 and put it after line 24, because that fixes your problem.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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