-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
94 lines (87 loc) · 3.06 KB
/
board.py
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
from square import Square
class Board:
def __init__(self):
self.board_list = []
for i in range(10):
col_list = []
for j in range(10):
temp_square = Square()
col_list.append(temp_square)
self.board_list.append(col_list)
self.hits = 0
def display_board(self):
display_board = []
for row in self.board_list:
temp_row = []
for col in row:
if col.attacked == True:
temp_row.append("X")
elif col.ship == True:
temp_row.append("O")
else:
temp_row.append("-")
if len(temp_row) > 0:
display_board.append(temp_row)
i = 1
# 3 spaces to indent
print(" 1 2 3 4 5 6 7 8 9 10")
for row in display_board:
if i < 10:
# two spaces after the i to account for 10
print(f"{i} ", end="")
else:
# one space after the i
print(f"{i} ", end="")
print(*row)
i += 1
print("\n")
def add_ship(self, position, length, orientation):
# make sure on board
# vertical placement
if orientation == "v":
if (position[0] > 10) or (position[0] < 0):
return False
if ((position[0] + length) > 10) or ((position[0] + length) < 0):
return False
if (position[1] > 10) or (position[1] < 0):
return False
# horizontal placement
elif orientation == "h":
if (position[0] > 10) or (position[0] < 0):
return False
if ((position[1] + length) > 10) or ((position[1] + length) < 0):
return False
if (position[1] > 10) or (position[1] < 0):
return False
else:
return False
# make sure all squares are clear
if orientation == "v":
bottom_square = position[0] + length
for i in range(position[0], bottom_square):
if self.board_list[i][position[1]].has_ship():
return False
# add the ship
for i in range(position[0], bottom_square):
self.board_list[i][position[1]].add_ship()
elif orientation == "h":
right_square = position[1] + length
for i in range(position[1], right_square):
if self.board_list[position[0]][i].has_ship():
return False
# add the ship
for i in range(position[1], right_square):
self.board_list[position[0]][i].add_ship()
return True
def attack_square(self, row, col):
result = self.board_list[row][col].attack()
if result[0] == False:
return False
elif result[1] == 1:
print("Hit!!!")
self.hits += 1
else:
print("Miss")
return True
#board = Board()
#board.display_board()