mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 15:50:20 -08:00
Avoid global variables
This commit is contained in:
@@ -55,7 +55,7 @@ def pick_number():
|
||||
return num
|
||||
|
||||
|
||||
def get_valid_guess():
|
||||
def get_valid_guess(guesses):
|
||||
valid = False
|
||||
while not valid:
|
||||
guess = input(f"Guess # {guesses} ? ")
|
||||
@@ -135,7 +135,7 @@ def main():
|
||||
guessing = True
|
||||
while guessing:
|
||||
|
||||
guess = get_valid_guess()
|
||||
guess = get_valid_guess(guesses)
|
||||
|
||||
if guess == num_str:
|
||||
print("You got it!!!\n")
|
||||
|
||||
@@ -57,8 +57,6 @@ computer_board = []
|
||||
player_ship_coords = []
|
||||
computer_ship_coords = []
|
||||
|
||||
# keep track of the turn
|
||||
current_turn = 0
|
||||
|
||||
####################################
|
||||
#
|
||||
@@ -287,14 +285,12 @@ def generate_board():
|
||||
return board, ship_coords
|
||||
|
||||
|
||||
# execute_shot
|
||||
#
|
||||
# given a board and x, y coordinates,
|
||||
# execute a shot. returns True if the shot
|
||||
# is valid, False if not
|
||||
def execute_shot(turn, board, x, y):
|
||||
|
||||
global current_turn
|
||||
def execute_shot(turn, board, x, y, current_turn):
|
||||
"""
|
||||
given a board and x, y coordinates,
|
||||
execute a shot. returns True if the shot
|
||||
is valid, False if not
|
||||
"""
|
||||
square = board[x - 1][y - 1]
|
||||
ship_hit = -1
|
||||
if square is not None and square >= 0 and square < len(SHIPS):
|
||||
@@ -422,7 +418,7 @@ first_turn = PLAYER
|
||||
second_turn = COMPUTER
|
||||
|
||||
|
||||
def execute_turn(turn):
|
||||
def execute_turn(turn, current_turn):
|
||||
|
||||
global num_computer_shots
|
||||
global num_player_shots
|
||||
@@ -464,7 +460,7 @@ def execute_turn(turn):
|
||||
|
||||
hits = []
|
||||
for shot in shots:
|
||||
hit = execute_shot(turn, board, shot[0], shot[1])
|
||||
hit = execute_shot(turn, board, shot[0], shot[1], current_turn)
|
||||
if hit >= 0:
|
||||
hits.append(hit)
|
||||
if turn == COMPUTER and print_computer_shots:
|
||||
@@ -491,6 +487,9 @@ def execute_turn(turn):
|
||||
|
||||
|
||||
def main():
|
||||
# keep track of the turn
|
||||
current_turn = 0
|
||||
|
||||
# initialize the player and computer
|
||||
# boards
|
||||
initialize_game()
|
||||
@@ -512,10 +511,10 @@ def main():
|
||||
# print("player")
|
||||
# print_board(player_board)
|
||||
|
||||
if execute_turn(first_turn) == 0:
|
||||
if execute_turn(first_turn, current_turn) == 0:
|
||||
game_over = True
|
||||
continue
|
||||
if execute_turn(second_turn) == 0:
|
||||
if execute_turn(second_turn, current_turn) == 0:
|
||||
game_over = True
|
||||
continue
|
||||
|
||||
|
||||
@@ -1,40 +1,10 @@
|
||||
from random import random
|
||||
|
||||
print("Slalom".rjust(39))
|
||||
print("Creative Computing Morristown, New Jersey\n\n\n".rjust(57))
|
||||
|
||||
medals = {
|
||||
"gold": 0,
|
||||
"silver": 0,
|
||||
"bronze": 0,
|
||||
}
|
||||
max_speeds = [
|
||||
14,
|
||||
18,
|
||||
26,
|
||||
29,
|
||||
18,
|
||||
25,
|
||||
28,
|
||||
32,
|
||||
29,
|
||||
20,
|
||||
29,
|
||||
29,
|
||||
25,
|
||||
21,
|
||||
26,
|
||||
29,
|
||||
20,
|
||||
21,
|
||||
20,
|
||||
18,
|
||||
26,
|
||||
25,
|
||||
33,
|
||||
31,
|
||||
22,
|
||||
]
|
||||
|
||||
|
||||
def ask(question):
|
||||
@@ -47,7 +17,7 @@ def ask_int(question):
|
||||
return int(reply) if reply.isnumeric() else -1
|
||||
|
||||
|
||||
def pre_run():
|
||||
def pre_run(gates, max_speeds):
|
||||
print('\nType "INS" for instructions')
|
||||
print('Type "MAX" for approximate maximum speeds')
|
||||
print('Type "RUN" for the beginning of the race')
|
||||
@@ -80,7 +50,7 @@ def pre_run():
|
||||
cmd = ask(f'"{cmd}" is an illegal command--Retry')
|
||||
|
||||
|
||||
def run():
|
||||
def run(gates, lvl, max_speeds):
|
||||
global medals
|
||||
print("The starter counts down...5...4...3...2...1...Go!")
|
||||
time = 0
|
||||
@@ -161,6 +131,37 @@ def run():
|
||||
|
||||
|
||||
def main():
|
||||
print("Slalom".rjust(39))
|
||||
print("Creative Computing Morristown, New Jersey\n\n\n".rjust(57))
|
||||
|
||||
max_speeds = [
|
||||
14,
|
||||
18,
|
||||
26,
|
||||
29,
|
||||
18,
|
||||
25,
|
||||
28,
|
||||
32,
|
||||
29,
|
||||
20,
|
||||
29,
|
||||
29,
|
||||
25,
|
||||
21,
|
||||
26,
|
||||
29,
|
||||
20,
|
||||
21,
|
||||
20,
|
||||
18,
|
||||
26,
|
||||
25,
|
||||
33,
|
||||
31,
|
||||
22,
|
||||
]
|
||||
|
||||
while True:
|
||||
gates = ask_int("How many gates does this course have (1 to 25)")
|
||||
if gates < 1:
|
||||
@@ -170,7 +171,7 @@ def main():
|
||||
print("25 is the limit.")
|
||||
break
|
||||
|
||||
pre_run()
|
||||
pre_run(gates, max_speeds)
|
||||
|
||||
while True:
|
||||
lvl = ask_int("Rate yourself as a skier, (1=Worst, 3=Best)")
|
||||
@@ -180,7 +181,7 @@ def main():
|
||||
break
|
||||
|
||||
while True:
|
||||
run()
|
||||
run(gates, lvl, max_speeds)
|
||||
while True:
|
||||
answer = ask("Do you want to play again?")
|
||||
if answer == "YES" or answer == "NO":
|
||||
|
||||
Reference in New Issue
Block a user