mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Cleaned up debug statements and program flow. Fixed a couple of bugs from testing
This commit is contained in:
@@ -147,8 +147,24 @@ def input_coord():
|
||||
return x, y
|
||||
|
||||
|
||||
# TODO: add an optional starting coordinate for testing
|
||||
# purposes
|
||||
# generate_ship_coordinates
|
||||
#
|
||||
# given a ship from the SHIPS array, generate
|
||||
# the coordinates of the ship. the starting point
|
||||
# of the ship's first coordinate is generated randomly.
|
||||
# once the starting coordinates are determined, the
|
||||
# possible directions of the ship, accounting for the
|
||||
# edges of the board, are determined. once possible
|
||||
# directions are found, a direction is randomly
|
||||
# determined and the remaining coordinates are
|
||||
# generated by adding or substraction from the starting
|
||||
# coordinates as determined by direction.
|
||||
#
|
||||
# arguments:
|
||||
# ship - index into the SHIPS array
|
||||
#
|
||||
# returns:
|
||||
# array of sets of coordinates (x,y)
|
||||
def generate_ship_coordinates(ship):
|
||||
# randomly generate starting x,y coordinates
|
||||
start_x, start_y = random_x_y()
|
||||
@@ -273,16 +289,16 @@ def generate_board():
|
||||
# given a board and x, y coordinates,
|
||||
# execute a shot. returns True if the shot
|
||||
# is valid, False if not
|
||||
def execute_shot(board, x, y):
|
||||
def execute_shot(turn, board, x, y):
|
||||
|
||||
global current_turn
|
||||
square = board[x-1][y-1]
|
||||
if square is not None:
|
||||
if square >= 0 and square < len(SHIPS):
|
||||
if active_turn == PLAYER:
|
||||
print("YOU HIT MY", SHIPS[square][0])
|
||||
else:
|
||||
if turn == COMPUTER:
|
||||
print("I HIT YOUR", SHIPS[square][0])
|
||||
else:
|
||||
print("YOU HIT MY", SHIPS[square][0])
|
||||
|
||||
board[x-1][y-1] = 10 + current_turn
|
||||
|
||||
@@ -328,7 +344,10 @@ def initialize_game():
|
||||
# print out the title 'screen'
|
||||
print('{0:>38}'.format("SALVO"))
|
||||
print('{0:>57s}'.format("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"))
|
||||
print('\n\n')
|
||||
print('')
|
||||
print('{0:>52s}'.format("ORIGINAL BY LAWRENCE SIEGEL, 1973"))
|
||||
print('{0:>56s}'.format("PYTHON 3 PORT BY TODD KAISER, MARCH 2021"))
|
||||
print('\n')
|
||||
|
||||
# ask the player for ship coordinates
|
||||
print("ENTER COORDINATES FOR...")
|
||||
@@ -468,7 +487,7 @@ def execute_turn(turn):
|
||||
valid_shot = True
|
||||
|
||||
for shot in shots:
|
||||
execute_shot(board, shot[0], shot[1])
|
||||
execute_shot(turn, board, shot[0], shot[1])
|
||||
if turn == COMPUTER:
|
||||
if print_computer_shots:
|
||||
print(shot[0], shot[1])
|
||||
@@ -507,10 +526,11 @@ while not game_over:
|
||||
|
||||
print("\n")
|
||||
print("TURN", current_turn)
|
||||
print("computer")
|
||||
print_board(computer_board)
|
||||
print("player")
|
||||
print_board(player_board)
|
||||
|
||||
# print("computer")
|
||||
# print_board(computer_board)
|
||||
# print("player")
|
||||
# print_board(player_board)
|
||||
|
||||
if execute_turn(first_turn) == 0:
|
||||
game_over = True
|
||||
|
||||
Reference in New Issue
Block a user