Skip to content

Instantly share code, notes, and snippets.

@AngelVI13
Created March 27, 2020 14:14
Show Gist options
  • Save AngelVI13/0f50e59e13bc5215e1ab3bfb1da117b0 to your computer and use it in GitHub Desktop.
Save AngelVI13/0f50e59e13bc5215e1ab3bfb1da117b0 to your computer and use it in GitHub Desktop.
Dice game advanced
import random
import time
class DiceGame:
ROLL_WAIT_TIME = 3
def __init__(self, rounds):
self.rounds = rounds
self.player1_score = 0
self.player2_score = 0
self.round_count = 0
def reset_game(self):
# reset scores so that game can be played again
self.player1_score = 0
self.player2_score = 0
self.round_count = 0
def roll(self):
dice1, dice2 = self.get_rolls_for_both_players()
if dice1 > dice2:
print("Player 1 Wins!")
print("\n")
self.player1_score += 1
elif dice1 == dice2:
print("Draw!")
print("\n")
else:
print("Player 2 Wins!")
print("\n")
self.player2_score += 1
# round count is incremented no matter the result
self.round_count += 1
def get_rolls_for_both_players(self):
print("Player 1 turn.")
print("Rolling the dice...")
time.sleep(self.ROLL_WAIT_TIME)
dice1 = random.randint(1, 6)
print(dice1)
print("\n")
print("Player 2 turn.")
print("Rolling the dice...")
time.sleep(self.ROLL_WAIT_TIME)
dice2 = random.randint(1, 6)
print(dice2)
print("\n")
return dice1, dice2
def play(self):
while self.round_count <= self.rounds:
if self.round_count < self.rounds:
self.roll()
else:
print("Game over!")
if self.player1_score > self.player2_score:
print("Player 1 Wins!")
elif self.player1_score == self.player2_score:
print("Draw!")
else:
print("Player 2 wins!")
# exit the while loop because the game is over
break
if __name__ == '__main__':
dice_game = DiceGame(rounds=5)
# dice_game.ROLL_WAIT_TIME = 0 you can change the roll wait time if you want
dice_game.play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment