-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameboard.py
196 lines (171 loc) · 6.58 KB
/
gameboard.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from item import Item
from ship import Ship
class GameBoard:
def __init__(self) -> None:
self.grid = ''
self.name = ''
self.square = ''
self.score_board = ''
self.grid = []
self.curser = Item("Curser", 1, "I")
self.hit = Item("Hit", 1, "X")
self.miss = Item("Miss", 1, "O")
self.destroyer = Ship("Destroyer", 2, "D")
self.submarine = Ship("Submarine", 3, "S")
self.battleship_1 = Ship("battleship_1", 4, "B")
self.battleship_2 = Ship("Battleship_2", 4, "B" )
self.aircraft_carrier = Ship("Aircraft Carrier", 5, "A")
self.move_direction = ''
self.ships = [self.destroyer, self.submarine, self.battleship_1, self.aircraft_carrier, self.battleship_2]
self.collision_exists = False
#make item, hit and miss an item and make an item class
def update_board(self):
pass
def display_hit(self):
pass
def display_miss(self):
pass
def display_sunk_ships(self):
pass
def check_collisions(self, ship):
ship.generate_ship_coordinates()
for boat in self.ships:
if boat.name == ship.name:
pass
elif ship.is_vertical == True:
if boat.is_vertical == True:
if ship.x == boat.x:
for y in ship.y_coordinates:
for z in boat.y_coordinates:
if y == z:
self.collision_exists = True
break
else:
for x in boat.x_coordinates:
if ship.x == x:
for y in ship.y_coordinates:
if y == boat.y:
self.collision_exists = True
else:
if boat.is_vertical == False:
if ship.y == boat.y:
for x in ship.x_coordinates:
for w in boat.x_coordinates:
if x == w:
self.collision_exists = True
else:
for y in boat.y_coordinates:
if ship.y == y:
for x in ship.x_coordinates:
if x == boat.y:
self.collision_exists = True
# PLACE COLLISION DETECTION INISDE MOVE ITEM IF " " IS SELECTED
# TO SEPARATE GRIDS PLACE | AT X 10
# EXPAND GRIDS TO X 20
# GRID.Y 0 LINE SHOULD HAVE MY SHIPS AND ENEMY SHIPS HEADER
def mark_identifyer(self, item):
if item.size > 1:
item.generate_ship_coordinates()
if item.is_vertical == True:
for p in item.y_coordinates:
self.grid[p][item.x] = item.identifyer
else:
for p in item.x_coordinates:
self.grid[item.y][p] = item.identifyer
elif item.size == 1:
self.grid[item.y][item.x] = item.identifyer
def display_set_ships(self):
for ship in self.ships:
if ship.is_set == True:
self.mark_identifyer(ship)
def display_grid(self, item):
for cell in self.grid:
print(" ".join(cell))
def generate_grid(self, rows, columns):
for y in range(0, rows):
self.grid.append(["[ ]" for x in range(0, columns)])
def get_move_input (self, item):
item.is_set = True
while True:
self.display_set_ships()
self.display_grid(item)
print(f"""Insructions:
Up: w
Down: s
Left: a
Right: d
Toggle ship vertical/horizontal: t
Set {item.name}: press SPACE BAR, then ENTER.""")
self.move_direction = input(f"Enter an option above to move the {item.name} :")
self.clear_item(item)
self.move_item(item)
if self.move_direction == " ":
break
def move_item(self, item):
if self.move_direction == "w":
if item.y == 0 and item.is_vertical == True:
item.y = 10 - item.size
elif item.y == 0:
item.y = 9
else:
item.y -= 1
elif self.move_direction == "s":
if item.y == 10 - item.size and item.is_vertical == True:
item.y = 0
elif item.y == 9:
item.y = 0
else:
item.y += 1
elif self.move_direction == "a":
if item.x == 0 and item.is_vertical == False:
item.x = 10 - item.size
elif item.x == 0:
item.x = 9
else:
item.x -= 1
elif self.move_direction == "d":
if item.x == 10 - item.size and item.is_vertical ==False:
item.x = 0
elif item.x == 9:
item.x = 0
else:
item.x += 1
elif self.move_direction == 't':
if item.size > 1:
item.toggle_vertical()
self.check_collisions(item)
if self.collision_exists == True:
self.move_back(item)
def move_back(self, item):
if self.collision_exists == True:
if self.move_direction == "w":
self.move_direction = "s"
elif self.move_direction == "s":
self.move_direction = "w"
elif self.move_direction == "a":
self.move_direction = "d"
elif self.move_direction == "d":
self.move_direction = "a"
self.collision_exists = False
self.move_item(item)
self.move_direction = self.move_direction
def place_ships(self):
self.get_move_input(self.battleship_1)
self.get_move_input(self.battleship_2)
self.get_move_input(self.aircraft_carrier)
self.get_move_input(self.submarine)
self.get_move_input(self.destroyer)
def run_game(self):
self.generate_grid(10, 10)
self.place_ships()
def clear_item(self, item):
if item.size > 1:
if item.is_vertical == True:
for p in item.y_coordinates:
self.grid[p][item.x] = "[ ]"
else:
for p in item.x_coordinates:
self.grid[item.y][p] = "[ ]"
item.clear_coordinates()
else:
self.grid[item.y][item.x] = "[ ]"