-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.py
More file actions
135 lines (122 loc) · 4.69 KB
/
Copy pathboard.py
File metadata and controls
135 lines (122 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import random
#### Functions related to the game board
## All n definded by board size are - 1 for padding but not for placing ships
# Function to create a blank board
def create_board(n):
board = [] # Initialize an empty list
for r in range(n+1): # for loop from 0 to rows - 1
row = [] # Initialize an empty list for each row
for c in range(n+1): # for loop from 0 to cols - 1
row.append('*') # Append a . to the row list
board.append(row) # Append the row list to the board list
return board # note: this is not a rxc matrix but a list of lists
# Checker for placing a ship
def can_place_ship(board, row, col, orientation, size):
n = len(board) - 1
if orientation == 'H':
# must fit to the right
if col + size > n:
return False
# check for overlap/neighbors
for i in range(size + 2):
if board[row][col - 1 + i] != '*':
return False
if row == 0: # top edge
if board[row + 1][col - 1 + i] != '*':
return False
elif row == n -1: # bottom edge
if board[row - 1][col - 1 + i] != '*':
return False
else: # middle rows
if board[row - 1][col - 1 + i] != '*':
return False
if board[row + 1][col - 1 + i] != '*':
return False
return True
elif orientation == 'V':
# must fit downward
if row + size > n:
return False
# check for overlap/neighbors
for i in range(size + 2):
if board[row - 1 + i][col] != '*':
return False
if col == 0: # left edge
if board[row - 1 + i][col + 1] != '*':
return False
elif col == n -1: # right edge
if board[row - 1 + i][col - 1] != '*':
return False
else: # middle cols
if board[row - 1 + i][col - 1] != '*':
return False
if board[row - 1 + i][col + 1] != '*':
return False
return True
else:
return False # invalid orientation
# place a ship on the board
# Orientation is H or V
def place_ship(board, row, col, orientation, size):
if not can_place_ship(board, row, col, orientation, size):
return False # Cannot place ship here
if orientation == 'H':
for i in range(size):
board[row][col + i] = 'S'
elif orientation == 'V':
for i in range(size):
board[row + i][col] = 'S'
return True
# Fleet definitions by board size
def fleet_for_n(n):
if n == 8:
return [4, 3, 3, 3, 2, 2, 2]
elif n == 9:
return [4,4,4,3,3,3,3,3]
elif n == 10:
return [4,3,3,2,2,2,1,1,1,1]
else:
raise ValueError("Unsupported board size for fleet definition")
# randomly place one ship
def randomly_place_ship(board, size, nmax=1000):
n = len(board) - 1
placed = False
tries = 1
while not placed:
orientation = random.choice(['H', 'V'])
if orientation == 'H':
row = random.randint(0, n - 1)
col = random.randint(0, n - size)
else: # V
row = random.randint(0, n - size)
col = random.randint(0, n - 1)
if place_ship(board, row, col, orientation, size):
return True
tries += 1
if tries > nmax:
print("Failed to place ship of size", size)
break
return False
# Place the whole fleet
def place_fleet(n, board):
fleet = fleet_for_n(n)
for size in fleet:
if not randomly_place_ship(board, size):
print("Failed to place the entire fleet.")
raise RuntimeError("Could not place fleet")
return board
# Fxn to print board
def print_board(board):
n = len(board) - 1 # Get number of rows and cols
# print column numbers
print(" ", end="") # Print initial spaces
for c in range(n): # for loop from 0 to cols - 1
print(c, end=" ") # Print column number with space
print() # Print a new line after column numbers
#Print rows
for r in range(n): # for loop from 0 to rows - 1
row_label = chr(ord('A') + r) # Convert row number to letter
print(f"{row_label} ", end="") # Print row label with space
for c in range(n): # for loop from 0 to cols - 1
print(board[r][c], end=" ") # Print the element at row r and col c with a space
print() # Print a new line after each row