mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-25 12:25:10 -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:
@@ -334,4 +334,4 @@
|
||||
"source": []
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import re
|
||||
import random
|
||||
import re
|
||||
|
||||
###################
|
||||
#
|
||||
@@ -20,21 +20,25 @@ BOARD_HEIGHT = 10
|
||||
# length - number of "parts" on the ship that
|
||||
# can be shot
|
||||
# shots - number of shots the ship counts for
|
||||
SHIPS = [("BATTLESHIP", 5, 3),
|
||||
("CRUISER", 3, 2),
|
||||
("DESTROYER<A>", 2, 1),
|
||||
("DESTROYER<B>", 2, 1)]
|
||||
SHIPS = [
|
||||
("BATTLESHIP", 5, 3),
|
||||
("CRUISER", 3, 2),
|
||||
("DESTROYER<A>", 2, 1),
|
||||
("DESTROYER<B>", 2, 1),
|
||||
]
|
||||
|
||||
VALID_MOVES = [[-1, 0], # North
|
||||
[-1, 1], # North East
|
||||
[0, 1], # East
|
||||
[1, 1], # South East
|
||||
[1, 0], # South
|
||||
[1, -1], # South West
|
||||
[0, -1], # West
|
||||
[-1, -1]] # North West
|
||||
VALID_MOVES = [
|
||||
[-1, 0], # North
|
||||
[-1, 1], # North East
|
||||
[0, 1], # East
|
||||
[1, 1], # South East
|
||||
[1, 0], # South
|
||||
[1, -1], # South West
|
||||
[0, -1], # West
|
||||
[-1, -1],
|
||||
] # North West
|
||||
|
||||
COORD_REGEX = '[ \t]{0,}(-?[0-9]{1,3})[ \t]{0,},[ \t]{0,}(-?[0-9]{1,2})'
|
||||
COORD_REGEX = "[ \t]{0,}(-?[0-9]{1,3})[ \t]{0,},[ \t]{0,}(-?[0-9]{1,2})"
|
||||
|
||||
####################
|
||||
#
|
||||
@@ -112,8 +116,8 @@ random.seed()
|
||||
# x: integer between 1 and BOARD_HEIGHT
|
||||
# y: integer between 1 and BOARD WIDTH
|
||||
def random_x_y():
|
||||
x = random.randrange(1, BOARD_WIDTH+1)
|
||||
y = random.randrange(1, BOARD_HEIGHT+1)
|
||||
x = random.randrange(1, BOARD_WIDTH + 1)
|
||||
y = random.randrange(1, BOARD_HEIGHT + 1)
|
||||
return (x, y)
|
||||
|
||||
|
||||
@@ -217,8 +221,7 @@ def generate_ship_coordinates(ship):
|
||||
# helper function to create a game board
|
||||
# that is blank
|
||||
def create_blank_board():
|
||||
return [[None for y in range(BOARD_WIDTH)]
|
||||
for x in range(BOARD_HEIGHT)]
|
||||
return [[None for y in range(BOARD_WIDTH)] for x in range(BOARD_HEIGHT)]
|
||||
|
||||
|
||||
# print_board
|
||||
@@ -228,19 +231,19 @@ def create_blank_board():
|
||||
def print_board(board):
|
||||
|
||||
# print board header (column numbers)
|
||||
print(' ', end='')
|
||||
print(" ", end="")
|
||||
for z in range(BOARD_WIDTH):
|
||||
print(f'{z+1:3}', end='')
|
||||
print('')
|
||||
print(f"{z+1:3}", end="")
|
||||
print("")
|
||||
|
||||
for x in range(len(board)):
|
||||
print(f'{x+1:2}', end='')
|
||||
print(f"{x+1:2}", end="")
|
||||
for y in range(len(board[x])):
|
||||
if(board[x][y] is None):
|
||||
print(f"{' ':3}", end='')
|
||||
if board[x][y] is None:
|
||||
print(f"{' ':3}", end="")
|
||||
else:
|
||||
print(f"{board[x][y]:3}", end='')
|
||||
print('')
|
||||
print(f"{board[x][y]:3}", end="")
|
||||
print("")
|
||||
|
||||
|
||||
# place_ship
|
||||
@@ -256,7 +259,7 @@ def print_board(board):
|
||||
# ship - integer repreesnting the type of ship (given in SHIPS)
|
||||
def place_ship(board, coords, ship):
|
||||
for coord in coords:
|
||||
board[coord[0]-1][coord[1]-1] = ship
|
||||
board[coord[0] - 1][coord[1] - 1] = ship
|
||||
|
||||
|
||||
# NOTE: A little quirk that exists here and in the orginal
|
||||
@@ -274,7 +277,7 @@ def generate_board():
|
||||
coords = generate_ship_coordinates(ship)
|
||||
clear = True
|
||||
for coord in coords:
|
||||
if board[coord[0]-1][coord[1]-1] is not None:
|
||||
if board[coord[0] - 1][coord[1] - 1] is not None:
|
||||
clear = False
|
||||
break
|
||||
if clear:
|
||||
@@ -292,12 +295,12 @@ def generate_board():
|
||||
def execute_shot(turn, board, x, y):
|
||||
|
||||
global current_turn
|
||||
square = board[x-1][y-1]
|
||||
square = board[x - 1][y - 1]
|
||||
ship_hit = -1
|
||||
if square is not None:
|
||||
if square >= 0 and square < len(SHIPS):
|
||||
ship_hit = square
|
||||
board[x-1][y-1] = 10 + current_turn
|
||||
board[x - 1][y - 1] = 10 + current_turn
|
||||
return ship_hit
|
||||
|
||||
|
||||
@@ -310,7 +313,7 @@ def calculate_shots(board):
|
||||
ships_found = [0 for x in range(len(SHIPS))]
|
||||
for x in range(BOARD_HEIGHT):
|
||||
for y in range(BOARD_WIDTH):
|
||||
square = board[x-1][y-1]
|
||||
square = board[x - 1][y - 1]
|
||||
if square is not None:
|
||||
if square >= 0 and square < len(SHIPS):
|
||||
ships_found[square] = 1
|
||||
@@ -340,12 +343,12 @@ def initialize_game():
|
||||
computer_board, computer_ship_coords = generate_board()
|
||||
|
||||
# print out the title 'screen'
|
||||
print('{0:>38}'.format("SALVO"))
|
||||
print('{0:>57s}'.format("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"))
|
||||
print('')
|
||||
print('{0:>52s}'.format("ORIGINAL BY LAWRENCE SIEGEL, 1973"))
|
||||
print('{0:>56s}'.format("PYTHON 3 PORT BY TODD KAISER, MARCH 2021"))
|
||||
print('\n')
|
||||
print("{:>38}".format("SALVO"))
|
||||
print("{:>57s}".format("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"))
|
||||
print("")
|
||||
print("{:>52s}".format("ORIGINAL BY LAWRENCE SIEGEL, 1973"))
|
||||
print("{:>56s}".format("PYTHON 3 PORT BY TODD KAISER, MARCH 2021"))
|
||||
print("\n")
|
||||
|
||||
# ask the player for ship coordinates
|
||||
print("ENTER COORDINATES FOR...")
|
||||
@@ -376,7 +379,7 @@ def initialize_game():
|
||||
for coord in coords:
|
||||
x = coord[0]
|
||||
y = coord[1]
|
||||
print('{0:2}'.format(x), '{0:2}'.format(y))
|
||||
print(f"{x:2}", f"{y:2}")
|
||||
else:
|
||||
input_loop = False
|
||||
|
||||
@@ -453,7 +456,7 @@ def execute_turn(turn):
|
||||
x, y = random_x_y()
|
||||
else:
|
||||
x, y = input_coord()
|
||||
square = board[x-1][y-1]
|
||||
square = board[x - 1][y - 1]
|
||||
if square is not None:
|
||||
if square > 10:
|
||||
if turn == PLAYER:
|
||||
@@ -477,7 +480,6 @@ def execute_turn(turn):
|
||||
else:
|
||||
print("YOU HIT MY", SHIPS[hit][0])
|
||||
|
||||
|
||||
if turn == COMPUTER:
|
||||
num_player_shots = calculate_shots(board)
|
||||
return num_player_shots
|
||||
|
||||
Reference in New Issue
Block a user