Adding dice_game.py
This commit is contained in:
parent
0f9964afb5
commit
a462f6c78a
100
dice_game.py
Normal file
100
dice_game.py
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
one = " \n * \n "
|
||||||
|
two = " * \n\n *"
|
||||||
|
three = " * \n * \n *"
|
||||||
|
four = " * *\n\n * *"
|
||||||
|
five = " * *\n *\n * *"
|
||||||
|
six = " * *\n * *\n * *"
|
||||||
|
|
||||||
|
dice_dict = {one: 1, two: 2, three: 3 , four: 4, five: 5, six: 6}
|
||||||
|
running = True
|
||||||
|
rolling = True
|
||||||
|
throws = 0
|
||||||
|
responses = ('y', 'n')
|
||||||
|
|
||||||
|
# Function to roll the dice when called
|
||||||
|
def roll_dice():
|
||||||
|
roll = input("Roll dice? (y/n) ")
|
||||||
|
while roll not in responses:
|
||||||
|
print("INVALID RESPONSE. Please try again.")
|
||||||
|
roll = input("Roll dice? (y/n) n")
|
||||||
|
continue
|
||||||
|
if roll == 'y':
|
||||||
|
first_roll = random.choice(tuple(dice_dict.keys()))
|
||||||
|
first_roll_int = dice_dict.get(first_roll)
|
||||||
|
second_roll = random.choice(tuple(dice_dict.keys()))
|
||||||
|
second_roll_int = dice_dict.get(second_roll)
|
||||||
|
elif roll == 'n':
|
||||||
|
print("Thanks for playing.")
|
||||||
|
time.sleep(3)
|
||||||
|
exit()
|
||||||
|
return first_roll, second_roll, first_roll_int, second_roll_int
|
||||||
|
|
||||||
|
print("------------------------------------------------")
|
||||||
|
print(" Python Dice Game ")
|
||||||
|
print("------------------------------------------------")
|
||||||
|
|
||||||
|
|
||||||
|
# Initial dice roll
|
||||||
|
first_die = random.choice(tuple(dice_dict.keys()))
|
||||||
|
first_die_int = dice_dict.get(first_die)
|
||||||
|
second_die = random.choice(tuple(dice_dict.keys()))
|
||||||
|
second_die_int = dice_dict.get(second_die)
|
||||||
|
|
||||||
|
# Display dice
|
||||||
|
print(first_die)
|
||||||
|
time.sleep(1)
|
||||||
|
print()
|
||||||
|
print(second_die)
|
||||||
|
|
||||||
|
# Check for wins and losses
|
||||||
|
# If 7 or 11, you win.
|
||||||
|
if first_die_int + second_die_int == 7:
|
||||||
|
print("\nYou win!")
|
||||||
|
elif first_die_int + second_die_int == 11:
|
||||||
|
print("\nYou win!")
|
||||||
|
# If any of these, you lose.
|
||||||
|
elif first_die_int + second_die_int == 12:
|
||||||
|
print("\nBOX CARS! You lose!")
|
||||||
|
elif first_die_int + second_die_int == 3:
|
||||||
|
print("\nLITTLE FEEBEE! You lose!")
|
||||||
|
elif first_die_int + second_die_int == 2:
|
||||||
|
print("SNAKE EYES! You lose!")
|
||||||
|
|
||||||
|
# Otherwise, keep rolling the dice to match the first dice roll total
|
||||||
|
else:
|
||||||
|
while rolling:
|
||||||
|
# Roll the dice
|
||||||
|
first_roll, second_roll, first_roll_int, second_roll_int = roll_dice()
|
||||||
|
print(first_roll)
|
||||||
|
time.sleep(1)
|
||||||
|
print()
|
||||||
|
print(second_roll)
|
||||||
|
# Check if dice total matches the first dice roll total
|
||||||
|
if not (first_roll_int + second_roll_int) == (first_die_int + second_die_int):
|
||||||
|
# Roll a 7 and you lose
|
||||||
|
if first_roll_int + second_roll_int == 7:
|
||||||
|
print("You lose!")
|
||||||
|
break
|
||||||
|
# Roll an 11 and you lose
|
||||||
|
elif first_roll_int + second_roll_int == 11:
|
||||||
|
print("You lose!")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
# Roll something other than 7 or 11, roll again
|
||||||
|
throws += 1
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
# If dice total matches first dice total, you win.
|
||||||
|
print("You win!")
|
||||||
|
# Stop rolling
|
||||||
|
rolling = False
|
||||||
|
print("Game over! Thanks for playing")
|
||||||
|
print()
|
||||||
|
print("------------------------------------------------")
|
||||||
|
print(" Game Stats ")
|
||||||
|
print("------------------------------------------------")
|
||||||
|
print(f"Number of tries to match: {throws}")
|
||||||
|
print("------------------------------------------------")
|
||||||
Loading…
Reference in New Issue
Block a user