-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path__AIShots__.py
391 lines (343 loc) · 16.4 KB
/
__AIShots__.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# =============================================================================
# Battleship AI generate shots
# =============================================================================
# import matplotlib.pyplot as plt
import random as rand
import numpy as np
AIBoard = np.array([[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']])
def updateMode():
"""
Updates the AI mode
Returns
-------------------
mode: str
"target" or "search"
"""
for row in range(0, 10):
for col in range(0, 10):
if AIBoard[row][col] == 'X':
return "target"
return "search"
def updateAIBoardSinking(shipCoords):
"""
Call once a ship is sunk. Replaces the 'X's on the AI board with 'S's to denote a sunk ship
"""
for i in range(0, len(shipCoords)):
row = shipCoords[i][0]
col = shipCoords[i][1]
AIBoard[row][col] = 'S'
def updateAIBoard(guess, value):
"""
Mutator function for AIBoard
Parameters
-------------------
guess: Tuple (int, int)
Coordinates of guess (row, column)
value: str
Use 'O' or 'X'
"""
AIBoard[guess[0]][guess[1]] = value
def checkValidShotLocation(guess):
"""
Checks if a shot location is valid
Parameters
-------------------
guess: Tuple (int, int)
Coordinates of guess (row, column)
Returns
-------------------
Boolean, true if valid location, false if not
"""
if str(guess[0]) in "0123456789" and str(guess[1]) in "0123456789":
if AIBoard[guess[0]][guess[1]] == 'O' or AIBoard[guess[0]][guess[1]] == 'X' or AIBoard[guess[0]][guess[1]] == 'S':
return False
return True
return False
def __saveHeatMap__(ships, mode, diff, turn, directory, name):
if mode == "search":
search(diff, ships, AIBoard, turn)
else:
target(ships)
a = AIBoardHeatMap
# plt.imshow(a, cmap='binary')
# plt.title(name + ' ' + str(turn))
# plt.savefig(directory + '/' + str(turn) + '.png')
def search(diff, ships, p2board, turnCount):
global AIBoardHeatMap
AIBoardHeatMap = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
method = 3
randomProb = 25
if turnCount > 50:
randomProb = 0
if rand.randint(1, 100) <= randomProb:
#random
method = 1
if rand.randint(1, 100) <= 65:
#noisemap
method = 0
elif rand.randint(1, 100) <= 10:
#corner
method = 2
#NOISEMAP
if method == 0:
shipCount = 0
if ships[0] == False:
shipCount += 1
if ships[1] == False:
shipCount += 1
if ships[2] == False:
shipCount += 1
if ships[3] == False:
shipCount += 1
if ships[4] == False:
shipCount += 1
for row in range(0, 10):
for col in range(0, 10):
AIBoardHeatMap[row][col] = rand.randint(0, shipCount)
method = 3
#RANDOM SHOT
if method == 1:
while True:
shot = (rand.randint(0, 9), rand.randint(0, 9))
if AIBoard[shot[0]][shot[1]] == ' ':
return shot
#RANDOM CORNER
if method == 2:
c0, c1, c2, c3 = False, False, False, False
if AIBoard[0][0] == ' ':
c0 = True
if AIBoard[0][9] == ' ':
c1 = True
if AIBoard[9][0] == ' ':
c2 = True
if AIBoard[9][9] == ' ':
c3 = True
shot = (0, 0)
if c0 or c1 or c2 or c3:
while True:
corner = rand.randint(0, 3)
if corner == 0:
if c0 == True:
shot = (0, 0)
break
elif corner == 1:
if c1 == True:
shot = (0, 9)
break
elif corner == 2:
if c2 == True:
shot = (9, 0)
break
elif corner == 3:
if c3 == True:
shot = (9, 9)
break
return shot
else:
method = 3
#HEATMAP
if method == 3:
#For each space
for row in range(0, 10):
for col in range(0, 10):
#Destroyer Check
if ships[4] == False and p2board[row][col] == ' ':
if col-1 >= 0:
if AIBoard[row][col-1] == ' ':
AIBoardHeatMap[row][col] += 1
if row-1 >= 0:
if AIBoard[row-1][col] == ' ':
AIBoardHeatMap[row][col] += 1
if col+1 <= 9:
if AIBoard[row][col+1] == ' ':
AIBoardHeatMap[row][col] += 1
if row+1 <= 9:
if AIBoard[row+1][col] == ' ':
AIBoardHeatMap[row][col] += 1
#Sub Check
if ships[3] == False and p2board[row][col] == ' ' and diff >= 2:
if col-1 >= 0 and col+1 <= 9:
if AIBoard[row][col-1] == ' ' and AIBoard[row][col+1] == ' ':
AIBoardHeatMap[row][col] += 1
if row-1 >= 0 and row+1 <= 9:
if AIBoard[row-1][col] == ' ' and AIBoard[row+1][col] == ' ':
AIBoardHeatMap[row][col] += 1
if col-2 >= 0:
if AIBoard[row][col-2] == ' ' and AIBoard[row][col-1] == ' ':
AIBoardHeatMap[row][col] += 1
if row-2 >= 0:
if AIBoard[row-2][col] == ' ' and AIBoard[row-1][col] == ' ':
AIBoardHeatMap[row][col] += 1
if col+2 <= 9:
if AIBoard[row][col+2] == ' ' and AIBoard[row][col+1] == ' ':
AIBoardHeatMap[row][col] += 1
if row+2 <= 9:
if AIBoard[row+2][col] == ' ' and AIBoard[row+1][col] == ' ':
AIBoardHeatMap[row][col] += 1
#Cruiser Check
if ships[2] == False and p2board[row][col] == ' ' and diff >= 2:
if col-1 >= 0 and col+1 <= 9:
if AIBoard[row][col-1] == ' ' and AIBoard[row][col+1] == ' ':
AIBoardHeatMap[row][col] += 1
if row-1 >= 0 and row+1 <= 9:
if AIBoard[row-1][col] == ' ' and AIBoard[row+1][col] == ' ':
AIBoardHeatMap[row][col] += 1
if col-2 >= 0:
if AIBoard[row][col-2] == ' ' and AIBoard[row][col-1] == ' ':
AIBoardHeatMap[row][col] += 1
if row-2 >= 0:
if AIBoard[row-2][col] == ' ' and AIBoard[row-1][col] == ' ':
AIBoardHeatMap[row][col] += 1
if col+2 <= 9:
if AIBoard[row][col+2] == ' ' and AIBoard[row][col+1] == ' ':
AIBoardHeatMap[row][col] += 1
if row+2 <= 9:
if AIBoard[row+2][col] == ' ' and AIBoard[row+1][col] == ' ':
AIBoardHeatMap[row][col] += 1
#Battleship Check
if ships[1] == False and p2board[row][col] == ' ' and diff == 3:
if col-3 >= 0:
if AIBoard[row][col-1] == ' ' and AIBoard[row][col-2] == ' ' and AIBoard[row][col-3] == ' ':
AIBoardHeatMap[row][col] += 1
if col-2 >= 0 and col+1 <= 9:
if AIBoard[row][col-1] == ' ' and AIBoard[row][col-2] == ' ' and AIBoard[row][col+1] == ' ':
AIBoardHeatMap[row][col] += 1
if col-1 >= 0 and col+2 <= 9:
if AIBoard[row][col-1] == ' ' and AIBoard[row][col+1] == ' ' and AIBoard[row][col+2] == ' ':
AIBoardHeatMap[row][col] += 1
if col+3 <= 9:
if AIBoard[row][col+1] == ' ' and AIBoard[row][col+2] == ' ' and AIBoard[row][col+3] == ' ':
AIBoardHeatMap[row][col] += 1
if row-3 >= 0:
if AIBoard[row-1][col] == ' ' and AIBoard[row-2][col] == ' ' and AIBoard[row-3][col] == ' ':
AIBoardHeatMap[row][col] += 1
if row-2 >= 0 and row+1 <= 9:
if AIBoard[row-1][col] == ' ' and AIBoard[row-2][col] == ' ' and AIBoard[row+1][col] == ' ':
AIBoardHeatMap[row][col] += 1
if row-1 >= 0 and row+2 <= 9:
if AIBoard[row-1][col] == ' ' and AIBoard[row+1][col] == ' ' and AIBoard[row+2][col] == ' ':
AIBoardHeatMap[row][col] += 1
if row+3 <= 9:
if AIBoard[row+1][col] == ' ' and AIBoard[row+2][col] == ' ' and AIBoard[row+3][col] == ' ':
AIBoardHeatMap[row][col] += 1
#Carrier Check
if ships[0] == False and p2board[row][col] == ' ' and diff == 3:
if col-4 >= 0:
if AIBoard[row][col-1] == ' ' and AIBoard[row][col-2] == ' ' and AIBoard[row][col-3] == ' ' and AIBoard[row][col-4] == ' ':
AIBoardHeatMap[row][col] += 1
if col-3 >= 0 and col+1 <= 9:
if AIBoard[row][col-1] == ' ' and AIBoard[row][col-2] == ' ' and AIBoard[row][col-3] == ' ' and AIBoard[row][col+1] == ' ':
AIBoardHeatMap[row][col] += 1
if col-2 >= 0 and col+2 <= 9:
if AIBoard[row][col-1] == ' ' and AIBoard[row][col-2] == ' ' and AIBoard[row][col+1] == ' ' and AIBoard[row][col+2] == ' ':
AIBoardHeatMap[row][col] += 1
if col-1 >= 0 and col+3 <= 9:
if AIBoard[row][col-1] == ' ' and AIBoard[row][col+1] == ' ' and AIBoard[row][col+2] == ' ' and AIBoard[row][col+3] == ' ':
AIBoardHeatMap[row][col] += 1
if col+4 <= 9:
if AIBoard[row][col+1] == ' ' and AIBoard[row][col+2] == ' ' and AIBoard[row][col+3] == ' ' and AIBoard[row][col+4] == ' ':
AIBoardHeatMap[row][col] += 1
if row-4 >= 0:
if AIBoard[row-1][col] == ' ' and AIBoard[row-2][col] == ' ' and AIBoard[row-3][col] == ' ' and AIBoard[row-4][col] == ' ':
AIBoardHeatMap[row][col] += 1
if row-3 >= 0 and row+1 <= 9:
if AIBoard[row-1][col] == ' ' and AIBoard[row-2][col] == ' ' and AIBoard[row-3][col] == ' ' and AIBoard[row-1][col] == ' ':
AIBoardHeatMap[row][col] += 1
if row-2 >= 0 and row+2 <= 9:
if AIBoard[row-1][col] == ' ' and AIBoard[row-2][col] == ' ' and AIBoard[row+1][col] == ' ' and AIBoard[row+2][col] == ' ':
AIBoardHeatMap[row][col] += 1
if row-1 >= 0 and row+3 <= 9:
if AIBoard[row-1][col] == ' ' and AIBoard[row+1][col] == ' ' and AIBoard[row+2][col] == ' ' and AIBoard[row+3][col] == ' ':
AIBoardHeatMap[row][col] += 1
if row+4 <= 9:
if AIBoard[row-1][col] == ' ' and AIBoard[row+2][col] == ' ' and AIBoard[row+3][col] == ' ' and AIBoard[row+4][col] == ' ':
AIBoardHeatMap[row][col] += 1
maxVal = 0
target = []
for row in range(0, 10):
for col in range(0, 10):
if AIBoardHeatMap[row][col] > maxVal:
maxVal = AIBoardHeatMap[row][col]
while True:
target = (rand.randint(0, 9), rand.randint(0, 9))
if AIBoardHeatMap[target[0]][target[1]] == maxVal:
return target
def target(ships):
global AIBoardHeatMap
AIBoardHeatMap = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
#For each space
for row in range(0, 10):
for col in range(0, 10):
if AIBoard[row][col] == 'X':
if col-1 >= 0 and AIBoard[row][col-1] == ' ':
AIBoardHeatMap[row][col-1] += 1
if col+1 <= 9 and AIBoard[row][col+1] == ' ':
AIBoardHeatMap[row][col+1] += 1
if row-1 >= 0 and AIBoard[row-1][col] == ' ':
AIBoardHeatMap[row-1][col] += 1
if row+1 <= 9 and AIBoard[row+1][col] == ' ':
AIBoardHeatMap[row+1][col] += 1
if col-1 >= 0 and col+1 <= 9:
if AIBoard[row][col+1] == ' ':
AIBoardHeatMap[row][col+1] += 1
if AIBoard[row][col-1] == 'X':
AIBoardHeatMap[row][col+1] += 1
if AIBoard[row][col+1] == 'O' or AIBoard[row][col+1] == 'X':
AIBoardHeatMap[row][col+1] = 0
if row-1 >= 0 and row+1 <= 9:
if AIBoard[row+1][col] == ' ':
AIBoardHeatMap[row+1][col] += 1
if AIBoard[row-1][col] == 'X':
AIBoardHeatMap[row+1][col] += 1
if AIBoard[row+1][col] == 'O' or AIBoard[row+1][col] == 'X':
AIBoardHeatMap[row+1][col] = 0
if col+1 <= 9 and col-1 >= 0:
if AIBoard[row][col-1] == ' ':
AIBoardHeatMap[row][col-1] += 1
if AIBoard[row][col+1] == 'X':
AIBoardHeatMap[row][col-1] += 1
if AIBoard[row][col-1] == 'O' or AIBoard[row][col-1] == 'X':
AIBoardHeatMap[row][col-1] = 0
if row+1 <= 9 and row-1 >= 0:
if AIBoard[row-1][col] == ' ':
AIBoardHeatMap[row-1][col] += 1
if AIBoard[row+1][col] == 'X':
AIBoardHeatMap[row-1][col] += 1
if AIBoard[row-1][col] == 'O' or AIBoard[row-1][col] == 'X':
AIBoardHeatMap[row-1][col] = 0
maxVal = 0
for row in range(0, 10):
for col in range(0, 10):
if AIBoardHeatMap[row][col] > maxVal:
maxVal = AIBoardHeatMap[row][col]
for row in range(0, 10):
for col in range(0, 10):
if AIBoardHeatMap[row][col] == maxVal:
return (row, col)