forked from Scriptim/Snake-Battle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
498 lines (393 loc) · 15.5 KB
/
ai.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
from __future__ import division
import sys, os, random, copy, time
## directions
RIGHT = (-1, 0)
UP = (0, -1)
DOWN = (0, 1)
LEFT = (1, 0)
MOVES = [UP, RIGHT, DOWN, LEFT]
def oppDir(direction):
if (direction == UP): return DOWN
if (direction == DOWN): return UP
if (direction == RIGHT): return LEFT
if (direction == LEFT): return RIGHT
def processMove(moveDir, direction):
if direction == UP:
if moveDir == UP:
return ''
if moveDir == DOWN:
return ''
if moveDir == LEFT:
return 'left'
if moveDir == RIGHT:
return 'right'
if direction == DOWN:
if moveDir == UP:
return ''
if moveDir == DOWN:
return ''
if moveDir == LEFT:
return 'right'
if moveDir == RIGHT:
return 'left'
if direction == LEFT:
if moveDir == UP:
return 'right'
if moveDir == DOWN:
return 'left'
if moveDir == LEFT:
return ''
if moveDir == RIGHT:
return ''
if direction == RIGHT:
if moveDir == UP:
return 'left'
if moveDir == DOWN:
return 'right'
if moveDir == LEFT:
return ''
if moveDir == RIGHT:
return ''
SIZE = 70
COLOR_P1 = (255, 30, 30) # player 1
COLOR_P2 = (30, 255, 30) # player 2
# Could probably benefit from inheritance but keeping it open to flexibility
''' Simple AI that only moves diagonally
'''
class Simple_ai:
def __init__(self, direction, marked_tiles, currHead, oppHead):
self.toggle = True
self.direction = direction
self.marked = marked_tiles
self.choice = UP
self.head = currHead
self.oppHead = oppHead
def computeMove(self, marked_tiles, currHead, oppHead, direction, oppDirUnused):
#Set fields
self.marked = marked_tiles
self.head = currHead
self.oppHead = oppHead
# print(self.toggle)
self.toggle = not self.toggle
if (self.toggle): return 'left'
else: return 'right'
''' Purely Random Player
Returns 'left' or 'right' decision randomly
'''
class Random_ai:
def __init__(self, direction, marked_tiles, currHead, oppHead):
self.direction = direction
self.marked = marked_tiles
self.choice = UP
self.head = currHead
self.oppHead = oppHead
def computeMove(self, marked_tiles, currHead, oppHead, direction, oppDirUnused):
result = random.randint(0,2)
if (result == 0): return 'left'
else: return 'right'
''' Original AI from the Hackathon
Simply looks 'n' steps forward and chooses the lowest cost path.
Recalculates this path after every move.
Does not account for movements of opponent.
Only accounts for current board state.
'''
class Original_ai:
def __init__(self, direction, marked_tiles, currHead, oppHead):
self.toggle = True
self.direction = direction
self.marked = marked_tiles
self.marked[currHead] = 1
self.marked[oppHead] = 1
self.choice = UP
self.head = currHead
self.oppHead = oppHead
self.score = 0
self.board = [[0 for _ in range(SIZE)] for _ in range(SIZE)]
self.setBoundaries()
#set the boundaries to be walls
def setBoundaries(self):
for i in range(SIZE):
self.board[0][i] = 1
self.board[i][0] = 1
self.board[SIZE-1][i] = 1
self.board[i][SIZE-1] = 1
def updateBoard(self, head, move):
board = copy.deepcopy(self.board)
#general update
for i in range (SIZE):
for j in range (SIZE):
if (i,j) in self.marked:
board[i][j] = 1
#add heads
board[head[0]][head[1]] = 1
board[self.oppHead[0]][self.oppHead[1]] = 1
nextMove = (head[0] + move[0], head[1]+move[1])
if (nextMove[0] < 0 or nextMove[1] < 0 or nextMove[0] >= SIZE or nextMove[1] >= SIZE):
return None
board[nextMove[0]][nextMove[1]] = 1
return board
def getScore(self,board,head):
exp = 2
# Score = sum of distances to obstacles
# Further away = higher score. Must be exponential to matter.
dist_top = head[1] **exp #distance FROM top
dist_bot = (SIZE - head[1]) **exp
dist_left = head[0] **exp
dist_right = (SIZE - head[0]) **exp
# if dist_top < 2: dist_top = -1000
# if dist_bot < 2: dist_bot = -1000
# if dist_left < 2: dist_left = -1000
# if dist_right < 2: dist_right = -1000
# print ("Distance from top ", dist_top)
# print ("Distance from bot ", dist_bot)
# print ("Distance from left ", dist_left)
# print ("Distance from right ", dist_right)
total = dist_top+dist_bot+dist_left+dist_right
return total
def moveHead(self, head, move):
if (not head):
return None
newHead = (head[0]+move[0], head[1]+move[1])
if (newHead[0] < 0 or newHead[1] < 0 or newHead[0] >= SIZE or newHead[1] >= SIZE):
return None
return newHead
def computeMove(self, marked_tiles, currHead, oppHead, direction, oppDirUnused):
#Set fields
self.marked = marked_tiles
self.head = currHead
self.oppHead = oppHead
board = [[]]
head = self.head
score = 0
curr_score = self.getScore(board, head)
max_score = curr_score #self.score
max_score_dir = LEFT
#Simulate 4 possible moves for current player
for move in MOVES:
head = self.head
if(move != oppDir(direction)):
#Update board with 1 square added
head = self.moveHead(head, move)
if (not head): continue
#Compute score for this board
score = self.getScore(board, head)
# print (score, max_score)
# print ("Score for moving ", move, " is ", score)
if (score >= max_score):
max_score = score
max_score_dir = move
curr_score = score
return processMove(max_score_dir, direction)
''' Runs Adverserial search to go down the path that gives maximum reward
Look 3 steps ahead (depth 3) and computes total reward of each path possible
Takes the path with highest reward - and RECALCULATES at every step to ensure
that the best possible path is still being taken
'''
class Adverse_ai:
def __init__(self, direction, marked_tiles, currHead, oppHead):
self.toggle = True
self.direction = direction
self.marked = marked_tiles
self.marked[currHead] = 1
self.marked[oppHead] = 1
self.choice = UP
self.head = currHead
self.oppHead = oppHead
self.score = 0
self.board = [[0 for _ in range(SIZE)] for _ in range(SIZE)]
self.setBoundaries()
#set the boundaries to be walls
def setBoundaries(self):
for i in range(SIZE):
self.board[0][i] = 1
self.board[i][0] = 1
self.board[SIZE-1][i] = 1
self.board[i][SIZE-1] = 1
def updateBoard(self, head, move):
board = copy.deepcopy(self.board)
#general update
for i in range (SIZE):
for j in range (SIZE):
if (i,j) in self.marked:
board[i][j] = 1
#add heads
board[head[0]][head[1]] = 1
board[self.oppHead[0]][self.oppHead[1]] = 1
nextMove = (head[0] + move[0], head[1]+move[1])
if (nextMove[0] < 0 or nextMove[1] < 0 or nextMove[0] >= SIZE or nextMove[1] >= SIZE):
return None
board[nextMove[0]][nextMove[1]] = 1
return board
def getScore(self,board,head):
exp = 2
# Score = sum of distances to obstacles
# Further away = higher score. Must be exponential to matter.
dist_top = head[1] **exp #distance FROM top
dist_bot = (SIZE - head[1]) **exp
dist_left = head[0] **exp
dist_right = (SIZE - head[0]) **exp
total = dist_top+dist_bot+dist_left+dist_right
return total
def moveHead(self, head, move):
if (not head):
return None
newHead = (head[0]+move[0], head[1]+move[1])
if (newHead[0] < 0 or newHead[1] < 0 or newHead[0] >= SIZE or newHead[1] >= SIZE):
return None
return newHead
def computeMove(self, marked_tiles, currHead, oppHead, direction, oppDirection):
#Set fields
self.marked = marked_tiles
self.head = currHead
self.oppHead = oppHead
playerScores = []
oppScores = []
board = [[]]
head = self.head
score = 0
curr_score = self.getScore(board, head)
max_score = curr_score #self.score
max_score_dir = LEFT
#Simulate 4 possible moves for current player
for move in MOVES:
head = self.head
if(move != oppDir(direction)):
#Update board with 1 square added
head = self.moveHead(head, move)
if (not head): continue
#Compute score for this board
score = self.getScore(board, head)
#Check 4 possible moves of the opponent and get their 4 scores, and min of those
oppScores = []
for oppMove in MOVES:
if(oppMove != oppDir(oppDirection)):
#Update board with 1 move from Opponent's Head
oppHead = self.moveHead(oppHead, oppMove)
if (not oppHead): continue
oppScore = self.getScore(board, oppHead)/100
#Check 4 possible moves of the player and get the max score
playerScores = []
for playerMove in MOVES:
head2 = head
if(playerMove != oppDir(move)):
head2 = self.moveHead(head2, playerMove)
if (not head2): continue
#Compute score for this board
playerScore = self.getScore(board, head2)/100
playerScores.append(playerScore)
oppScores.append(oppScore)
# print (score)
if (oppScores): score += min(oppScores)
# print (score)
if (playerScores) : score += max(playerScores)
# print (score)
if (score >= max_score):
max_score = score
max_score_dir = move
curr_score = score
return processMove(max_score_dir, direction)
# class Adverse_ai:
# def __init__(self, direction, marked_tiles, currHead, oppHead):
# self.toggle = True
# self.direction = direction
# self.marked = marked_tiles
# self.choice = UP
# self.head = currHead
# self.oppHead = oppHead
# self.board = [[0 for _ in range(SIZE)] for _ in range(SIZE)]
# self.toggle = True
# #set the boundaries to be walls
# def setBoundaries(self):
# for i in range(SIZE):
# self.board[0][i] = 1
# self.board[i][0] = 1
# self.board[SIZE-1][i] = 1
# self.board[i][SIZE-1] = 1
# def updateBoard(self, move):
# board = copy.deepcopy(self.board)
# #general update
# for i in range (SIZE):
# for j in range (SIZE):
# if (i,j) in self.marked:
# board[i][j] = 1
# #add heads
# board[self.head[0]][self.head[1]] = 1
# board[self.oppHead[0]][self.oppHead[1]] = 1
# nextMove = self.head + move
# if (nextMove[0] < 0 or nextMove[1] < 0):
# return False
# board[nextMove[0]][nextMove[1]] = 1
# return board
# def moveHead(self, head, move):
# if (not head):
# return None
# newHead = (head[0]+move[0], head[1]+move[1])
# if (newHead[0] < 0 or newHead[1] < 0 or newHead[0] >= SIZE or newHead[1] >= SIZE):
# return None
# return newHead
# def getScore(self,board,head):
# exp = 2
# # Score = sum of distances to obstacles
# # Further away = higher score. Must be exponential to matter.
# dist_top = head[1] **exp #distance FROM top
# dist_bot = (SIZE - head[1]) **exp
# dist_left = head[0] **exp
# dist_right = (SIZE - head[0]) **exp
# total = dist_top+dist_bot+dist_left+dist_right
# return total
# def computeMove(self, marked_tiles, currHead, oppHead, direction, oppDirection):
# #Set fields
# self.marked = marked_tiles
# self.head = currHead
# self.oppHead = oppHead
# board = [[]]
# head = self.head
# score = 0
# curr_score = self.getScore(board, head)
# max_score = curr_score #self.score
# max_score_dir = LEFT
# #Simulate 4 possible moves for current player
# for move in MOVES:
# head = self.head
# if(move != oppDir(direction)):
# #Update board with 1 square added
# head = self.moveHead(head, move)
# if (not head): continue
# #Compute score for this board
# score = self.getScore(board, head)
# # print (score, max_score)
# # print ("Score for moving ", move, " is ", score)
# if (score >= max_score):
# max_score = score
# max_score_dir = move
# curr_score = score
# board = [[]]
# head = self.head
# score = 0
# curr_score = self.getScore(board, head)
# max_score = curr_score #self.score
# max_score_dir = LEFT
# #Simulate 4 possible moves for current player
# for move in MOVES:
# head = self.head
# oppHead = self.oppHead
# if(move != oppDir(direction)):
# #Update board with 1 square added
# head = self.moveHead(head, move)
# if (not head): continue
# #Compute score for this board
# score = self.getScore(board, head)
# if (score > max_score):
# max_score = score
# max_score_dir = move
# curr_score = score
# return processMove(max_score_dir, dir)
#Check 4 possible moves of the opponent and get their 4 scores, and min of those
# oppScores = []
# for oppMove in MOVES:
# if(oppMove != oppDir(oppDirection)):
# #Update board with 1 move from Opponent's Head
# oppHead = self.moveHead(oppHead, oppMove)
# if (not oppHead): continue
# oppScores.append(self.getScore(board, oppHead))
# score += min(oppScores)