-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
364 lines (315 loc) · 15.2 KB
/
bot.py
File metadata and controls
364 lines (315 loc) · 15.2 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import random
import logging
logging.basicConfig(filename="info.log", filemode='w', level=logging.DEBUG)
class Robot:
def __init__(self, size_=10):
logging.info("Bot created")
self.size = size_
self.ships = []
self.rest_ships = []
self.enemy_ships = []
self.win_enemy = False
self.win_me = False
self.is_injure = False
self.my_moves = []
self.direction_of_last_injure = 0 # h - горизонтально, v - вертикально
self.current_enemy_ship = []
self.previous_command = ''
self.my_previous_step = (-1, -1)
self.table = []
self.enemy_rest_cells = []
self.enemy_table = []
self.ships_size = self.enemy_ships_size = [4] + [3] * 2 + [2] * 3 + [1] * 4
def start(self):
logging.info("Start the game")
self.generate_table()
self.enemy_table = [[0] * self.size for i in range(self.size)]
self.enemy_rest_cells = [(i, j) for i in range(self.size) for j in range(self.size)]
def generate_table(self):
logging.info("Creating table")
my_table = [[0] * self.size for i in range(self.size)]
free_cells = [(i, j) for i in range(self.size) for j in range(self.size)]
for size in self.ships_size:
self.generate_ship(free_cells, my_table, size)
self.table = my_table
def generate_ship(self, free_cells, my_table, size_ship):
logging.info("Generation ship of size {}".format(size_ship))
# будем рассматривать только 2 направления от выбранной клетки - вниз и вправо
ship_put = False
curr_cell = None
while not ship_put:
curr_cell = random.choice(free_cells)
correct_directions = []
if curr_cell[0] + size_ship <= self.size:
correct_directions.append((1, 0))
if curr_cell[1] + size_ship <= self.size:
correct_directions.append((0, 1))
if len(correct_directions) > 0:
direction = random.choice(correct_directions)
# проверяем, что в выбранном направлении можем поставить корабль
i = 1
success = True
while i < size_ship and success:
if free_cells.count((curr_cell[0] + i * direction[0], curr_cell[1] + i * direction[1])) == 0:
success = False
i += 1
if success:
ship_put = True
current_ship = []
for k in range(size_ship):
current_ship.append((curr_cell[0], curr_cell[1]))
my_table[curr_cell[0]][curr_cell[1]] = 2
self.delete_used_cells(free_cells, curr_cell)
curr_cell = (curr_cell[0] + direction[0], curr_cell[1] + direction[1])
self.ships.append(current_ship)
self.rest_ships.append(current_ship)
def delete_used_cells(self, free_cells, curr_cell):
all_directions = [(first, second) for first in [-1, 0, 1] for second in [-1, 0, 1]]
for direction in all_directions:
cell = (curr_cell[0] + direction[0], curr_cell[1] + direction[1])
if free_cells.count(cell) > 0:
free_cells.remove(cell)
def get_rest_ships(self, all_ships=False):
# result = ''
ships = self.ships if all_ships else self.rest_ships
# for ship in ships:
# for cell in ship:
# result += "[ {} {} ], ".format(str(cell[0] + 1), str(chr(cell[1] + ord('A'))))
# result += "\n"
return '\n'.join(list(map(lambda ship: ', '.join(list(map(
lambda cell: "{}{}".format(cell[0] + 1, chr(cell[1] + ord('A'))), ship))), ships)))
def enemy_step(self, cell_0, cell_1):
cell = (int(cell_0) - 1, int(ord(cell_1) - ord('a')))
if self.table[cell[0]][cell[1]] == '@' or self.table[cell[0]][cell[1]] == '*' or self.table[cell[0]][cell[1]] == '#':
logging.info("User repeated his move")
if self.table[cell[0]][cell[1]] == '@' or self.table[cell[0]][cell[1]] == '#':
answer = "injure\nBut you made this move before"
elif self.table[cell[0]][cell[1]] == '*':
answer = "miss\nBut you made this move before"
if self.table[cell[0]][cell[1]] == 2:
if self.injure_my_ship(cell) == 0:
answer = "kill"
else:
answer = "injure"
self.table[cell[0]][cell[1]] = '@'
else:
answer = "miss"
self.table[cell[0]][cell[1]] = '*'
logging.info("user {}".format(answer))
self.check_win_enemy()
if self.win_enemy :
logging.info("User win")
return "{}\nYou win \n If you want to play again write 'play'".format(answer)
cell = self.my_step()
if cell == (-1,-1):
return "You made a mistake in your answers"
self.my_previous_step = cell
logging.info("Bot chose a cell: ({} {})".format(cell[0], cell[1]))
return "{} \n {} {}".format(answer, str(cell[0] + 1), str(chr(ord('A') + cell[1])))
def my_step(self):
try:
if self.is_injure:
return self.find_neighbour_deck()
else:
return self.find_ship()
except:
logging.info("User made a mistake in his answers.")
return -1, -1
def find_neighbour_deck(self):
possible_cells = []
if self.direction_of_last_injure == 0:
last_injure = self.current_enemy_ship[0]
if last_injure[0] - 1 >= 0 and self.enemy_table[last_injure[0] - 1][last_injure[1]] == 0:
possible_cells.append((last_injure[0] - 1, last_injure[1]))
if last_injure[1] - 1 >= 0 and self.enemy_table[last_injure[0]][last_injure[1] - 1] == 0:
possible_cells.append((last_injure[0], last_injure[1] - 1))
if last_injure[0] + 1 < self.size and self.enemy_table[last_injure[0] + 1][last_injure[1]] == 0:
possible_cells.append((last_injure[0] + 1, last_injure[1]))
if last_injure[1] + 1 < self.size and self.enemy_table[last_injure[0]][last_injure[1] + 1] == 0:
possible_cells.append((last_injure[0], last_injure[1] + 1))
elif self.direction_of_last_injure == 'h':
minimum = min(self.current_enemy_ship)
maximum = max(self.current_enemy_ship)
if minimum[1] - 1 >= 0 and self.enemy_table[minimum[0]][minimum[1] - 1] == 0:
possible_cells.append((minimum[0], minimum[1] - 1))
if maximum[1] + 1 < self.size and self.enemy_table[maximum[0]][maximum[1] + 1] == 0:
possible_cells.append((maximum[0], maximum[1] + 1))
elif self.direction_of_last_injure == 'v':
minimum = min(self.current_enemy_ship)
maximum = max(self.current_enemy_ship)
if minimum[0] - 1 >= 0 and self.enemy_table[minimum[0] - 1][minimum[1]] == 0:
possible_cells.append((minimum[0] - 1, minimum[1]))
if maximum[0] + 1 < self.size and self.enemy_table[maximum[0] + 1][maximum[1]] == 0:
possible_cells.append((maximum[0] + 1, maximum[1]))
cell = random.choice(possible_cells)
self.enemy_rest_cells.remove(cell)
return cell
def find_ship(self):
cell = random.choice(self.enemy_rest_cells)
self.enemy_rest_cells.remove(cell)
return cell
def injure_my_ship(self, cell):
for ship in self.rest_ships:
if ship.count(cell) > 0:
ship.remove(cell)
result = len(ship)
if result == 0:
self.rest_ships.remove(ship)
return result
def enemy_answer(self, answer):
cell = self.my_previous_step
if answer == 'miss':
self.enemy_table[cell[0]][cell[1]] = 1
elif answer == 'injure':
if len(self.current_enemy_ship) >= max(self.enemy_ships_size) :
logging.warning("User made a mistake in the answers")
return "You were wrong somewhere"
self.is_injure = True
self.current_enemy_ship.append(cell)
self.enemy_table[cell[0]][cell[1]] = 2
self.conclusion_injure(cell)
else:
self.current_enemy_ship.append(cell)
self.enemy_table[cell[0]][cell[1]] = 2
self.is_injure = False
self.delete_neighbour()
self.enemy_ships.append(self.current_enemy_ship)
if self.enemy_ships_size.count(len(self.current_enemy_ship)) == 0:
logging.warning("User made a mistake in the answers")
return "You were wrong somewhere"
self.enemy_ships_size.remove(len(self.current_enemy_ship))
self.current_enemy_ship.clear()
self.direction_of_last_injure = 0
self.check_win_me()
if self.win_me:
logging.info("User lose")
return "You lose. \n If you want to play again write 'play'"
return ''
def delete_neighbour(self):
all_directions = [(0, 0), (-1, 0), (1, 0), (0, -1), (0, 1), (1, -1), (1, 1), (-1, -1), (-1, 1)]
for deck in self.current_enemy_ship:
for direction in all_directions:
cell = (deck[0] + direction[0], deck[1] + direction[1])
if 0 <= cell[0] < self.size and 0 <= cell[1] < self.size and not self.enemy_table[cell[0]][cell[1]]:
self.enemy_table[cell[0]][cell[1]] = 1
self.enemy_rest_cells.remove(cell)
def conclusion_injure(self, cell):
if len(self.current_enemy_ship) == 2:
direction = (self.current_enemy_ship[0][0] - self.current_enemy_ship[1][0],
self.current_enemy_ship[0][1] - self.current_enemy_ship[1][1])
if direction[0] == 0:
self.direction_of_last_injure = 'h'
else:
self.direction_of_last_injure = 'v'
def check_win_enemy(self):
if len(self.rest_ships) == 0:
self.win_enemy = True
def check_win_me(self):
if len(self.enemy_ships) == 10:
self.win_me = True
def help(self):
logging.info("Output of information about the game")
return "At first, you should put your ships on table size*size. Ships : 1 four-deck, 2 three-deck, " \
"3 two-deck and 4 one-deck. \n" \
"defoult size = 10, but if you want to resize table write word 'size' and number > 7 before playing. \n" \
"Your step is first. You must write me a number from 1 to size and a letter from A to A + size. \n" \
"For each of my answers such as '1 A' you have to answer one of the following options: \n " \
"'miss', 'injure' or 'kill' \n" \
"If you're ready to start write 'Play'"
def handling_yes_no(self, cmd):
if cmd.lower() == "yes":
self.previous_command = cmd.lower()
logging.info("User want to play")
return "Let's go! If you want to read information write 'help'\n" \
"If you want to resize table write 'size' and number > 7. \n" \
"If you're ready to start write 'play' "
elif cmd.lower() == "no":
logging.info("User doesn't want to play")
self.previous_command = cmd.lower()
return "It's a pity. Good Buy!"
else:
logging.warning("Users message is incorrect")
return "Sorry, I don't understand you. You have to say me yes or no"
def change_size(self, commands):
if self.previous_command == "yes":
if len(commands) > 1:
cmd2 = commands[1]
else:
logging.warning("Users message is incorrect")
return "It is incorrect message. Try again"
try:
test = int(cmd2)
except ValueError:
logging.warning("Users message is incorrect")
return "It is incorrect message. Try again"
if int(cmd2) > 7:
self.size = int(cmd2)
logging.info("Size changed")
return "Ok"
else:
logging.warning("Users message is incorrect")
return "It is very small size"
else:
logging.warning("Users message is incorrect")
return "You can change size only before playing"
def handling_cell_selection(self, commands):
cmd = commands[0]
if len(commands) > 1:
cmd2 = commands[1]
else:
logging.warning("Users message is incorrect")
return "It is incorrect message. Try again"
try:
if 1 <= int(cmd) <= self.size and "a" <= cmd2.lower() <= chr(ord("a") + self.size - 1):
self.previous_command = "cell"
return self.enemy_step(cmd, cmd2.lower())
else:
logging.warning("Users message is incorrect")
return "It is incorrect message. Try again"
except ValueError:
logging.warning("Users message is incorrect")
return "It is incorrect message. Try again"
def command(self, message):
logging.info("User message: " + message)
commands = message.split()
cmd = commands[0]
if cmd.lower() in ["hi", "hello"]:
self.previous_command = "hi"
return "Hello! \n Do you want to play Seabatttle?"
elif self.previous_command == "hi":
return self.handling_yes_no(cmd)
elif cmd.lower() == "help":
return self.help()
elif cmd.lower() == "size":
return self.change_size(commands)
elif cmd.lower() == "play":
try:
self.previous_command = "play"
self.start()
return "Your step is first"
except:
logging.error("Can't create table")
return "Something went wrong"
elif cmd.lower() == "ships":
logging.info("User requests ships")
return self.get_rest_ships(True)
elif self.win_me or self.win_enemy:
logging.warning("Users message is incorrect")
return "The gave is over. \nIf you want to play again write 'play'"
elif self.previous_command == "play" or self.previous_command == "answer":
return self.handling_cell_selection(commands)
elif self.previous_command == "cell":
if cmd.lower() in ["miss", "injure", "kill"] and len(commands) == 1:
self.previous_command = "answer"
try:
return self.enemy_answer(cmd.lower())
except:
logging.warning("User made a mistake in the answers")
return "You were wrong " \
"somewhere"
else:
logging.warning("Users message is incorrect")
return "It is incorrect message. Try again"
else:
logging.warning("Users message is incorrect")
return "It is incorrect message. Try again"