Initial commit
This commit is contained in:
147
wizard.py
Normal file
147
wizard.py
Normal file
@@ -0,0 +1,147 @@
|
||||
import os
|
||||
|
||||
MIN_CELL_WIDTH = 15
|
||||
|
||||
|
||||
def query(text):
|
||||
return input(f"{text} \n> ")
|
||||
|
||||
|
||||
def query_int(text):
|
||||
try:
|
||||
ans = query(text)
|
||||
return int(ans)
|
||||
except ValueError:
|
||||
print("Please enter a valid number!")
|
||||
return query_int(text)
|
||||
|
||||
|
||||
def clear():
|
||||
os.system("cls" if os.name in ("nt", "dos") else "clear")
|
||||
|
||||
|
||||
def query_yes_no(question, default="yes"):
|
||||
"""Ask a yes/no question via raw_input() and return their answer.
|
||||
|
||||
https://stackoverflow.com/questions/3041986/apt-command-line-interface-like-yes-no-input
|
||||
|
||||
"question" is a string that is presented to the user.
|
||||
"default" is the presumed answer if the user just hits <Enter>.
|
||||
It must be "yes" (the default), "no" or None (meaning
|
||||
an answer is required of the user).
|
||||
|
||||
The "answer" return value is True for "yes" or False for "no".
|
||||
"""
|
||||
valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
|
||||
if default is None:
|
||||
prompt = " [y/n] "
|
||||
elif default == "yes":
|
||||
prompt = " [Y/n] "
|
||||
elif default == "no":
|
||||
prompt = " [y/N] "
|
||||
else:
|
||||
raise ValueError("invalid default answer: '%s'" % default)
|
||||
|
||||
while True:
|
||||
choice = query(question + prompt).lower()
|
||||
if default is not None and choice == "":
|
||||
return valid[default]
|
||||
elif choice in valid:
|
||||
return valid[choice]
|
||||
else:
|
||||
print("Please respond with 'yes' or 'no' (or 'y' or 'n').")
|
||||
|
||||
|
||||
def remove_line(d):
|
||||
for key in d.keys():
|
||||
del d[key][-1]
|
||||
|
||||
|
||||
def calc_points(guess, result):
|
||||
if guess == result:
|
||||
return 20 + guess * 10
|
||||
else:
|
||||
return -10 * abs(guess - result)
|
||||
|
||||
|
||||
def print_standings(players, guesses, results):
|
||||
# Header
|
||||
width = max(MIN_CELL_WIDTH, *[len(player) for player in players])
|
||||
|
||||
print("| Round |", end="")
|
||||
for player in players:
|
||||
pad = " " * (width - len(player) - 2)
|
||||
print(f" {player}{pad} |", end="")
|
||||
print()
|
||||
print("-" * (9 + len(players) * (width + 1)))
|
||||
|
||||
# Data
|
||||
points = {player: 0 for player in players}
|
||||
for num_round in range(1, len(guesses[players[0]]) + 1):
|
||||
pad = 7 - len(str(num_round)) - 2
|
||||
print(f"| {' ' * pad}{num_round} |", end="")
|
||||
|
||||
for player in players:
|
||||
if num_round <= len(results[player]):
|
||||
points[player] += calc_points(guesses[player][num_round - 1], results[player][num_round - 1])
|
||||
text = f"{points[player]} - {guesses[player][num_round - 1]} ({results[player][num_round - 1]})"
|
||||
else:
|
||||
text = f"??? - {guesses[player][num_round - 1]} (?)"
|
||||
|
||||
pad = " " * (width - len(text) - 2)
|
||||
print(f" {pad}{text} |", end="")
|
||||
print()
|
||||
|
||||
|
||||
def main():
|
||||
# SETUP
|
||||
clear()
|
||||
print("Welcome! This is WiZaRδ!\n")
|
||||
|
||||
players = [name.strip() for name in query("Enter Player Names (separated by comma)").split(",")]
|
||||
guesses = {player: [] for player in players}
|
||||
results = {player: [] for player in players}
|
||||
|
||||
num_rounds = 60 // len(players)
|
||||
|
||||
# NORMAL GAMEPLAY
|
||||
|
||||
for i in range(1, num_rounds + 1):
|
||||
print("\n\n")
|
||||
print(f"Round {i} of {num_rounds}")
|
||||
print(f"{players[(i - 1) % len(players)]} deals cards, {players[i % len(players)]} begins\n\n")
|
||||
|
||||
# INPUT GUESSES
|
||||
|
||||
while True:
|
||||
guess_sum = 0
|
||||
for j in range(i, i + len(players)):
|
||||
player = players[j % len(players)]
|
||||
guess = query_int(f"{player}, enter your guess{f' (not {i - guess_sum})' if j == i + len(players) - 1 and i > 3 else ''}!")
|
||||
guesses[player] += [guess]
|
||||
guess_sum += guess
|
||||
if not query_yes_no("Reenter guesses?", default="no"):
|
||||
break
|
||||
else:
|
||||
remove_line(guesses)
|
||||
|
||||
# PRINT STANDINGS
|
||||
print_standings(players, guesses, results)
|
||||
|
||||
# INPUT RESULTS
|
||||
while True:
|
||||
for j in range(i, i + len(players)):
|
||||
player = players[j % len(players)]
|
||||
result = query_int(f"{player}, how many stitches did you get?")
|
||||
results[player] += [result]
|
||||
if not query_yes_no("Reenter results?", default="no"):
|
||||
break
|
||||
else:
|
||||
remove_line(results)
|
||||
|
||||
# PRINT STANDINGS
|
||||
print_standings(players, guesses, results)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user