Skip to content

Instantly share code, notes, and snippets.

@AngelVI13
Created March 27, 2020 13:59
Show Gist options
  • Save AngelVI13/39f2dc4b36a8e47922f69cb2eeb48d03 to your computer and use it in GitHub Desktop.
Save AngelVI13/39f2dc4b36a8e47922f69cb2eeb48d03 to your computer and use it in GitHub Desktop.
Dice game gist
import random
import time
# Note: rolling variable is not changed anywhere else -> its not doing anything - remove it
rolling = True
rounds = 2
round_count = 0
player1_score = 0
player2_score = 0
# Note: method is very complex. It is hard to trace the whole logic at once. Focus on
# writing small methods whose logic is obvious at a glance.
def main():
while rolling and round_count <= rounds:
if round_count < rounds:
# Note: function definitions inside of flow control statements i.e. if statements,
# for loops etc. is generally not a good idea because they make your code very complex.
# Instead move the function outside of this while loop.
def roll():
# Note: adding global variables is not recommended,
# replace them in input arguments to the function and return values
global player1_score, player2_score, round_count
print("Player 1 turn.")
print("Rolling the dice...")
# Note: having "magic" numbers is not a good idea. If you want to
# change your sleep time in your whole game then you have to change
# every single place when this is done. If you replace this with a
# variable such as ROLL_WAIT_TIME = 3 and then use that variable
# everywhere - if you want to change the roll wait time you just
# have to change it in one place.
time.sleep(3)
# Note: look at "magic" number comment above
dice1 = random.randint(1, 6)
print(dice1)
# print by default outputs a newline character -> do you really need 2 newlines here ?
print("\n")
print("Player 2 turn.")
print("Rolling the dice...")
time.sleep(3)
dice2 = random.randint(1, 6)
print(dice2)
print("\n")
if dice1 > dice2:
print("Player 1 Wins!")
print("\n")
# Note: If you would like to increase the current value of a variable by some number
# there is a shorthand: player1_score += 1. This will add 1 to the current value
# of player1_score. Similarly you can use different operations:
# player1_score *= 2 (this will multiply the current value of player1_score by 2)
player1_score = player1_score + 1
round_count = round_count + 1
elif dice1 == dice2:
print("Draw!")
print("\n")
round_count = round_count + 1
else:
print("Player 2 Wins!")
print("\n")
player2_score = player2_score + 1
round_count = round_count + 1
else:
print("Game over!")
if player1_score > player2_score:
print("Player 1 Wins!")
elif player1_score == player2_score:
print("Draw!")
else:
print("Player 2 wins!")
roll()
# Note: typically you would like to only call main() if this file is run
# directly (for example by calling python file.py). However, you most likely
# do not want to automatically call main() if somebody imports this file. In order
# to prevent this happening you should enclose the call to main() in an if statement
# i.e. if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment