Skip to content

Instantly share code, notes, and snippets.

@ksaver
Created May 29, 2020 13:25
Show Gist options
  • Save ksaver/a183fa15a669e2ebc390cfcf5590782a to your computer and use it in GitHub Desktop.
Save ksaver/a183fa15a669e2ebc390cfcf5590782a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#
# simple_dice_game.py
# Simple example of a dice game
#
from random import randint
from time import sleep
from os import system
def head(text):
print("------------------")
print(f" {text} ")
print("------------------")
def roll():
dice1 = randint(1,6)
dice2 = randint(1,6)
total = dice1 + dice2
results = {"dice1": dice1, "dice2": dice2, "total": total}
return(results)
def main():
score = {"user": 0, "ai": 0, "draw": 0}
quit = False
while not quit:
system("clear")
head("Dice Game 1.0")
print("[*] Player rolling the dice.. ")
sleep(1)
user = roll()
print(f"\t[{user['dice1']}] + [{user['dice2']}] = {user['total']}")
print("[*] AI rolling the dice.. ")
sleep(1)
ai = roll()
print(f"\t[{ai['dice1']}] + [{ai['dice2']}] = {ai['total']}")
if user["total"] > ai["total"]:
print("[*] You won!")
score["user"] += 1
elif user["total"] < ai["total"]:
print("[*] AI won!")
score["ai"] += 1
else:
print("It's a draw!")
score["draw"] += 1
key = input("Press Enter to roll the dice (Press \"q\" to exit): ")
if key.lower() == "q":
quit = True
continue
# quit
head(f"Score: [User: {score['user']}] [AI: {score['ai']}] [Draw games: {score['draw']}]")
print("Good bye!")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment