-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb_ships.py
274 lines (207 loc) · 8.36 KB
/
b_ships.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import os
from random import randint
EMPTY = True
ROWS = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F':
5, 'G': 6, 'H': 7, 'I': 8, 'J': 9, 'K': 10, 'L': 11}
HIT = ' X '
MISS = ' O '
def create_board(rows, cols):
board = []
for i in range(rows):
board_row = []
for j in range(cols):
board_row.append(EMPTY)
board.append(board_row)
return board
BOARD = create_board(12, 12)
BOARD_TO_DRAW = [[" "] * 12 for x in range(12)]
class Vehicle:
def __init__(self, symbol, size, count_in_game):
self.symbol = symbol
self.size = size
self.count_in_game = count_in_game
# def __str__(self):
# return self.symbol
def __call__(self):
return self
def __len__(self):
return self.size * self.count_in_game
class Tank(Vehicle):
def __init__(self):
Vehicle.__init__(self, 'Tk', 3, 3)
class Jeep(Vehicle):
def __init__(self):
Vehicle.__init__(self, 'Jp', 1, 5)
class Howitzer(Vehicle):
def __init__(self):
Vehicle.__init__(self, 'Hr', 2, 4)
class Military_truck(Vehicle):
def __init__(self):
Vehicle.__init__(self, 'Mt', 4, 2)
VEHICLES = [Tank(), Jeep(), Howitzer(), Military_truck()]
class Game:
def __init__(self, vehicles=[]):
self.vehicles = vehicles
def clear(self):
os.system('clear')
def valid_position(self, row, col, orientation, vehicle):
valid = True
if orientation == 'vertical' and row + vehicle.size >= len(BOARD):
valid = False
elif orientation == 'horizontal' and col + vehicle.size >= len(BOARD[0]):
valid = False
else:
if orientation == 'vertical':
for position in range(vehicle.size):
if BOARD[row + position][col] != EMPTY:
valid = False
elif orientation == 'horizontal':
for position in range(vehicle.size):
if BOARD[row][col + position] != EMPTY:
valid = False
return valid
def count_of_vehicles_in_board(self):
total_vehicle_objects = 0
for row in range(len(BOARD)):
for col in range(len(BOARD[0])):
if isinstance(BOARD[row][col], Vehicle):
total_vehicle_objects += 1
return total_vehicle_objects
def place_vehicle(self, row, col, orientation, vehicle):
if orientation == 'vertical':
for size in range(vehicle.size):
BOARD[row + size][col] = vehicle
elif orientation == 'horizontal':
for size in range(vehicle.size):
BOARD[row][col + size] = vehicle
return BOARD
def random_row(self):
random_row = randint(0, len(BOARD) - 1)
return random_row
def random_col(self):
random_col = randint(0, len(BOARD[0]) - 1)
return random_col
def random_orientation(self):
random_orientation = randint(0, 1)
if random_orientation == 0:
orientation = 'vertical'
else:
orientation = 'horizontal'
return orientation
def spawn_all_vehicles(self):
curent_vehicles = self.count_of_vehicles_in_board()
for vehicle in self.vehicles:
needed_vehicles = len(vehicle) + curent_vehicles # 9, 5, 8, 8
while needed_vehicles > curent_vehicles:
random_row = self.random_row()
random_col = self.random_col()
random_orientation = self.random_orientation()
if self.valid_position(random_row, random_col, random_orientation, vehicle):
self.place_vehicle(
random_row, random_col, random_orientation, vehicle)
curent_vehicles = self.count_of_vehicles_in_board()
def reveal_board(self, board):
for row in range(len(board)):
for col in range(len(board[0])):
if isinstance(board[row][col], Vehicle):
print("" + board[row][col].symbol + "|", end='')
else:
print(" " + "|", end='')
print()
return self.clear
def player_choice(self):
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
print('You can enter A-L for row.Small letters are also possibility')
print('You can enter 1-12 for column. 01; 02; 03 ... 09 is also possible choice :)')
player_input = input(
'Въведи координати, батка!' + "\n" + 'Give a shot: ')
if len(player_input) < 2 or len(player_input) > 3:
print(
'Invalid input! You need to enter exactly 2 entries! ДИМИЕК, ни са прай, оу, Мари Ханън!')
return self.player_choice()
elif len(player_input) == 2:
player_input = player_input + " "
player_row = player_input[0].upper()
player_column = player_input[1] + player_input[2]
if is_number(player_column):
player_column = int(player_column) - 1
else:
print('Enter number for column please!')
return self.player_choice()
if player_row not in ROWS:
print('Invalid row')
return self.player_choice()
elif player_column >= 12 or player_column < 0:
print('Ivalid column.That is out of range')
return self.player_choice()
else:
player_choice = {
'player_row': ROWS[player_row], 'player_column': player_column}
return player_choice
def get_vehicles_position(self):
occupied_positions = []
for row in range(len(BOARD)):
for col in range(len(BOARD[0])):
if isinstance(BOARD[row][col], Vehicle):
occupied_positions.append([row, col])
return occupied_positions
def draw_board(self, board):
print("\n" * 5)
print(" " * 3, end='')
for i in range(len(board)):
print(" " + str(i + 1) + " ", end='')
print()
for row in range(len(board)):
print(" " + chr(65 + row) + "|", end='')
for col in range(len(board[0])):
print("" + board[row][col] + "|", end='')
print()
print("\n" * 5)
return self.clear
def play(self):
bombed_positions = []
count_of_alive_vehicles = self.count_of_vehicles_in_board()
self.draw_board(BOARD_TO_DRAW)
while count_of_alive_vehicles > 0:
player_coordinates = self.player_choice()
if [player_coordinates['player_row'], player_coordinates['player_column']] in bombed_positions:
print(
"Those coordinates were already bombed. Please strike again somewhere else.")
continue
else:
bombed_positions.append(
[player_coordinates['player_row'], player_coordinates['player_column']])
if BOARD[player_coordinates['player_row']][player_coordinates['player_column']] == EMPTY:
message = 'You missed.Shoot again'
BOARD_TO_DRAW[player_coordinates['player_row']][
player_coordinates['player_column']] = MISS
elif isinstance(BOARD[player_coordinates['player_row']][player_coordinates['player_column']], Vehicle):
count_of_alive_vehicles -= 1
message = 'You hit an enemy ' + BOARD[player_coordinates['player_row']][
player_coordinates['player_column']].__class__.__name__
BOARD_TO_DRAW[player_coordinates['player_row']][
player_coordinates['player_column']] = HIT
print(message)
self.draw_board(BOARD_TO_DRAW)
if count_of_alive_vehicles == 0:
self.clear()
print('GAME OVER!')
print(
'Sorry for the long lanes. I know they are in violation with pep8.')
game = Game(VEHICLES)
game.spawn_all_vehicles()
def start():
reveal_or_no = input('Check board before game ?!? --- enter Y/N ' + "\n")
if reveal_or_no.upper() == 'Y':
game.reveal_board(BOARD)
return start()
elif reveal_or_no.upper() == 'N':
game.play()
else:
return start()
start()