mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 15:50:20 -08:00
BUG: Hangman (Python) had a List-vs-Str comparison
MAINT: Add type annotations to find such issues
This commit is contained in:
@@ -115,7 +115,7 @@ def game_loop() -> None:
|
||||
game_over = True
|
||||
|
||||
|
||||
def main():
|
||||
def main() -> None:
|
||||
print(
|
||||
"""
|
||||
Acey Ducey is a card game where you play against the computer.
|
||||
|
||||
@@ -6,10 +6,10 @@ You are able to choose your shot types as well as defensive formations
|
||||
"""
|
||||
|
||||
import random
|
||||
from typing import Optional, List, Literal
|
||||
from typing import List, Literal, Optional
|
||||
|
||||
|
||||
def explain_keyboard_inputs():
|
||||
def explain_keyboard_inputs() -> None:
|
||||
print("\t\t\t Basketball")
|
||||
print("\t Creative Computing Morristown, New Jersey\n\n\n")
|
||||
print("This is Dartmouth College basketball. ")
|
||||
|
||||
@@ -112,10 +112,10 @@ def print_intro() -> None:
|
||||
|
||||
def ask_bool(prompt: str) -> bool:
|
||||
while True:
|
||||
answer = input(prompt)
|
||||
if answer == "YES":
|
||||
answer = input(prompt).lower()
|
||||
if answer == "yes":
|
||||
return True
|
||||
elif answer == "NO":
|
||||
elif answer == "no":
|
||||
return False
|
||||
else:
|
||||
print("INCORRECT ANSWER - - PLEASE TYPE 'YES' OR 'NO'.")
|
||||
|
||||
@@ -2,241 +2,10 @@
|
||||
|
||||
|
||||
# This data is meant to be read-only, so we are storing it in a tuple
|
||||
DATA = (
|
||||
2,
|
||||
21,
|
||||
14,
|
||||
14,
|
||||
25,
|
||||
1,
|
||||
2,
|
||||
-1,
|
||||
0,
|
||||
2,
|
||||
45,
|
||||
50,
|
||||
-1,
|
||||
0,
|
||||
5,
|
||||
43,
|
||||
52,
|
||||
-1,
|
||||
0,
|
||||
7,
|
||||
41,
|
||||
52,
|
||||
-1,
|
||||
1,
|
||||
9,
|
||||
37,
|
||||
50,
|
||||
-1,
|
||||
2,
|
||||
11,
|
||||
36,
|
||||
50,
|
||||
-1,
|
||||
3,
|
||||
13,
|
||||
34,
|
||||
49,
|
||||
-1,
|
||||
4,
|
||||
14,
|
||||
32,
|
||||
48,
|
||||
-1,
|
||||
5,
|
||||
15,
|
||||
31,
|
||||
47,
|
||||
-1,
|
||||
6,
|
||||
16,
|
||||
30,
|
||||
45,
|
||||
-1,
|
||||
7,
|
||||
17,
|
||||
29,
|
||||
44,
|
||||
-1,
|
||||
8,
|
||||
19,
|
||||
28,
|
||||
43,
|
||||
-1,
|
||||
9,
|
||||
20,
|
||||
27,
|
||||
41,
|
||||
-1,
|
||||
10,
|
||||
21,
|
||||
26,
|
||||
40,
|
||||
-1,
|
||||
11,
|
||||
22,
|
||||
25,
|
||||
38,
|
||||
-1,
|
||||
12,
|
||||
22,
|
||||
24,
|
||||
36,
|
||||
-1,
|
||||
13,
|
||||
34,
|
||||
-1,
|
||||
14,
|
||||
33,
|
||||
-1,
|
||||
15,
|
||||
31,
|
||||
-1,
|
||||
17,
|
||||
29,
|
||||
-1,
|
||||
18,
|
||||
27,
|
||||
-1,
|
||||
19,
|
||||
26,
|
||||
-1,
|
||||
16,
|
||||
28,
|
||||
-1,
|
||||
13,
|
||||
30,
|
||||
-1,
|
||||
11,
|
||||
31,
|
||||
-1,
|
||||
10,
|
||||
32,
|
||||
-1,
|
||||
8,
|
||||
33,
|
||||
-1,
|
||||
7,
|
||||
34,
|
||||
-1,
|
||||
6,
|
||||
13,
|
||||
16,
|
||||
34,
|
||||
-1,
|
||||
5,
|
||||
12,
|
||||
16,
|
||||
35,
|
||||
-1,
|
||||
4,
|
||||
12,
|
||||
16,
|
||||
35,
|
||||
-1,
|
||||
3,
|
||||
12,
|
||||
15,
|
||||
35,
|
||||
-1,
|
||||
2,
|
||||
35,
|
||||
-1,
|
||||
1,
|
||||
35,
|
||||
-1,
|
||||
2,
|
||||
34,
|
||||
-1,
|
||||
3,
|
||||
34,
|
||||
-1,
|
||||
4,
|
||||
33,
|
||||
-1,
|
||||
6,
|
||||
33,
|
||||
-1,
|
||||
10,
|
||||
32,
|
||||
34,
|
||||
34,
|
||||
-1,
|
||||
14,
|
||||
17,
|
||||
19,
|
||||
25,
|
||||
28,
|
||||
31,
|
||||
35,
|
||||
35,
|
||||
-1,
|
||||
15,
|
||||
19,
|
||||
23,
|
||||
30,
|
||||
36,
|
||||
36,
|
||||
-1,
|
||||
14,
|
||||
18,
|
||||
21,
|
||||
21,
|
||||
24,
|
||||
30,
|
||||
37,
|
||||
37,
|
||||
-1,
|
||||
13,
|
||||
18,
|
||||
23,
|
||||
29,
|
||||
33,
|
||||
38,
|
||||
-1,
|
||||
12,
|
||||
29,
|
||||
31,
|
||||
33,
|
||||
-1,
|
||||
11,
|
||||
13,
|
||||
17,
|
||||
17,
|
||||
19,
|
||||
19,
|
||||
22,
|
||||
22,
|
||||
24,
|
||||
31,
|
||||
-1,
|
||||
10,
|
||||
11,
|
||||
17,
|
||||
18,
|
||||
22,
|
||||
22,
|
||||
24,
|
||||
24,
|
||||
29,
|
||||
29,
|
||||
-1,
|
||||
22,
|
||||
23,
|
||||
26,
|
||||
29,
|
||||
-1,
|
||||
27,
|
||||
29,
|
||||
-1,
|
||||
28,
|
||||
29,
|
||||
-1,
|
||||
4096,
|
||||
)
|
||||
import json
|
||||
|
||||
with open("data.json") as f:
|
||||
DATA = tuple(json.load(f))
|
||||
|
||||
|
||||
def display_intro() -> None:
|
||||
@@ -245,7 +14,7 @@ def display_intro() -> None:
|
||||
print("\n\n")
|
||||
|
||||
|
||||
def tab(column) -> str:
|
||||
def tab(column: int) -> str:
|
||||
"""Emulates the TAB command in BASIC. Returns a string with ASCII
|
||||
codes for setting the cursor to the specified column."""
|
||||
return f"\r\33[{column}C"
|
||||
|
||||
235
19_Bunny/python/data.json
Normal file
235
19_Bunny/python/data.json
Normal file
@@ -0,0 +1,235 @@
|
||||
[
|
||||
2,
|
||||
21,
|
||||
14,
|
||||
14,
|
||||
25,
|
||||
1,
|
||||
2,
|
||||
-1,
|
||||
0,
|
||||
2,
|
||||
45,
|
||||
50,
|
||||
-1,
|
||||
0,
|
||||
5,
|
||||
43,
|
||||
52,
|
||||
-1,
|
||||
0,
|
||||
7,
|
||||
41,
|
||||
52,
|
||||
-1,
|
||||
1,
|
||||
9,
|
||||
37,
|
||||
50,
|
||||
-1,
|
||||
2,
|
||||
11,
|
||||
36,
|
||||
50,
|
||||
-1,
|
||||
3,
|
||||
13,
|
||||
34,
|
||||
49,
|
||||
-1,
|
||||
4,
|
||||
14,
|
||||
32,
|
||||
48,
|
||||
-1,
|
||||
5,
|
||||
15,
|
||||
31,
|
||||
47,
|
||||
-1,
|
||||
6,
|
||||
16,
|
||||
30,
|
||||
45,
|
||||
-1,
|
||||
7,
|
||||
17,
|
||||
29,
|
||||
44,
|
||||
-1,
|
||||
8,
|
||||
19,
|
||||
28,
|
||||
43,
|
||||
-1,
|
||||
9,
|
||||
20,
|
||||
27,
|
||||
41,
|
||||
-1,
|
||||
10,
|
||||
21,
|
||||
26,
|
||||
40,
|
||||
-1,
|
||||
11,
|
||||
22,
|
||||
25,
|
||||
38,
|
||||
-1,
|
||||
12,
|
||||
22,
|
||||
24,
|
||||
36,
|
||||
-1,
|
||||
13,
|
||||
34,
|
||||
-1,
|
||||
14,
|
||||
33,
|
||||
-1,
|
||||
15,
|
||||
31,
|
||||
-1,
|
||||
17,
|
||||
29,
|
||||
-1,
|
||||
18,
|
||||
27,
|
||||
-1,
|
||||
19,
|
||||
26,
|
||||
-1,
|
||||
16,
|
||||
28,
|
||||
-1,
|
||||
13,
|
||||
30,
|
||||
-1,
|
||||
11,
|
||||
31,
|
||||
-1,
|
||||
10,
|
||||
32,
|
||||
-1,
|
||||
8,
|
||||
33,
|
||||
-1,
|
||||
7,
|
||||
34,
|
||||
-1,
|
||||
6,
|
||||
13,
|
||||
16,
|
||||
34,
|
||||
-1,
|
||||
5,
|
||||
12,
|
||||
16,
|
||||
35,
|
||||
-1,
|
||||
4,
|
||||
12,
|
||||
16,
|
||||
35,
|
||||
-1,
|
||||
3,
|
||||
12,
|
||||
15,
|
||||
35,
|
||||
-1,
|
||||
2,
|
||||
35,
|
||||
-1,
|
||||
1,
|
||||
35,
|
||||
-1,
|
||||
2,
|
||||
34,
|
||||
-1,
|
||||
3,
|
||||
34,
|
||||
-1,
|
||||
4,
|
||||
33,
|
||||
-1,
|
||||
6,
|
||||
33,
|
||||
-1,
|
||||
10,
|
||||
32,
|
||||
34,
|
||||
34,
|
||||
-1,
|
||||
14,
|
||||
17,
|
||||
19,
|
||||
25,
|
||||
28,
|
||||
31,
|
||||
35,
|
||||
35,
|
||||
-1,
|
||||
15,
|
||||
19,
|
||||
23,
|
||||
30,
|
||||
36,
|
||||
36,
|
||||
-1,
|
||||
14,
|
||||
18,
|
||||
21,
|
||||
21,
|
||||
24,
|
||||
30,
|
||||
37,
|
||||
37,
|
||||
-1,
|
||||
13,
|
||||
18,
|
||||
23,
|
||||
29,
|
||||
33,
|
||||
38,
|
||||
-1,
|
||||
12,
|
||||
29,
|
||||
31,
|
||||
33,
|
||||
-1,
|
||||
11,
|
||||
13,
|
||||
17,
|
||||
17,
|
||||
19,
|
||||
19,
|
||||
22,
|
||||
22,
|
||||
24,
|
||||
31,
|
||||
-1,
|
||||
10,
|
||||
11,
|
||||
17,
|
||||
18,
|
||||
22,
|
||||
22,
|
||||
24,
|
||||
24,
|
||||
29,
|
||||
29,
|
||||
-1,
|
||||
22,
|
||||
23,
|
||||
26,
|
||||
29,
|
||||
-1,
|
||||
27,
|
||||
29,
|
||||
-1,
|
||||
28,
|
||||
29,
|
||||
-1,
|
||||
4096
|
||||
]
|
||||
@@ -24,7 +24,7 @@ import random
|
||||
|
||||
|
||||
def main() -> None:
|
||||
WORDS = [
|
||||
words = [
|
||||
[
|
||||
"Ability",
|
||||
"Basal",
|
||||
@@ -85,7 +85,7 @@ def main() -> None:
|
||||
still_running = True
|
||||
while still_running:
|
||||
phrase = ""
|
||||
for section in WORDS:
|
||||
for section in words:
|
||||
if len(phrase) > 0:
|
||||
phrase += " "
|
||||
phrase += section[random.randint(0, len(section) - 1)]
|
||||
|
||||
@@ -65,7 +65,7 @@ def parse_input() -> Tuple[int, bool]:
|
||||
return day, leap_day
|
||||
|
||||
|
||||
def calendar(weekday, leap_year):
|
||||
def calendar(weekday: int, leap_year: bool) -> None:
|
||||
"""
|
||||
function to print a year's calendar.
|
||||
|
||||
@@ -104,9 +104,8 @@ def calendar(weekday, leap_year):
|
||||
for n in range(1, 13):
|
||||
days_count += months_days[n - 1]
|
||||
print(
|
||||
"** {} ****************** {} ****************** {} **\n".format(
|
||||
days_count, months_names[n - 1], years_day - days_count
|
||||
)
|
||||
f"** {days_count} ****************** {months_names[n - 1]} "
|
||||
f"****************** {years_day - days_count} **\n"
|
||||
)
|
||||
print(days)
|
||||
print(sep)
|
||||
@@ -121,7 +120,7 @@ def calendar(weekday, leap_year):
|
||||
break
|
||||
|
||||
if d2 <= 0:
|
||||
print("{}".format(" "), end=" ")
|
||||
print(" ", end=" ")
|
||||
elif d2 < 10:
|
||||
print(f" {d2}", end=" ")
|
||||
else:
|
||||
|
||||
@@ -16,23 +16,17 @@ def print_centered(msg: str) -> None:
|
||||
|
||||
def print_header(title: str) -> None:
|
||||
print_centered(title)
|
||||
print_centered("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
print_centered("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n")
|
||||
|
||||
|
||||
def print_introduction() -> None:
|
||||
print("I, YOUR FRIENDLY MICROCOMPUTER, WILL DETERMINE")
|
||||
print("THE CORRECT CHANGE FOR ITEMS COSTING UP TO $100.")
|
||||
print()
|
||||
print()
|
||||
print("THE CORRECT CHANGE FOR ITEMS COSTING UP TO $100.\n\n")
|
||||
|
||||
|
||||
def pennies_to_dollar_string(p):
|
||||
def pennies_to_dollar_string(p: float) -> str:
|
||||
d = p / 100
|
||||
ds = f"${d:0.2f}"
|
||||
return ds
|
||||
return f"${d:0.2f}"
|
||||
|
||||
|
||||
def compute_change() -> None:
|
||||
@@ -95,9 +89,7 @@ def compute_change() -> None:
|
||||
|
||||
|
||||
def print_thanks() -> None:
|
||||
print("THANK YOU, COME AGAIN.")
|
||||
print()
|
||||
print()
|
||||
print("THANK YOU, COME AGAIN.\n\n")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
||||
@@ -37,10 +37,7 @@ def print_centered(msg: str) -> None:
|
||||
|
||||
def print_header(title: str) -> None:
|
||||
print_centered(title)
|
||||
print_centered("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
print_centered("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n")
|
||||
|
||||
|
||||
def get_coordinates(prompt: str) -> Tuple[int, int]:
|
||||
@@ -373,20 +370,15 @@ def print_instructions() -> None:
|
||||
print("(7,0) IS THE LOWER RIGHT CORNER")
|
||||
print("(7,7) IS THE UPPER RIGHT CORNER")
|
||||
print("THE COMPUTER WILL TYPE '+TO' WHEN YOU HAVE ANOTHER")
|
||||
print("JUMP. TYPE TWO NEGATIVE NUMBERS IF YOU CANNOT JUMP.")
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
print("JUMP. TYPE TWO NEGATIVE NUMBERS IF YOU CANNOT JUMP.\n\n\n")
|
||||
|
||||
|
||||
def print_human_won() -> None:
|
||||
print()
|
||||
print("YOU WIN.")
|
||||
print("\nYOU WIN.")
|
||||
|
||||
|
||||
def print_computer_won() -> None:
|
||||
print()
|
||||
print("I WIN.")
|
||||
print("\nI WIN.")
|
||||
|
||||
|
||||
def play_game() -> None:
|
||||
|
||||
@@ -20,7 +20,7 @@ def print_with_tab(space_count: int, msg: str) -> None:
|
||||
print(spaces + msg)
|
||||
|
||||
|
||||
def play_scenario():
|
||||
def play_scenario() -> bool:
|
||||
acid_amount = random.randint(1, 50)
|
||||
|
||||
water_amount = 7 * acid_amount / 3
|
||||
@@ -43,27 +43,23 @@ def play_scenario():
|
||||
return True
|
||||
|
||||
|
||||
def show_failure():
|
||||
def show_failure() -> None:
|
||||
print(" SIZZLE! YOU HAVE JUST BEEN DESALINATED INTO A BLOB")
|
||||
print(" OF QUIVERING PROTOPLASM!")
|
||||
|
||||
|
||||
def show_success():
|
||||
print(" GOOD JOB! YOU MAY BREATHE NOW, BUT DON'T INHALE THE FUMES!")
|
||||
print()
|
||||
def show_success() -> None:
|
||||
print(" GOOD JOB! YOU MAY BREATHE NOW, BUT DON'T INHALE THE FUMES!\n")
|
||||
|
||||
|
||||
def show_ending():
|
||||
def show_ending() -> None:
|
||||
print(f" YOUR {MAX_LIVES} LIVES ARE USED, BUT YOU WILL BE LONG REMEMBERED FOR")
|
||||
print(" YOUR CONTRIBUTIONS TO THE FIELD OF COMIC BOOK CHEMISTRY.")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
print_with_tab(33, "CHEMIST")
|
||||
print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n")
|
||||
|
||||
print("THE FICTITIOUS CHEMICAL KRYPTOCYANIC ACID CAN ONLY BE")
|
||||
print("DILUTED BY THE RATIO OF 7 PARTS WATER TO 3 PARTS ACID.")
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
def print_lightning_bolt() -> None:
|
||||
|
||||
print("*" * 36)
|
||||
n = 24
|
||||
while n > 16:
|
||||
@@ -17,8 +16,7 @@ def print_lightning_bolt() -> None:
|
||||
print("*" * 36)
|
||||
|
||||
|
||||
def print_solution(n: int) -> None:
|
||||
|
||||
def print_solution(n: float) -> None:
|
||||
print(f"\n{n} plus 3 gives {n + 3}. This Divided by 5 equals {(n + 3) / 5}")
|
||||
print(f"This times 8 gives {((n + 3) / 5) * 8}. If we divide 5 and add 5.")
|
||||
print(
|
||||
@@ -27,20 +25,19 @@ def print_solution(n: int) -> None:
|
||||
)
|
||||
|
||||
|
||||
def Game():
|
||||
def game() -> None:
|
||||
print("\nTake a Number and ADD 3. Now, Divide this number by 5 and")
|
||||
print("multiply by 8. Now, Divide by 5 and add the same. Subtract 1")
|
||||
|
||||
resp = float(input("\nWhat do you have? "))
|
||||
comp_guess = (((resp - 4) * 5) / 8) * 5 - 3
|
||||
resp2 = input(f"\nI bet your number was {comp_guess} was i right(Yes or No)? ")
|
||||
resp2 = input(f"\nI bet your number was {comp_guess} was I right(Yes or No)? ")
|
||||
|
||||
if resp2 == "Yes" or resp2 == "YES" or resp2 == "yes":
|
||||
if resp2.lower() == "yes":
|
||||
print("\nHuh, I Knew I was unbeatable")
|
||||
print("And here is how i did it")
|
||||
print_solution(comp_guess)
|
||||
input("")
|
||||
|
||||
else:
|
||||
resp3 = float(input("\nHUH!! what was you original number? "))
|
||||
|
||||
@@ -52,7 +49,6 @@ def Game():
|
||||
print("Here is how i did it")
|
||||
print_solution(comp_guess)
|
||||
input("")
|
||||
|
||||
else:
|
||||
print("\nSo you think you're so smart, EH?")
|
||||
print("Now, Watch")
|
||||
@@ -60,10 +56,9 @@ def Game():
|
||||
|
||||
resp4 = input("\nNow do you believe me? ")
|
||||
|
||||
if resp4 == "Yes" or resp4 == "YES" or resp4 == "yes":
|
||||
if resp4.lower() == "yes":
|
||||
print("\nOk, Lets play again sometime bye!!!!")
|
||||
input("")
|
||||
|
||||
else:
|
||||
print("\nYOU HAVE MADE ME VERY MAD!!!!!")
|
||||
print("BY THE WRATH OF THE MATHEMATICS AND THE RAGE OF THE GODS")
|
||||
@@ -74,11 +69,10 @@ def Game():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print("I am CHIEF NUMBERS FREEK, The GREAT INDIAN MATH GOD.")
|
||||
play = input("\nAre you ready to take the test you called me out for(Yes or No)? ")
|
||||
if play == "Yes" or play == "YES" or play == "yes":
|
||||
Game()
|
||||
if play.lower() == "yes":
|
||||
game()
|
||||
else:
|
||||
print("Ok, Nevermind. Let me go back to my great slumber, Bye")
|
||||
input("")
|
||||
@@ -125,7 +125,6 @@ def main() -> None:
|
||||
|
||||
keep_playing = True
|
||||
while keep_playing:
|
||||
|
||||
play_game()
|
||||
keep_playing = input("\nAgain (1=Yes, 0=No!) ") == "1"
|
||||
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
|
||||
import math
|
||||
import random
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
def show_welcome():
|
||||
def show_welcome() -> None:
|
||||
# Clear screen. chr(27) is `Esc`, and the control sequence is
|
||||
# initiated by Ctrl+[
|
||||
# `J` is "Erase in Display" and `2J` means clear the entire screen
|
||||
@@ -18,14 +19,14 @@ def show_welcome():
|
||||
print("Creative Computing Morristown, New Jersey\n\n".center(45))
|
||||
|
||||
|
||||
def get_num_charges():
|
||||
def get_num_charges() -> Tuple[int, int]:
|
||||
print("Depth Charge game\n")
|
||||
while True:
|
||||
search_area = input("Dimensions of search area? ")
|
||||
search_area_str = input("Dimensions of search area? ")
|
||||
|
||||
# Make sure the input is an integer
|
||||
try:
|
||||
search_area = int(search_area)
|
||||
search_area = int(search_area_str)
|
||||
break
|
||||
except ValueError:
|
||||
print("Must enter an integer number. Please try again...")
|
||||
@@ -34,7 +35,7 @@ def get_num_charges():
|
||||
return search_area, num_charges
|
||||
|
||||
|
||||
def ask_for_new_game():
|
||||
def ask_for_new_game() -> None:
|
||||
answer = input("Another game (Y or N): ")
|
||||
if answer.lower().strip()[0] == "y":
|
||||
start_new_game()
|
||||
@@ -43,7 +44,7 @@ def ask_for_new_game():
|
||||
exit()
|
||||
|
||||
|
||||
def show_shot_result(shot, location):
|
||||
def show_shot_result(shot, location) -> None:
|
||||
result = "Sonar reports shot was "
|
||||
if shot[1] > location[1]: # y-direction
|
||||
result += "north"
|
||||
@@ -66,17 +67,17 @@ def show_shot_result(shot, location):
|
||||
return
|
||||
|
||||
|
||||
def get_shot_input():
|
||||
def get_shot_input() -> Tuple[int, int, int]:
|
||||
while True:
|
||||
raw_guess = input("Enter coordinates: ")
|
||||
try:
|
||||
x, y, z = raw_guess.split()
|
||||
xyz = raw_guess.split()
|
||||
except ValueError:
|
||||
print("Please enter coordinates separated by spaces")
|
||||
print("Example: 3 2 1")
|
||||
continue
|
||||
try:
|
||||
x, y, z = (int(num) for num in [x, y, z])
|
||||
x, y, z = (int(num) for num in xyz)
|
||||
return x, y, z
|
||||
except ValueError:
|
||||
print("Please enter whole numbers only")
|
||||
@@ -111,7 +112,7 @@ def play_game(search_area, num_charges):
|
||||
ask_for_new_game()
|
||||
|
||||
|
||||
def start_new_game():
|
||||
def start_new_game() -> None:
|
||||
search_area, num_charges = get_num_charges()
|
||||
play_game(search_area, num_charges)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import random
|
||||
from typing import List
|
||||
|
||||
|
||||
def print_intro() -> None:
|
||||
@@ -30,7 +31,7 @@ def print_instructions() -> None:
|
||||
print()
|
||||
|
||||
|
||||
def read_10_numbers():
|
||||
def read_10_numbers() -> List[int]:
|
||||
print("TEN NUMBERS, PLEASE ? ")
|
||||
numbers = []
|
||||
|
||||
@@ -47,7 +48,7 @@ def read_10_numbers():
|
||||
return numbers
|
||||
|
||||
|
||||
def read_continue_choice():
|
||||
def read_continue_choice() -> bool:
|
||||
print("\nDO YOU WANT TO TRY AGAIN (1 FOR YES, 0 FOR NO) ? ")
|
||||
try:
|
||||
choice = int(input())
|
||||
|
||||
@@ -28,7 +28,7 @@ def print_at_column(column: int, words: str) -> None:
|
||||
print(spaces + words)
|
||||
|
||||
|
||||
def show_introduction():
|
||||
def show_introduction() -> None:
|
||||
"""Show the player the introductory message"""
|
||||
print("YOU ARE THE LEADER OF A FRENCH FUR TRADING EXPEDITION IN ")
|
||||
print("1776 LEAVING THE LAKE ONTARIO AREA TO SELL FURS AND GET")
|
||||
@@ -39,7 +39,7 @@ def show_introduction():
|
||||
print("")
|
||||
|
||||
|
||||
def get_fort_choice():
|
||||
def get_fort_choice() -> int:
|
||||
"""Show the player the choices of Fort, get their input, if the
|
||||
input is a valid choice (1,2,3) return it, otherwise keep
|
||||
prompting the user."""
|
||||
|
||||
@@ -1,31 +1,32 @@
|
||||
from random import random, seed
|
||||
|
||||
|
||||
def gen_random():
|
||||
def gen_random() -> int:
|
||||
return int(random() * 5) + 1
|
||||
|
||||
|
||||
def bad_input_850():
|
||||
def bad_input_850() -> None:
|
||||
print("\nHAMURABI: I CANNOT DO WHAT YOU WISH.")
|
||||
print("GET YOURSELF ANOTHER STEWARD!!!!!")
|
||||
|
||||
|
||||
def bad_input_710(S):
|
||||
def bad_input_710(grain_bushels: int) -> None:
|
||||
print("HAMURABI: THINK AGAIN. YOU HAVE ONLY")
|
||||
print(S, "BUSHELS OF GRAIN. NOW THEN,")
|
||||
print(f"{grain_bushels} BUSHELS OF GRAIN. NOW THEN,")
|
||||
|
||||
|
||||
def bad_input_720(A):
|
||||
print("HAMURABI: THINK AGAIN. YOU OWN ONLY", A, "ACRES. NOW THEN,")
|
||||
def bad_input_720(acres: float) -> None:
|
||||
print(f"HAMURABI: THINK AGAIN. YOU OWN ONLY {acres} ACRES. NOW THEN,")
|
||||
|
||||
|
||||
def national_fink():
|
||||
def national_fink() -> None:
|
||||
print("DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY")
|
||||
print("BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE")
|
||||
print("ALSO BEEN DECLARED NATIONAL FINK!!!!")
|
||||
|
||||
|
||||
def b_input(promptstring): # emulate BASIC input. It rejects non-numeric values
|
||||
def b_input(promptstring: str) -> int:
|
||||
"""emulate BASIC input. It rejects non-numeric values"""
|
||||
x = input(promptstring)
|
||||
while x.isalpha():
|
||||
x = input("?REDO FROM START\n? ")
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
#!/usr/bin/env python3
|
||||
# HANGMAN
|
||||
#
|
||||
# Converted from BASIC to Python by Trevor Hobson and Daniel Piron
|
||||
|
||||
"""
|
||||
HANGMAN
|
||||
|
||||
Converted from BASIC to Python by Trevor Hobson and Daniel Piron
|
||||
"""
|
||||
|
||||
import random
|
||||
from typing import List
|
||||
|
||||
|
||||
class Canvas:
|
||||
"""For drawing text-based figures"""
|
||||
|
||||
def __init__(self, width=12, height=12, fill=" "):
|
||||
def __init__(self, width: int = 12, height: int = 12, fill: str = " ") -> None:
|
||||
self._buffer = []
|
||||
for _ in range(height):
|
||||
line = []
|
||||
@@ -19,12 +23,12 @@ class Canvas:
|
||||
|
||||
self.clear()
|
||||
|
||||
def clear(self, fill=" "):
|
||||
def clear(self, fill: str = " ") -> None:
|
||||
for row in self._buffer:
|
||||
for x in range(len(row)):
|
||||
row[x] = fill
|
||||
|
||||
def render(self):
|
||||
def render(self) -> str:
|
||||
lines = []
|
||||
for line in self._buffer:
|
||||
# Joining by the empty string ("") smooshes all of the
|
||||
@@ -32,13 +36,13 @@ class Canvas:
|
||||
lines.append("".join(line))
|
||||
return "\n".join(lines)
|
||||
|
||||
def put(self, s, x, y):
|
||||
def put(self, s: str, x: int, y: int) -> None:
|
||||
# In an effort to avoid distorting the drawn image, only write the
|
||||
# first character of the given string to the buffer.
|
||||
self._buffer[y][x] = s[0]
|
||||
|
||||
|
||||
def init_gallows(canvas):
|
||||
def init_gallows(canvas: Canvas) -> None:
|
||||
for i in range(12):
|
||||
canvas.put("X", 0, i)
|
||||
for i in range(7):
|
||||
@@ -46,7 +50,7 @@ def init_gallows(canvas):
|
||||
canvas.put("X", 6, 1)
|
||||
|
||||
|
||||
def draw_head(canvas):
|
||||
def draw_head(canvas: Canvas) -> None:
|
||||
canvas.put("-", 5, 2)
|
||||
canvas.put("-", 6, 2)
|
||||
canvas.put("-", 7, 2)
|
||||
@@ -59,47 +63,47 @@ def draw_head(canvas):
|
||||
canvas.put("-", 7, 4)
|
||||
|
||||
|
||||
def draw_body(canvas):
|
||||
def draw_body(canvas: Canvas) -> None:
|
||||
for i in range(5, 9, 1):
|
||||
canvas.put("X", 6, i)
|
||||
|
||||
|
||||
def draw_right_arm(canvas):
|
||||
def draw_right_arm(canvas: Canvas) -> None:
|
||||
for i in range(3, 7):
|
||||
canvas.put("\\", i - 1, i)
|
||||
|
||||
|
||||
def draw_left_arm(canvas):
|
||||
def draw_left_arm(canvas: Canvas) -> None:
|
||||
canvas.put("/", 10, 3)
|
||||
canvas.put("/", 9, 4)
|
||||
canvas.put("/", 8, 5)
|
||||
canvas.put("/", 7, 6)
|
||||
|
||||
|
||||
def draw_right_leg(canvas):
|
||||
def draw_right_leg(canvas: Canvas) -> None:
|
||||
canvas.put("/", 5, 9)
|
||||
canvas.put("/", 4, 10)
|
||||
|
||||
|
||||
def draw_left_leg(canvas):
|
||||
def draw_left_leg(canvas: Canvas) -> None:
|
||||
canvas.put("\\", 7, 9)
|
||||
canvas.put("\\", 8, 10)
|
||||
|
||||
|
||||
def draw_left_hand(canvas):
|
||||
def draw_left_hand(canvas: Canvas) -> None:
|
||||
canvas.put("\\", 10, 2)
|
||||
|
||||
|
||||
def draw_right_hand(canvas):
|
||||
def draw_right_hand(canvas: Canvas) -> None:
|
||||
canvas.put("/", 2, 2)
|
||||
|
||||
|
||||
def draw_left_foot(canvas):
|
||||
def draw_left_foot(canvas: Canvas) -> None:
|
||||
canvas.put("\\", 9, 11)
|
||||
canvas.put("-", 10, 11)
|
||||
|
||||
|
||||
def draw_right_foot(canvas):
|
||||
def draw_right_foot(canvas: Canvas) -> None:
|
||||
canvas.put("-", 2, 11)
|
||||
canvas.put("/", 3, 11)
|
||||
|
||||
@@ -172,11 +176,11 @@ words = [
|
||||
]
|
||||
|
||||
|
||||
def play_game(guess_target):
|
||||
def play_game(guess_target: str) -> None:
|
||||
"""Play one round of the game"""
|
||||
wrong_guesses = 0
|
||||
guess_progress = ["-"] * len(guess_target)
|
||||
guess_list = []
|
||||
guess_list: List[str] = []
|
||||
|
||||
gallows = Canvas()
|
||||
init_gallows(gallows)
|
||||
@@ -206,7 +210,7 @@ def play_game(guess_target):
|
||||
]
|
||||
for i in indices:
|
||||
guess_progress[i] = guess_letter
|
||||
if guess_progress == guess_target:
|
||||
if "".join(guess_progress) == guess_target:
|
||||
print("You found the word!")
|
||||
break
|
||||
else:
|
||||
|
||||
@@ -9,6 +9,7 @@ Ported by Dave LeCompte
|
||||
"""
|
||||
|
||||
import time
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
def print_with_tab(space_count: int, msg: str) -> None:
|
||||
@@ -19,7 +20,7 @@ def print_with_tab(space_count: int, msg: str) -> None:
|
||||
print(spaces + msg)
|
||||
|
||||
|
||||
def get_yes_or_no():
|
||||
def get_yes_or_no() -> Tuple[bool, Optional[bool], str]:
|
||||
msg = input()
|
||||
if msg.upper() == "YES":
|
||||
return True, True, msg
|
||||
@@ -58,7 +59,7 @@ def prompt_for_problems(user_name):
|
||||
return problem_type
|
||||
|
||||
|
||||
def prompt_too_much_or_too_little():
|
||||
def prompt_too_much_or_too_little() -> Tuple[bool, Optional[bool]]:
|
||||
answer = input().upper()
|
||||
if answer == "TOO MUCH":
|
||||
return True, True
|
||||
@@ -67,7 +68,7 @@ def prompt_too_much_or_too_little():
|
||||
return False, None
|
||||
|
||||
|
||||
def solve_sex_problem(user_name):
|
||||
def solve_sex_problem(user_name: str) -> None:
|
||||
print("IS YOUR PROBLEM TOO MUCH OR TOO LITTLE?")
|
||||
while True:
|
||||
valid, too_much = prompt_too_much_or_too_little()
|
||||
@@ -85,31 +86,31 @@ def solve_sex_problem(user_name):
|
||||
print("WITH 'TOO MUCH' OR 'TOO LITTLE'. WHICH IS IT?")
|
||||
|
||||
|
||||
def solve_money_problem(user_name):
|
||||
def solve_money_problem(user_name: str) -> None:
|
||||
print(f"SORRY, {user_name}, I'M BROKE TOO. WHY DON'T YOU SELL")
|
||||
print("ENCYCLOPEADIAS OR MARRY SOMEONE RICH OR STOP EATING")
|
||||
print("SO YOU WON'T NEED SO MUCH MONEY?")
|
||||
|
||||
|
||||
def solve_health_problem(user_name):
|
||||
def solve_health_problem(user_name: str) -> None:
|
||||
print(f"MY ADVICE TO YOU {user_name} IS:")
|
||||
print(" 1. TAKE TWO ASPRIN")
|
||||
print(" 2. DRINK PLENTY OF FLUIDS (ORANGE JUICE, NOT BEER!)")
|
||||
print(" 3. GO TO BED (ALONE)")
|
||||
|
||||
|
||||
def solve_job_problem(user_name):
|
||||
def solve_job_problem(user_name: str) -> None:
|
||||
print(f"I CAN SYMPATHIZE WITH YOU {user_name}. I HAVE TO WORK")
|
||||
print("VERY LONG HOURS FOR NO PAY -- AND SOME OF MY BOSSES")
|
||||
print(f"REALLY BEAT ON MY KEYBOARD. MY ADVICE TO YOU, {user_name},")
|
||||
print("IS TO OPEN A RETAIL COMPUTER STORE. IT'S GREAT FUN.")
|
||||
|
||||
|
||||
def alert_unknown_problem_type(user_name, problem_type):
|
||||
def alert_unknown_problem_type(user_name: str, problem_type: str) -> None:
|
||||
print(f"OH, {user_name}, YOUR ANSWER OF {problem_type} IS GREEK TO ME.")
|
||||
|
||||
|
||||
def ask_question_loop(user_name):
|
||||
def ask_question_loop(user_name: str) -> None:
|
||||
while True:
|
||||
problem_type = prompt_for_problems(user_name)
|
||||
if problem_type == "SEX":
|
||||
@@ -137,7 +138,7 @@ def ask_question_loop(user_name):
|
||||
print(f"JUST A SIMPLE 'YES' OR 'NO' PLEASE, {user_name}.")
|
||||
|
||||
|
||||
def ask_for_fee(user_name):
|
||||
def ask_for_fee(user_name: str) -> None:
|
||||
print()
|
||||
print(f"THAT WILL BE $5.00 FOR THE ADVICE, {user_name}.")
|
||||
print("PLEASE LEAVE THE MONEY ON THE TERMINAL.")
|
||||
@@ -166,14 +167,14 @@ def ask_for_fee(user_name):
|
||||
print("PLEASE RESPOND WITH 'YES' or 'NO'.")
|
||||
|
||||
|
||||
def unhappy_goodbye(user_name):
|
||||
def unhappy_goodbye(user_name: str) -> None:
|
||||
print()
|
||||
print(f"TAKE A WALK, {user_name}.")
|
||||
print()
|
||||
print()
|
||||
|
||||
|
||||
def happy_goodbye(user_name):
|
||||
def happy_goodbye(user_name: str) -> None:
|
||||
print(f"NICE MEETING YOU, {user_name}, HAVE A NICE DAY.")
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import math
|
||||
import random
|
||||
import time
|
||||
from typing import List
|
||||
|
||||
|
||||
def basic_print(*zones, **kwargs) -> None:
|
||||
@@ -17,7 +18,7 @@ def basic_print(*zones, **kwargs) -> None:
|
||||
print(" " * identation + line, end=end)
|
||||
|
||||
|
||||
def basic_input(prompt, type_conversion=None):
|
||||
def basic_input(prompt: str, type_conversion=None):
|
||||
"""BASIC INPUT command with optional type conversion"""
|
||||
|
||||
while True:
|
||||
@@ -45,7 +46,7 @@ HORSE_NAMES = [
|
||||
]
|
||||
|
||||
|
||||
def introduction():
|
||||
def introduction() -> None:
|
||||
"""Print the introduction, and optional the instructions"""
|
||||
|
||||
basic_print("HORSERACE", indent=31)
|
||||
@@ -66,7 +67,7 @@ def introduction():
|
||||
basic_print("")
|
||||
|
||||
|
||||
def setup_players():
|
||||
def setup_players() -> List[str]:
|
||||
"""Gather the number of players and their names"""
|
||||
|
||||
# ensure we get an integer value from the user
|
||||
|
||||
@@ -15,9 +15,9 @@ def print_with_tab(space_count: int, msg: str) -> None:
|
||||
print(spaces + msg)
|
||||
|
||||
|
||||
def is_yes_ish(answer):
|
||||
def is_yes_ish(answer: str) -> bool:
|
||||
cleaned = answer.strip().upper()
|
||||
if cleaned == "Y" or cleaned == "YES":
|
||||
if cleaned in ["Y", "YES"]:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ GOOD LUCK. THE FEDERATION IS COUNTING ON YOU.
|
||||
)
|
||||
|
||||
|
||||
def get_yes_or_no():
|
||||
def get_yes_or_no() -> bool:
|
||||
while True:
|
||||
response = input().upper()
|
||||
if response == "YES":
|
||||
@@ -92,7 +92,7 @@ def get_yes_or_no():
|
||||
print("PLEASE TYPE 'YES' OR 'NO'")
|
||||
|
||||
|
||||
def game_over(is_success):
|
||||
def game_over(is_success: bool) -> bool:
|
||||
if is_success:
|
||||
print("YOU HAVE SUCCESSFULLY COMPLETED YOUR MISSION.")
|
||||
else:
|
||||
@@ -103,7 +103,7 @@ def game_over(is_success):
|
||||
return get_yes_or_no()
|
||||
|
||||
|
||||
def play_game():
|
||||
def play_game() -> bool:
|
||||
rom_angle = random.randint(0, 359)
|
||||
rom_distance = random.randint(100, 300)
|
||||
rom_angular_velocity = random.randint(10, 30)
|
||||
@@ -145,10 +145,7 @@ def play_game():
|
||||
|
||||
def main() -> None:
|
||||
print_centered("ORBIT")
|
||||
print_centered("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
print_centered("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n")
|
||||
|
||||
print_instructions()
|
||||
|
||||
|
||||
@@ -192,7 +192,7 @@ if __name__ == "__main__":
|
||||
help = input("\nDO YOU WANT INSTRUCTIONS(YES OR NO)? ")
|
||||
|
||||
# Printing Instruction
|
||||
if help == "YES" or help == "yes" or help == "Yes":
|
||||
if help.lower() == "yes":
|
||||
print_instruction()
|
||||
|
||||
# Initialize Game
|
||||
|
||||
Reference in New Issue
Block a user