Merge pull request #98 from djaychela/main

Added python version of MATHDICE
This commit is contained in:
Jeff Atwood
2021-02-27 17:03:02 -08:00
committed by GitHub
2 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
from random import randint
print("Math Dice")
print("https://github.com/coding-horror/basic-computer-games")
print()
print("""This program generates images of two dice.
When two dice and an equals sign followed by a question
mark have been printed, type your answer, and hit the ENTER
key.
To conclude the program, type 0.
""")
def print_dice(n):
def print_0():
print("| |")
def print_2():
print("| * * |")
print(" ----- ")
if n in [4,5,6]:
print_2()
elif n in [2,3]:
print("| * |")
else:
print_0()
if n in [1,3,5]:
print("| * |")
elif n in [2,4]:
print_0()
else:
print_2()
if n in [4,5,6]:
print_2()
elif n in [2,3]:
print("| * |")
else:
print_0()
print(" ----- ")
def main():
while True:
d1 = randint(1,6)
d2 = randint(1,6)
guess = 13
print_dice(d1)
print(" +")
print_dice(d2)
print(" =")
tries = 0
while guess != (d1 + d2) and tries < 2:
if tries == 1:
print("No, count the spots and give another answer.")
try:
guess = int(input())
except ValueError:
print("That's not a number!")
if guess == 0:
exit()
tries += 1
if guess != (d1 + d2):
print(f"No, the answer is {d1 + d2}!")
else:
print("Correct!")
print("The dice roll again....")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,80 @@
from math import sqrt
from random import randint
def introduction():
print(
"""The object of this game is to find 4 mugwumps
hidden on a 10*10 grid. Homebase is position 0,0.
Any guess you make must be two numbers with each
number between 0 and 9 inclusive. First number
is distance to right of homebase, and second number
is the distance above homebase."""
)
print(
"""You get 10 tries. After each try, I will tell
you how far you are from each mugwump."""
)
def generate_mugwumps(n=4):
mugwumps = []
for _ in range(n):
current = [randint(0, 9), randint(0, 9)]
mugwumps.append(current)
return mugwumps
def reveal_mugwumps(mugwumps):
print("Sorry, that's 10 tries. Here's where they're hiding.")
for idx, mugwump in enumerate(mugwumps, 1):
if mugwump[0] != -1:
print(f"Mugwump {idx} is at {mugwump[0]},{mugwump[1]}")
def calculate_distance(guess, mugwump):
d = sqrt(((mugwump[0] - guess[0]) ** 2) + ((mugwump[1] - guess[1]) ** 2))
return d
def play_again():
print("THAT WAS FUN! LET'S PLAY AGAIN.......")
choice = input("Press Enter to play again, any other key then Enter to quit.")
if choice == "":
print("Four more mugwumps are now in hiding.")
else:
exit()
def play_round():
mugwumps = generate_mugwumps()
turns = 1
score = 0
while turns <= 10 and score != 4:
m = -1
while m == -1:
try:
m, n = map(int, input(f"Turn {turns} - what is your guess? ").split())
except ValueError:
m = -1
for idx, mugwump in enumerate(mugwumps):
if m == mugwump[0] and n == mugwump[1]:
print(f"You found mugwump {idx + 1}")
mugwumps[idx][0] = -1
score += 1
if mugwump[0] == -1:
continue
print(
f"You are {calculate_distance((m, n), mugwump):.1f} units from mugwump {idx + 1}"
)
turns += 1
if score == 4:
print(f"Well done! You got them all in {turns} turns.")
else:
reveal_mugwumps(mugwumps)
if __name__ == "__main__":
introduction()
while True:
play_round()
play_again()