mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-26 20:54:07 -08:00
MAINT: Apply pre-commit
Remove byte-order-marker pre-commit check as there would be many adjustments necessary
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import random
|
||||
import math
|
||||
import random
|
||||
import time
|
||||
|
||||
|
||||
def basic_print(*zones, **kwargs):
|
||||
"""Simulates the PRINT command from BASIC to some degree.
|
||||
Supports `printing zones` if given multiple arguments."""
|
||||
@@ -10,7 +11,7 @@ def basic_print(*zones, **kwargs):
|
||||
if len(zones) == 1:
|
||||
line = str(zones[0])
|
||||
else:
|
||||
line = "".join(["{:<14}".format(str(zone)) for zone in zones])
|
||||
line = "".join([f"{str(zone):<14}" for zone in zones])
|
||||
identation = kwargs.get("indent", 0)
|
||||
end = kwargs.get("end", "\n")
|
||||
print(" " * identation + line, end=end)
|
||||
@@ -40,7 +41,7 @@ HORSE_NAMES = [
|
||||
"JOLLY",
|
||||
"HORSE",
|
||||
"JELLY DO NOT",
|
||||
"MIDNIGHT"
|
||||
"MIDNIGHT",
|
||||
]
|
||||
|
||||
|
||||
@@ -88,7 +89,7 @@ def setup_horses():
|
||||
|
||||
# rounding odds to two decimals for nicer output,
|
||||
# this is not in the origin implementation
|
||||
return [round(total/odd, 2) for odd in odds]
|
||||
return [round(total / odd, 2) for odd in odds]
|
||||
|
||||
|
||||
def print_horse_odds(odds):
|
||||
@@ -117,7 +118,7 @@ def get_bets(player_names):
|
||||
basic_print(" YOU CAN'T DO THAT!")
|
||||
amount = None
|
||||
bets.append((horse, amount))
|
||||
|
||||
|
||||
basic_print("")
|
||||
|
||||
return bets
|
||||
@@ -127,7 +128,7 @@ def get_distance(odd):
|
||||
"""Advances a horse during one step of the racing simulation.
|
||||
The amount travelled is random, but scaled by the odds of the horse"""
|
||||
|
||||
d = random.randrange(1,100)
|
||||
d = random.randrange(1, 100)
|
||||
s = math.ceil(odd)
|
||||
if d < 10:
|
||||
return 1
|
||||
@@ -160,7 +161,7 @@ def print_race_state(total_distance, race_pos):
|
||||
# race_pos is stored by last to first horse in the race.
|
||||
# we get the next horse we need to print out
|
||||
next_pos = next(race_pos_iter)
|
||||
|
||||
|
||||
# start line
|
||||
basic_print("XXXXSTARTXXXX")
|
||||
|
||||
@@ -195,7 +196,7 @@ def simulate_race(odds):
|
||||
# e.g. race_pos[0] => last horse
|
||||
# race_pos[-1] => winning horse
|
||||
race_pos = list(range(num_horses))
|
||||
|
||||
|
||||
basic_print("\n1 2 3 4 5 6 7 8")
|
||||
|
||||
while True:
|
||||
@@ -205,14 +206,14 @@ def simulate_race(odds):
|
||||
total_distance[i] += get_distance(odds[i])
|
||||
|
||||
# bubble sort race_pos based on total distance travelled
|
||||
# in the original implementation, race_pos is reset for each
|
||||
# in the original implementation, race_pos is reset for each
|
||||
# simulation step, so we keep this behaviour here
|
||||
race_pos = list(range(num_horses))
|
||||
for l in range(num_horses):
|
||||
for i in range(num_horses-1-l):
|
||||
if total_distance[race_pos[i]] < total_distance[race_pos[i+1]]:
|
||||
for i in range(num_horses - 1 - l):
|
||||
if total_distance[race_pos[i]] < total_distance[race_pos[i + 1]]:
|
||||
continue
|
||||
race_pos[i], race_pos[i+1] = race_pos[i+1], race_pos[i]
|
||||
race_pos[i], race_pos[i + 1] = race_pos[i + 1], race_pos[i]
|
||||
|
||||
# print current state of the race
|
||||
print_race_state(total_distance, race_pos)
|
||||
@@ -221,7 +222,7 @@ def simulate_race(odds):
|
||||
# check if the winning horse is already over the finish line
|
||||
if total_distance[race_pos[-1]] >= 28:
|
||||
return race_pos
|
||||
|
||||
|
||||
# this was not in the original BASIC implementation, but it makes the
|
||||
# race visualization a nice animation (if the terminal size is set to 31 rows)
|
||||
time.sleep(1)
|
||||
@@ -246,7 +247,7 @@ def print_race_results(race_positions, odds, bets, player_names):
|
||||
if horse == winning_horse_idx:
|
||||
basic_print("")
|
||||
basic_print(f"{name} WINS ${amount * odds[winning_horse_idx]}")
|
||||
|
||||
|
||||
|
||||
def main_loop(player_names, horse_odds):
|
||||
"""Main game loop"""
|
||||
@@ -272,7 +273,7 @@ def main():
|
||||
# main loop of the game, the player can play multiple races, with the
|
||||
# same odds
|
||||
main_loop(player_names, horse_odds)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user