Reputation: 1
I am one week into a coding bootcamp so forgive my simplistic and probably sloppy coding, but I was wondering how I could put in a running tally of wins for my two player dice rolling game. I am able to produce the final score and loop the function back around to start again if the players want to keep playing but I would like to keep a running tally of wins so the players know how many times each player has won. Here is my function:
def DiceRoll():
Dice1 = random.randint(1, 20)
Dice2 = random.randint(1, 12)
Dice3 = random.randint(1,6)
Dice4 = random.randint(1, 20)
Dice5 = random.randint(1, 12)
Dice6 = random.randint (1, 6)
DoubleDice1 = (((Dice1 + Dice2)*2) + Dice3)
DoubleDice2 = (((Dice1 + Dice3)*2) + Dice2)
DoubleDice3 = (((Dice2 + Dice3)*2) + Dice1)
DoubleDice4 = (((Dice4 + Dice5)*2) + Dice6)
DoubleDice5 = (((Dice4 + Dice6)*2) + Dice5)
DoubleDice6 = (((Dice5 + Dice6)*2) + Dice4)
TripleDice1 = ((Dice1 + Dice2 +Dice3) * 3)
TripleDice2 = ((Dice4 + Dice5 +Dice6) * 3)
print("Player 1, Roll?")
Roll = input("Y/N?")
if Roll =="y":
print("Ok!")
if Roll == "n":
print("Goodbye!")
time.sleep(2)
sys.exit(0)
print(" ")
print(Dice1, Dice2, Dice3)
Score1 = Dice1 + Dice2 + Dice3
if Dice1 == Dice2:
Score1 = DoubleDice1
print(Score1)
elif Dice1 ==Dice3:
Score1 = DoubleDice2
print(Score1)
elif Dice2 == Dice3:
Score1 = DoubleDice3
print(Score1)
elif Dice1 == Dice2 ==Dice3:
Score1 = TripleDice1
print(Score1)
else:
print(Dice1 + Dice2 + Dice3)
print("""
""")
print("Player 2, Roll?")
Roll2 = input("Y/N?")
if Roll2 =="y":
print("Ok!")
if Roll2 == "n":
print("Goodbye!")
time.sleep(2)
sys.exit(0)
print(" ")
print(Dice4, Dice5, Dice6)
Score2 = (Dice4 + Dice5 + Dice6)
if Dice4 == Dice5:
Score2 = DoubleDice4
print(Score2)
elif Dice4 == Dice6:
Score2 = DoubleDice5
print(Score2)
elif Dice5 == Dice6:
Score2 = DoubleDice6
print(Score2)
elif Dice4 == Dice5 ==Dice6:
Score2 = TripleDice2
print(Score2)
else:
print(Dice4 + Dice5 + Dice6)
print("""
""")
if Score1 > Score2:
print("Player 1:", Score1, "Player 2:", Score2)
print("Player 1 Wins!")
if Score1 < Score2:
print("Player 1:", Score1, "Player 2:", Score2)
print("Player 2 Wins!")
if Score1 == Score2:
print("Player 1:", Score1, "Player 2:", Score2)
print("Tie!")
Upvotes: 0
Views: 916
Reputation: 375
The most simplistic solution would be to have the DiceRoll function return which player won.
winner = None
if Score1 > Score2:
print("Player 1:", Score1, "Player 2:", Score2)
print("Player 1 Wins!")
winner = 1
elif Score1 < Score2:
print("Player 1:", Score1, "Player 2:", Score2)
print("Player 2 Wins!")
winner = 2
elif Score1 == Score2:
print("Player 1:", Score1, "Player 2:", Score2)
print("Tie!")
winner = 0
return winner
Then in the loop that calls DiceRoll, just check the returned result.
Now, you've got a lot of repeated code in this function. You're basically doing everything twice. This makes it a prime candidate for breaking into another function. I would suggest making a function that takes the player as a parameter, and then does the dice rolls for just that player, and returns the score. Then you could just call the function for each player, compare the results, and determine the winner from that.
One last thing. If a player chooses not to roll, it's exiting the program. That should probably be changed to be considered a forfeit, and have it exit at the outer loop so you can display the final tally.
Upvotes: 1
Reputation: 77860
Since there is no loop in your function, I assume that you have that control loop in the program that calls DiceRoll. To make a clean tally, you'll need to return the win/lose designation to that program, something like this:
if Score1 > Score2:
print("Player 1:", Score1, "Player 2:", Score2)
print("Player 1 Wins!")
winner = 1
elif Score1 < Score2:
print("Player 1:", Score1, "Player 2:", Score2)
print("Player 2 Wins!")
winner = 2
else:
print("Player 1:", Score1, "Player 2:", Score2)
print("Tie!")
winner = 0
Now, back in your main program, we'll have something like:
p1_wins = 0
p2_wins = 0
ties = 0
while game_on:
result = DiceRoll()
if result == 1:
p1_wins += 1
elif result = 2
p2_wins += 1
else:
ties += 1
I've kept this at the level of programming I think you're using. First you get to understand it. In a few more boot camp sessions, come back to see how you'd like to improve your coding.
Upvotes: 1