mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-30 06:31:46 -08:00
Avoid global variables
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user