Use NamedTuple; Fix camelCase->snake_case

This commit is contained in:
Martin Thoma
2022-04-02 11:21:11 +02:00
parent ac184fec42
commit e17388d072
13 changed files with 167 additions and 178 deletions

View File

@@ -7,16 +7,15 @@ From: BASIC Computer Games (1978)
Python port by Aviyam Fischer, 2022 Python port by Aviyam Fischer, 2022
""" """
from typing import List, Literal, TypeAlias, get_args from typing import List, Literal, NamedTuple, TypeAlias, get_args
Suit: TypeAlias = Literal["\u2665", "\u2666", "\u2663", "\u2660"] Suit: TypeAlias = Literal["\u2665", "\u2666", "\u2663", "\u2660"]
Rank: TypeAlias = Literal[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] Rank: TypeAlias = Literal[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
class Card: class Card(NamedTuple):
def __init__(self, suit: Suit, rank: Rank) -> None: suit: Suit
self.suit = suit rank: Rank
self.rank = rank
def __str__(self) -> str: def __str__(self) -> str:
r = str(self.rank) r = str(self.rank)

View File

@@ -7,11 +7,7 @@ from typing import List, Tuple
class Maze: class Maze:
def __init__( def __init__(self, width: int, length: int) -> None:
self,
width: int,
length: int,
):
assert width >= 2 and length >= 2 assert width >= 2 and length >= 2
used: List[List[int]] = [] used: List[List[int]] = []
walls: List[List[int]] = [] walls: List[List[int]] = []
@@ -117,7 +113,7 @@ def build_maze(width: int, length: int) -> Maze:
else: else:
while True: while True:
if position.col != width - 1: if position.col != width - 1:
position.col = position.col + 1 position.col += 1
elif position.row != length - 1: elif position.row != length - 1:
position.row, position.col = position.row + 1, 0 position.row, position.col = position.row + 1, 0
else: else:

View File

@@ -86,10 +86,10 @@ def print_banner() -> None:
for statement_char in statement: for statement_char in statement:
s = letters[statement_char].copy() s = letters[statement_char].copy()
xStr = character x_str = character
if character == "ALL": if character == "ALL":
xStr = statement_char x_str = statement_char
if xStr == " ": if x_str == " ":
print("\n" * (7 * horizontal)) print("\n" * (7 * horizontal))
else: else:
for u in range(0, 7): for u in range(0, 7):
@@ -103,13 +103,13 @@ def print_banner() -> None:
f[u] = 8 - k f[u] = 8 - k
break break
for _t1 in range(1, horizontal + 1): for _t1 in range(1, horizontal + 1):
line_str = " " * int((63 - 4.5 * vertical) * g1 / len(xStr) + 1) line_str = " " * int((63 - 4.5 * vertical) * g1 / len(x_str) + 1)
for b in range(0, f[u] + 1): for b in range(0, f[u] + 1):
if j[b] == 0: if j[b] == 0:
for _ in range(1, vertical + 1): for _ in range(1, vertical + 1):
line_str = line_str + " " * len(xStr) line_str = line_str + " " * len(x_str)
else: else:
line_str = line_str + xStr * vertical line_str = line_str + x_str * vertical
print(line_str) print(line_str)
print("\n" * (2 * horizontal - 1)) print("\n" * (2 * horizontal - 1))
# print("\n" * 75) # Feed some more paper from the printer # print("\n" * 75) # Feed some more paper from the printer

View File

@@ -64,12 +64,12 @@ def get_params() -> Tuple[int, int, int, StartOptions, WinOptions]:
"""This requests the necessary parameters to play the game. """This requests the necessary parameters to play the game.
Returns a set with the five game parameters: Returns a set with the five game parameters:
pileSize - the starting size of the object pile pile_size - the starting size of the object pile
minSelect - minimum selection that can be made on each turn min_select - minimum selection that can be made on each turn
maxSelect - maximum selection that can be made on each turn max_select - maximum selection that can be made on each turn
startOption - 1 if the computer is first start_option - 1 if the computer is first
or 2 if the player is first or 2 if the player is first
winOption - 1 if the goal is to take the last object win_option - 1 if the goal is to take the last object
or 2 if the goal is to not take the last object or 2 if the goal is to not take the last object
""" """
pile_size = get_pile_size() pile_size = get_pile_size()
@@ -123,7 +123,7 @@ def player_move(
to take and doing some basic validation around that input. Then it to take and doing some basic validation around that input. Then it
checks for any win conditions. checks for any win conditions.
Returns a boolean indicating whether the game is over and the new pileSize.""" Returns a boolean indicating whether the game is over and the new pile_size."""
player_done = False player_done = False
while not player_done: while not player_done:
player_move = int(input("YOUR MOVE ")) player_move = int(input("YOUR MOVE "))
@@ -167,7 +167,7 @@ def computer_move(
win/lose conditions and then calculating how many objects win/lose conditions and then calculating how many objects
the computer will take. the computer will take.
Returns a boolean indicating whether the game is over and the new pileSize.""" Returns a boolean indicating whether the game is over and the new pile_size."""
# First, check for win conditions on this move # First, check for win conditions on this move
# In this case, we win by taking the last object and # In this case, we win by taking the last object and
# the remaining pile is less than max select # the remaining pile is less than max select
@@ -200,7 +200,7 @@ def play_game(
of the win/lose conditions is met. of the win/lose conditions is met.
""" """
game_over = False game_over = False
# playersTurn is a boolean keeping track of whether it's the # players_turn is a boolean keeping track of whether it's the
# player's or computer's turn # player's or computer's turn
players_turn = start_option == StartOptions.PlayerFirst players_turn = start_option == StartOptions.PlayerFirst

View File

@@ -37,7 +37,7 @@ def game() -> None:
print("\nHuh, I Knew I was unbeatable") print("\nHuh, I Knew I was unbeatable")
print("And here is how i did it") print("And here is how i did it")
print_solution(comp_guess) print_solution(comp_guess)
input("") input()
else: else:
resp3 = float(input("\nHUH!! what was you original number? ")) resp3 = float(input("\nHUH!! what was you original number? "))
@@ -48,7 +48,7 @@ def game() -> None:
) )
print("Here is how i did it") print("Here is how i did it")
print_solution(comp_guess) print_solution(comp_guess)
input("") input()
else: else:
print("\nSo you think you're so smart, EH?") print("\nSo you think you're so smart, EH?")
print("Now, Watch") print("Now, Watch")
@@ -58,14 +58,14 @@ def game() -> None:
if resp4.lower() == "yes": if resp4.lower() == "yes":
print("\nOk, Lets play again sometime bye!!!!") print("\nOk, Lets play again sometime bye!!!!")
input("") input()
else: else:
print("\nYOU HAVE MADE ME VERY MAD!!!!!") print("\nYOU HAVE MADE ME VERY MAD!!!!!")
print("BY THE WRATH OF THE MATHEMATICS AND THE RAGE OF THE GODS") print("BY THE WRATH OF THE MATHEMATICS AND THE RAGE OF THE GODS")
print("THERE SHALL BE LIGHTNING!!!!!!!") print("THERE SHALL BE LIGHTNING!!!!!!!")
print_lightning_bolt() print_lightning_bolt()
print("\nI Hope you believe me now, for your own sake") print("\nI Hope you believe me now, for your own sake")
input("") input()
if __name__ == "__main__": if __name__ == "__main__":
@@ -75,4 +75,4 @@ if __name__ == "__main__":
game() game()
else: else:
print("Ok, Nevermind. Let me go back to my great slumber, Bye") print("Ok, Nevermind. Let me go back to my great slumber, Bye")
input("") input()

View File

@@ -8,7 +8,7 @@ Ported by Dave LeCompte
def print_diamond(begin_width, end_width, step, width, count) -> None: def print_diamond(begin_width, end_width, step, width, count) -> None:
edgeString = "CC" edge_string = "CC"
fill = "!" fill = "!"
n = begin_width n = begin_width
@@ -16,10 +16,10 @@ def print_diamond(begin_width, end_width, step, width, count) -> None:
line_buffer = " " * ((width - n) // 2) line_buffer = " " * ((width - n) // 2)
for across in range(count): for across in range(count):
for a in range(n): for a in range(n):
if a >= len(edgeString): if a >= len(edge_string):
line_buffer += fill line_buffer += fill
else: else:
line_buffer += edgeString[a] line_buffer += edge_string[a]
line_buffer += " " * ( line_buffer += " " * (
(width * (across + 1) + (width - n) // 2) - len(line_buffer) (width * (across + 1) + (width - n) // 2) - len(line_buffer)
) )

View File

@@ -129,8 +129,8 @@ def clear_console() -> None:
class Point(NamedTuple): class Point(NamedTuple):
X: int x: int
Y: int y: int
class GameObjType(enum.Enum): class GameObjType(enum.Enum):
@@ -183,14 +183,14 @@ class HoleGeometry(NamedTuple):
@dataclass @dataclass
class Plot: class Plot:
X: int x: int
Y: int y: int
Offline: int offline: int
def get_distance(pt1: Point, pt2: Point) -> float: def get_distance(pt1: Point, pt2: Point) -> float:
"""distance between 2 points""" """distance between 2 points"""
return math.sqrt(math.pow((pt2.X - pt1.X), 2) + math.pow((pt2.Y - pt1.Y), 2)) return math.sqrt(math.pow((pt2.x - pt1.x), 2) + math.pow((pt2.y - pt1.y), 2))
def is_in_rectangle(pt: CircleGameObj, rect: RectGameObj) -> bool: def is_in_rectangle(pt: CircleGameObj, rect: RectGameObj) -> bool:
@@ -392,9 +392,9 @@ ace = 0b10000000000000
class Golf: class Golf:
BALL: Ball ball: Ball
HOLE_NUM: int = 0 hole_num: int = 0
STROKE_NUM: int = 0 stroke_num: int = 0
handicap: int = 0 handicap: int = 0
player_difficulty: int = 0 player_difficulty: int = 0
hole_geometry: HoleGeometry hole_geometry: HoleGeometry
@@ -491,10 +491,10 @@ class Golf:
self.new_hole() self.new_hole()
def new_hole(self) -> None: def new_hole(self) -> None:
self.HOLE_NUM += 1 self.hole_num += 1
self.STROKE_NUM = 0 self.stroke_num = 0
info: HoleInfo = CourseInfo[self.HOLE_NUM] info: HoleInfo = CourseInfo[self.hole_num]
yards: int = info.yards yards: int = info.yards
# from tee to cup # from tee to cup
@@ -517,19 +517,19 @@ class Golf:
GameObjType.ROUGH, GameObjType.ROUGH,
) )
self.BALL = Ball(0, yards, 0, GameObjType.BALL) self.ball = Ball(0, yards, 0, GameObjType.BALL)
self.score_card_start_new_hole() self.score_card_start_new_hole()
self.hole_geometry = HoleGeometry(cup, green, fairway, rough, info.hazards) self.hole_geometry = HoleGeometry(cup, green, fairway, rough, info.hazards)
print(f" |> {self.HOLE_NUM}") print(f" |> {self.hole_num}")
print(" | ") print(" | ")
print(" | ") print(" | ")
print(" ^^^^^^^^^^^^^^^") print(" ^^^^^^^^^^^^^^^")
print( print(
f"Hole #{self.HOLE_NUM}. You are at the tee. Distance {info.yards} yards, par {info.par}." f"Hole #{self.hole_num}. You are at the tee. Distance {info.yards} yards, par {info.par}."
) )
print(info.description) print(info.description)
@@ -560,8 +560,8 @@ class Golf:
def tee_up(self) -> None: def tee_up(self) -> None:
# on the green? automatically select putter # on the green? automatically select putter
# otherwise Ask club and swing strength # otherwise Ask club and swing strength
if self.is_on_green(self.BALL) and not self.is_in_hazard( if self.is_on_green(self.ball) and not self.is_in_hazard(
self.BALL, GameObjType.SAND self.ball, GameObjType.SAND
): ):
self.putt = 10 self.putt = 10
print("[PUTTER: average 10 yards]") print("[PUTTER: average 10 yards]")
@@ -579,13 +579,13 @@ class Golf:
else: else:
self.ask("What club do you choose? (1-10)", 1, 10, self.ask_gauge) self.ask("What club do you choose? (1-10)", 1, 10, self.ask_gauge)
def stroke(self, clubAmt: float, clubIndex: int) -> None: def stroke(self, club_amt: float, club_index: int) -> None:
self.STROKE_NUM += 1 self.stroke_num += 1
flags = 0b000000000000 flags = 0b000000000000
# fore! only when driving # fore! only when driving
if (self.STROKE_NUM == 1) and (clubAmt > 210) and odds(30): if (self.stroke_num == 1) and (club_amt > 210) and odds(30):
print('"...Fore !"') print('"...Fore !"')
# dub # dub
@@ -596,37 +596,37 @@ class Golf:
# if you're in the rough, or sand, you really should be using a wedge # if you're in the rough, or sand, you really should be using a wedge
if ( if (
( (
self.is_in_rough(self.BALL) self.is_in_rough(self.ball)
or self.is_in_hazard(self.BALL, GameObjType.SAND) or self.is_in_hazard(self.ball, GameObjType.SAND)
) )
and not (clubIndex == 8 or clubIndex == 9) and not (club_index == 8 or club_index == 9)
and odds(40) and odds(40)
): ):
flags |= dub flags |= dub
# trap difficulty # trap difficulty
if ( if (
self.is_in_hazard(self.BALL, GameObjType.SAND) self.is_in_hazard(self.ball, GameObjType.SAND)
and self.player_difficulty == 4 and self.player_difficulty == 4
) and odds(20): ) and odds(20):
flags |= dub flags |= dub
# hook/slice # hook/slice
# There's 10% chance of a hook or slice # There's 10% chance of a hook or slice
# if it's a known playerDifficulty then increase chance to 30% # if it's a known player_difficulty then increase chance to 30%
# if it's a putt & putting is a playerDifficulty increase to 30% # if it's a putt & putting is a player_difficulty increase to 30%
randHookSlice: bool rand_hook_slice: bool
if ( if (
self.player_difficulty == 1 self.player_difficulty == 1
or self.player_difficulty == 2 or self.player_difficulty == 2
or (self.player_difficulty == 5 and self.is_on_green(self.BALL)) or (self.player_difficulty == 5 and self.is_on_green(self.ball))
): ):
randHookSlice = odds(30) rand_hook_slice = odds(30)
else: else:
randHookSlice = odds(10) rand_hook_slice = odds(10)
if randHookSlice: if rand_hook_slice:
if self.player_difficulty == 1: if self.player_difficulty == 1:
if odds(80): if odds(80):
flags |= hook flags |= hook
@@ -650,7 +650,7 @@ class Golf:
# ace # ace
# there's a 10% chance of an Ace on a par 3 # there's a 10% chance of an Ace on a par 3
if CourseInfo[self.HOLE_NUM].par == 3 and odds(10) and self.STROKE_NUM == 1: if CourseInfo[self.hole_num].par == 3 and odds(10) and self.stroke_num == 1:
flags |= ace flags |= ace
# distance: # distance:
@@ -659,29 +659,29 @@ class Golf:
# If handicap is > 15, there's a 25% chance of reaching club average, # If handicap is > 15, there's a 25% chance of reaching club average,
# and 75% chance of falling short # and 75% chance of falling short
# The greater the handicap, the more the ball falls short # The greater the handicap, the more the ball falls short
# If poor distance is a known playerDifficulty, then reduce distance by 10% # If poor distance is a known player_difficulty, then reduce distance by 10%
distance: float distance: float
rnd = random.randint(1, 101) rnd = random.randint(1, 101)
if self.handicap < 15: if self.handicap < 15:
if rnd <= 25: if rnd <= 25:
distance = clubAmt - (clubAmt * (self.handicap / 100.0)) distance = club_amt - (club_amt * (self.handicap / 100.0))
elif rnd > 25 and rnd <= 75: elif rnd > 25 and rnd <= 75:
distance = clubAmt distance = club_amt
else: else:
distance = clubAmt + (clubAmt * 0.10) distance = club_amt + (club_amt * 0.10)
else: else:
if rnd <= 75: if rnd <= 75:
distance = clubAmt - (clubAmt * (self.handicap / 100.0)) distance = club_amt - (club_amt * (self.handicap / 100.0))
else: else:
distance = clubAmt distance = club_amt
if self.player_difficulty == 3 and odds(80): # poor distance if self.player_difficulty == 3 and odds(80): # poor distance
distance = distance * 0.80 distance = distance * 0.80
if (flags & luck) == luck: if (flags & luck) == luck:
distance = clubAmt distance = club_amt
# angle # angle
# For all strokes, there's a possible "drift" of 4 degrees # For all strokes, there's a possible "drift" of 4 degrees
@@ -695,43 +695,45 @@ class Golf:
if (flags & luck) == luck: if (flags & luck) == luck:
angle = 0 angle = 0
plot = self.plot_ball(self.BALL, distance, angle) plot = self.plot_ball(self.ball, distance, angle)
# calculate a new location # calculate a new location
if (flags & luck) == luck and plot.Y > 0: if (flags & luck) == luck and plot.y > 0:
plot.Y = 2 plot.y = 2
flags = self.find_ball( flags = self.find_ball(
Ball(plot.X, plot.Y, plot.Offline, GameObjType.BALL), flags Ball(plot.x, plot.y, plot.offline, GameObjType.BALL), flags
) )
self.interpret_results(plot, flags) self.interpret_results(plot, flags)
def plot_ball(self, ball: Ball, strokeDistance: float, degreesOff: float) -> Plot: def plot_ball(self, ball: Ball, stroke_distance: float, degrees_off: float) -> Plot:
cupVector = Point(0, -1) cup_vector = Point(0, -1)
radFromCup = math.atan2(ball.Y, ball.X) - math.atan2(cupVector.Y, cupVector.X) rad_from_cup = math.atan2(ball.Y, ball.X) - math.atan2(
radFromBall = radFromCup - math.pi cup_vector.y, cup_vector.x
)
rad_from_ball = rad_from_cup - math.pi
hypotenuse = strokeDistance hypotenuse = stroke_distance
adjacent = math.cos(radFromBall + to_radians(degreesOff)) * hypotenuse adjacent = math.cos(rad_from_ball + to_radians(degrees_off)) * hypotenuse
opposite = math.sqrt(math.pow(hypotenuse, 2) - math.pow(adjacent, 2)) opposite = math.sqrt(math.pow(hypotenuse, 2) - math.pow(adjacent, 2))
newPos: Point new_pos: Point
if to_degrees_360(radFromBall + to_radians(degreesOff)) > 180: if to_degrees_360(rad_from_ball + to_radians(degrees_off)) > 180:
newPos = Point(int(ball.X - opposite), int(ball.Y - adjacent)) new_pos = Point(int(ball.X - opposite), int(ball.Y - adjacent))
else: else:
newPos = Point(int(ball.X + opposite), int(ball.Y - adjacent)) new_pos = Point(int(ball.X + opposite), int(ball.Y - adjacent))
return Plot(newPos.X, newPos.Y, int(opposite)) return Plot(new_pos.x, new_pos.y, int(opposite))
def interpret_results(self, plot: Plot, flags: int) -> None: def interpret_results(self, plot: Plot, flags: int) -> None:
cupDistance: int = int( cup_distance: int = int(
get_distance( get_distance(
Point(plot.X, plot.Y), Point(plot.x, plot.y),
Point(self.hole_geometry.cup.X, self.hole_geometry.cup.Y), Point(self.hole_geometry.cup.X, self.hole_geometry.cup.Y),
) )
) )
travelDistance: int = int( travel_distance: int = int(
get_distance(Point(plot.X, plot.Y), Point(self.BALL.X, self.BALL.Y)) get_distance(Point(plot.x, plot.y), Point(self.ball.X, self.ball.Y))
) )
print(" ") print(" ")
@@ -744,7 +746,7 @@ class Golf:
if (flags & in_trees) == in_trees: if (flags & in_trees) == in_trees:
print("Your ball is lost in the trees. Take a penalty stroke.") print("Your ball is lost in the trees. Take a penalty stroke.")
self.score_card_record_stroke(self.BALL) self.score_card_record_stroke(self.ball)
self.tee_up() self.tee_up()
return return
@@ -754,19 +756,19 @@ class Golf:
else: else:
msg = "Your ball is lost in the water." msg = "Your ball is lost in the water."
print(msg + " Take a penalty stroke.") print(msg + " Take a penalty stroke.")
self.score_card_record_stroke(self.BALL) self.score_card_record_stroke(self.ball)
self.tee_up() self.tee_up()
return return
if (flags & out_of_bounds) == out_of_bounds: if (flags & out_of_bounds) == out_of_bounds:
print("Out of bounds. Take a penalty stroke.") print("Out of bounds. Take a penalty stroke.")
self.score_card_record_stroke(self.BALL) self.score_card_record_stroke(self.ball)
self.tee_up() self.tee_up()
return return
if (flags & dub) == dub: if (flags & dub) == dub:
print("You dubbed it.") print("You dubbed it.")
self.score_card_record_stroke(self.BALL) self.score_card_record_stroke(self.ball)
self.tee_up() self.tee_up()
return return
@@ -776,7 +778,7 @@ class Golf:
else: else:
msg = "It's in!" msg = "It's in!"
print(msg) print(msg)
self.score_card_record_stroke(Ball(plot.X, plot.Y, 0, GameObjType.BALL)) self.score_card_record_stroke(Ball(plot.x, plot.y, 0, GameObjType.BALL))
self.report_current_score() self.report_current_score()
return return
@@ -785,22 +787,22 @@ class Golf:
bad = "badly" bad = "badly"
else: else:
bad = "" bad = ""
print(f"You sliced{bad}: {plot.Offline} yards offline.") print(f"You sliced{bad}: {plot.offline} yards offline.")
if ((flags & hook) == hook) and not ((flags & on_green) == on_green): if ((flags & hook) == hook) and not ((flags & on_green) == on_green):
if (flags & out_of_bounds) == out_of_bounds: if (flags & out_of_bounds) == out_of_bounds:
bad = "badly" bad = "badly"
else: else:
bad = "" bad = ""
print(f"You hooked{bad}: {plot.Offline} yards offline.") print(f"You hooked{bad}: {plot.offline} yards offline.")
if self.STROKE_NUM > 1: if self.stroke_num > 1:
prevBall = self.score_card_get_previous_stroke() prev_ball = self.score_card_get_previous_stroke()
d1 = get_distance( d1 = get_distance(
Point(prevBall.X, prevBall.Y), Point(prev_ball.X, prev_ball.Y),
Point(self.hole_geometry.cup.X, self.hole_geometry.cup.Y), Point(self.hole_geometry.cup.X, self.hole_geometry.cup.Y),
) )
d2 = cupDistance d2 = cup_distance
if d2 > d1: if d2 > d1:
print("Too much club.") print("Too much club.")
@@ -811,55 +813,55 @@ class Golf:
print("You're in a sand trap.") print("You're in a sand trap.")
if (flags & on_green) == on_green: if (flags & on_green) == on_green:
if cupDistance < 4: if cup_distance < 4:
pd = str(cupDistance * 3) + " feet" pd = str(cup_distance * 3) + " feet"
else: else:
pd = f"{cupDistance} yards" pd = f"{cup_distance} yards"
print(f"You're on the green. It's {pd} from the pin.") print(f"You're on the green. It's {pd} from the pin.")
if ((flags & on_fairway) == on_fairway) or ((flags & in_rough) == in_rough): if ((flags & on_fairway) == on_fairway) or ((flags & in_rough) == in_rough):
print( print(
f"Shot went {travelDistance} yards. " f"Shot went {travel_distance} yards. "
f"It's {cupDistance} yards from the cup." f"It's {cup_distance} yards from the cup."
) )
self.score_card_record_stroke(Ball(plot.X, plot.Y, 0, GameObjType.BALL)) self.score_card_record_stroke(Ball(plot.x, plot.y, 0, GameObjType.BALL))
self.BALL = Ball(plot.X, plot.Y, 0, GameObjType.BALL) self.ball = Ball(plot.x, plot.y, 0, GameObjType.BALL)
self.tee_up() self.tee_up()
def report_current_score(self) -> None: def report_current_score(self) -> None:
par = CourseInfo[self.HOLE_NUM].par par = CourseInfo[self.hole_num].par
if len(self.score_card[self.HOLE_NUM]) == par + 1: if len(self.score_card[self.hole_num]) == par + 1:
print("A bogey. One above par.") print("A bogey. One above par.")
if len(self.score_card[self.HOLE_NUM]) == par: if len(self.score_card[self.hole_num]) == par:
print("Par. Nice.") print("Par. Nice.")
if len(self.score_card[self.HOLE_NUM]) == (par - 1): if len(self.score_card[self.hole_num]) == (par - 1):
print("A birdie! One below par.") print("A birdie! One below par.")
if len(self.score_card[self.HOLE_NUM]) == (par - 2): if len(self.score_card[self.hole_num]) == (par - 2):
print("An Eagle! Two below par.") print("An Eagle! Two below par.")
if len(self.score_card[self.HOLE_NUM]) == (par - 3): if len(self.score_card[self.hole_num]) == (par - 3):
print("Double Eagle! Unbelievable.") print("Double Eagle! Unbelievable.")
totalPar: int = 0 total_par: int = 0
for i in range(1, self.HOLE_NUM + 1): for i in range(1, self.hole_num + 1):
totalPar += CourseInfo[i].par total_par += CourseInfo[i].par
print(" ") print(" ")
print("-----------------------------------------------------") print("-----------------------------------------------------")
if self.HOLE_NUM > 1: if self.hole_num > 1:
hole_str = "holes" hole_str = "holes"
else: else:
hole_str = "hole" hole_str = "hole"
print( print(
f" Total par for {self.HOLE_NUM} {hole_str} is: {totalPar}. " f" Total par for {self.hole_num} {hole_str} is: {total_par}. "
f"Your total is: {self.score_card_get_total()}." f"Your total is: {self.score_card_get_total()}."
) )
print("-----------------------------------------------------") print("-----------------------------------------------------")
print(" ") print(" ")
if self.HOLE_NUM == 18: if self.hole_num == 18:
self.game_over() self.game_over()
else: else:
time.sleep(2) time.sleep(2)
@@ -930,10 +932,10 @@ class Golf:
def score_card_record_stroke(self, ball: Ball) -> None: def score_card_record_stroke(self, ball: Ball) -> None:
clone = Ball(ball.X, ball.Y, 0, GameObjType.BALL) clone = Ball(ball.X, ball.Y, 0, GameObjType.BALL)
self.score_card[self.HOLE_NUM].append(clone) self.score_card[self.hole_num].append(clone)
def score_card_get_previous_stroke(self) -> Ball: def score_card_get_previous_stroke(self) -> Ball:
return self.score_card[self.HOLE_NUM][len(self.score_card[self.HOLE_NUM]) - 1] return self.score_card[self.hole_num][len(self.score_card[self.hole_num]) - 1]
def score_card_get_total(self) -> int: def score_card_get_total(self) -> int:
total: int = 0 total: int = 0

View File

@@ -62,10 +62,10 @@ def initialize_board(n: int) -> List[List[int]]:
# Initialize the board # Initialize the board
board = [] board = []
for _x in range(n): for _x in range(n):
subA = [] sub_a = []
for _y in range(n): for _y in range(n):
subA.append(0) sub_a.append(0)
board.append(subA) board.append(sub_a)
return board return board

View File

@@ -43,8 +43,8 @@ def play_game() -> None:
print("GOOD JOB !!!!!") print("GOOD JOB !!!!!")
if BELLS_ON_SUCCESS: if BELLS_ON_SUCCESS:
bellStr = chr(7) * 15 bell_str = chr(7) * 15
print(bellStr) print(bell_str)
print() print()
print("LET'S PLAY AGAIN.....") print("LET'S PLAY AGAIN.....")

View File

@@ -6,18 +6,17 @@ A children's literature quiz
Ported by Dave LeCompte Ported by Dave LeCompte
""" """
from typing import List, NamedTuple
PAGE_WIDTH = 64 PAGE_WIDTH = 64
class Question: class Question(NamedTuple):
def __init__( question: str
self, question, answer_list, correct_number, incorrect_message, correct_message answer_list: List[str]
): correct_number: int
self.question = question incorrect_message: str
self.answer_list = answer_list correct_message: str
self.correct_number = correct_number
self.incorrect_message = incorrect_message
self.correct_message = correct_message
def ask(self) -> bool: def ask(self) -> bool:
print(self.question) print(self.question)
@@ -69,7 +68,6 @@ questions = [
def print_centered(msg: str) -> None: def print_centered(msg: str) -> None:
spaces = " " * ((64 - len(msg)) // 2) spaces = " " * ((64 - len(msg)) // 2)
print(spaces + msg) print(spaces + msg)

View File

@@ -7,6 +7,7 @@ Ported by Dave LeCompte
""" """
import math import math
from dataclasses import dataclass
from typing import Any, NamedTuple from typing import Any, NamedTuple
PAGE_WIDTH = 64 PAGE_WIDTH = 64
@@ -122,23 +123,15 @@ class SimulationClock:
self.time_until_next_prompt -= delta_t self.time_until_next_prompt -= delta_t
@dataclass
class Capsule: class Capsule:
def __init__( altitude: float = 120 # in miles above the surface
self, velocity: float = 1 # downward
altitude: float = 120, m: float = 33000 # mass_with_fuel
velocity: float = 1, n: float = 16500 # mass_without_fuel
mass_with_fuel: float = 33000, g: float = 1e-3
mass_without_fuel: float = 16500, z: float = 1.8
g: float = 1e-3, fuel_per_second: float = 0
z: float = 1.8,
) -> None:
self.a = altitude # in miles above the surface
self.v = velocity # downward
self.m = mass_with_fuel
self.n = mass_without_fuel
self.g = g
self.z = z
self.fuel_per_second: float = 0
def remaining_fuel(self) -> float: def remaining_fuel(self) -> float:
return self.m - self.n return self.m - self.n
@@ -151,8 +144,8 @@ class Capsule:
) -> None: ) -> None:
sim_clock.advance(delta_t) sim_clock.advance(delta_t)
self.m = self.m - delta_t * self.fuel_per_second self.m = self.m - delta_t * self.fuel_per_second
self.a = new_state.altitude self.altitude = new_state.altitude
self.v = new_state.velocity self.velocity = new_state.velocity
def fuel_time_remaining(self) -> float: def fuel_time_remaining(self) -> float:
# extrapolates out how many seconds we have at the current fuel burn rate # extrapolates out how many seconds we have at the current fuel burn rate
@@ -166,16 +159,16 @@ class Capsule:
# new velocity # new velocity
new_velocity = ( new_velocity = (
self.v self.velocity
+ self.g * delta_t + self.g * delta_t
+ self.z * (-q - q**2 / 2 - q**3 / 3 - q**4 / 4 - q**5 / 5) + self.z * (-q - q**2 / 2 - q**3 / 3 - q**4 / 4 - q**5 / 5)
) )
# new altitude # new altitude
new_altitude = ( new_altitude = (
self.a self.altitude
- self.g * delta_t**2 / 2 - self.g * delta_t**2 / 2
- self.v * delta_t - self.velocity * delta_t
+ self.z + self.z
* delta_t * delta_t
* (q / 2 + q**2 / 6 + q**3 / 12 + q**4 / 20 + q**5 / 30) * (q / 2 + q**2 / 6 + q**3 / 12 + q**4 / 20 + q**5 / 30)
@@ -185,9 +178,9 @@ class Capsule:
def make_state_display_string(self, sim_clock: SimulationClock) -> str: def make_state_display_string(self, sim_clock: SimulationClock) -> str:
seconds = sim_clock.elapsed_time seconds = sim_clock.elapsed_time
miles = int(self.a) miles = int(self.altitude)
feet = int(5280 * (self.a - miles)) feet = int(5280 * (self.altitude - miles))
velocity = int(3600 * self.v) velocity = int(3600 * self.velocity)
fuel = int(self.remaining_fuel()) fuel = int(self.remaining_fuel())
burn_rate = " ? " burn_rate = " ? "
@@ -203,7 +196,7 @@ class Capsule:
def show_landing(sim_clock: SimulationClock, capsule: Capsule) -> None: def show_landing(sim_clock: SimulationClock, capsule: Capsule) -> None:
w = 3600 * capsule.v w = 3600 * capsule.velocity
print( print(
f"ON MOON AT {sim_clock.elapsed_time:.2f} SECONDS - IMPACT VELOCITY {w:.2f} MPH" f"ON MOON AT {sim_clock.elapsed_time:.2f} SECONDS - IMPACT VELOCITY {w:.2f} MPH"
) )
@@ -223,9 +216,10 @@ def show_landing(sim_clock: SimulationClock, capsule: Capsule) -> None:
def show_out_of_fuel(sim_clock: SimulationClock, capsule: Capsule) -> None: def show_out_of_fuel(sim_clock: SimulationClock, capsule: Capsule) -> None:
print(f"FUEL OUT AT {sim_clock.elapsed_time} SECONDS") print(f"FUEL OUT AT {sim_clock.elapsed_time} SECONDS")
delta_t = ( delta_t = (
-capsule.v + math.sqrt(capsule.v**2 + 2 * capsule.a * capsule.g) -capsule.velocity
+ math.sqrt(capsule.velocity**2 + 2 * capsule.altitude * capsule.g)
) / capsule.g ) / capsule.g
capsule.v += capsule.g * delta_t capsule.velocity += capsule.g * delta_t
sim_clock.advance(delta_t) sim_clock.advance(delta_t)
show_landing(sim_clock, capsule) show_landing(sim_clock, capsule)
@@ -243,15 +237,15 @@ def process_final_tick(
return return
# line 35 # line 35
average_vel = ( average_vel = (
capsule.v capsule.velocity
+ math.sqrt( + math.sqrt(
capsule.v**2 capsule.velocity**2
+ 2 + 2
* capsule.a * capsule.altitude
* (capsule.g - capsule.z * capsule.fuel_per_second / capsule.m) * (capsule.g - capsule.z * capsule.fuel_per_second / capsule.m)
) )
) / 2 ) / 2
delta_t = capsule.a / average_vel delta_t = capsule.altitude / average_vel
new_state = capsule.predict_motion(delta_t) new_state = capsule.predict_motion(delta_t)
capsule.update_state(sim_clock, delta_t, new_state) capsule.update_state(sim_clock, delta_t, new_state)
@@ -269,11 +263,11 @@ def handle_flyaway(sim_clock: SimulationClock, capsule: Capsule) -> bool:
w = (1 - capsule.m * capsule.g / (capsule.z * capsule.fuel_per_second)) / 2 w = (1 - capsule.m * capsule.g / (capsule.z * capsule.fuel_per_second)) / 2
delta_t = ( delta_t = (
capsule.m capsule.m
* capsule.v * capsule.velocity
/ ( / (
capsule.z capsule.z
* capsule.fuel_per_second * capsule.fuel_per_second
* math.sqrt(w**2 + capsule.v / capsule.z) * math.sqrt(w**2 + capsule.velocity / capsule.z)
) )
) + 0.05 ) + 0.05
@@ -285,7 +279,7 @@ def handle_flyaway(sim_clock: SimulationClock, capsule: Capsule) -> bool:
capsule.update_state(sim_clock, delta_t, new_state) capsule.update_state(sim_clock, delta_t, new_state)
if (new_state.velocity > 0) or (capsule.v <= 0): if (new_state.velocity > 0) or (capsule.velocity <= 0):
# return to normal sim # return to normal sim
return False return False
@@ -329,7 +323,7 @@ def run_simulation() -> None:
process_final_tick(delta_t, sim_clock, capsule) process_final_tick(delta_t, sim_clock, capsule)
return return
if capsule.v > 0 and new_state.velocity < 0: if capsule.velocity > 0 and new_state.velocity < 0:
# moving away from the moon # moving away from the moon
landed = handle_flyaway(sim_clock, capsule) landed = handle_flyaway(sim_clock, capsule)

View File

@@ -75,8 +75,8 @@ def query_bets() -> Tuple[List[int], List[int]]:
for i in range(bet_count): for i in range(bet_count):
while bet_ids[i] == -1: while bet_ids[i] == -1:
try: try:
inString = input("NUMBER " + str(i + 1) + "? ").split(",") in_string = input("NUMBER " + str(i + 1) + "? ").split(",")
id_, val = int(inString[0]), int(inString[1]) id_, val = int(in_string[0]), int(in_string[1])
# check other bet_IDs # check other bet_IDs
for j in range(i): for j in range(i):

View File

@@ -356,7 +356,7 @@ class Qubit:
break break
print("Incorrect answer. Please type 'yes' or 'no'.") print("Incorrect answer. Please type 'yes' or 'no'.")
skipHuman = s[0] in "nN" skip_human = s[0] in "nN"
move_text = [ move_text = [
"Machine moves to", "Machine moves to",
@@ -368,9 +368,9 @@ class Qubit:
] ]
while True: while True:
if not skipHuman and not self.human_move(board): if not skip_human and not self.human_move(board):
break break
skipHuman = False skip_human = False
m = board.machine_move() m = board.machine_move()
assert m is not None assert m is not None