-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiplayer.py
702 lines (651 loc) · 29.7 KB
/
aiplayer.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#AI Players
import numpy as py
import random
import gamelogicfunctions as glf
import aifunctions as aif
'''random player: picks a random slot every time
also use as default for smarter players
'@param gameBoard - underlying matrix representing game grid
'@return coordinates of cell in which player decides to play
'@calling glf.isFull, glf.isMoveValid
'@caller gl.gameplay, randomMovePlus, randomMovePlus2, randomMovePlusPlus
'''
def randomMove(gameBoard):
if glf.isFull( gameBoard ):
return 0,0
numrows, numcolumns= py.shape(gameBoard)
randomRow= 5 #random.randint(0,numrows-1)
randomColumn= random.randint(0,numcolumns-1)
while not glf.isMoveValid( randomColumn, gameBoard):
#randomRow= random.randint(0,numrows-1)
randomColumn= random.randint(0,numcolumns-1)
#print "playing randomly at " ,randomRow, randomColumn
return randomRow, randomColumn
'''random player that tries to block opponent or win if it notices the possibility
'@param gameBoard - underlying matrix representing game grid
'@return coordinates of cell in which player decides to play
'@calling randomMove, aif.blockOpponent, glf.boardContainsWinner
'@caller gl.gamePlay
'''
def randomMovePlus(gameBoard):
move= None
res,winner,pos,direction= glf.boardContainsWinner(gameBoard,3)
if res:
move= aif.blockOpponent(gameBoard, pos, direction)
else:
move= randomMove(gameBoard)
if not move:
return randomMove(gameBoard)
else:
return move
'''random player that blocks opponent or wins if possible
'@param gameBoard - underlying matrix representing game grid
'@return coordinates of cell in which player decides to play
'@calling randomMove, aif.blockOpponent, glf.getSequentialCells
'''
def randomMovePlus2(gameBoard, playerTurn):
move= None
sequentialCells= glf.getSequentialCells(gameBoard,3)
if playerTurn == 1:
possibleWins= sequentialCells[1]
possibleLosses= sequentialCells[2]
elif playerTurn == 2:
possibleWins= sequentialCells[2]
possibleLosses= sequentialCells[1]
#try to win
for pos, direction in possibleWins:
move= aif.blockOpponent(gameBoard, pos, direction)
if move:
#print "WOOOOOOOONNN, playing: ", move
break
if not move:
#print "TRY TO BLOCK OPPONENT"
for pos, direction in possibleLosses:
move= aif.blockOpponent(gameBoard, pos, direction)
if move:
#print "BLOCKING OPPONENT, playing: " , move
break
if not move:
#print "COULD NOT BLOCK OR WIN, playing random"
return randomMove(gameBoard)
else:
return move
'''random player that blocks opponent or wins if possible
'@param gameBoard - underlying matrix representing game grid
'@return coordinates of cell in which player decides to play, and a flag for randomness
'@calling randomMove, aif.blockOpponent, glf.getSequentialCells
'@caller gl.gamePlay, bestLocalMovePlus, randomOffense
'''
def randomMovePlusPlus(gameBoard, playerTurn):
move= None
sequentialCells= glf.getSequentialCellsPlus(gameBoard,4)
if playerTurn == 1:
possibleWins= sequentialCells[1]
possibleLosses= sequentialCells[2]
elif playerTurn == 2:
possibleWins= sequentialCells[2]
possibleLosses= sequentialCells[1]
#print "TRY TO WIN"
for pos, direction in possibleWins:
move= aif.blockOrWin(gameBoard, pos)
if move:
#print "WOOOOOOOONNN, playing: ", move
break
if not move:
#print "TRY TO BLOCK OPPONENT"
for pos, direction in possibleLosses:
move= aif.blockOrWin(gameBoard, pos)
if move:
#print "BLOCKING OPPONENT, playing: " , move
break
if not move:
#print "COULD NOT BLOCK OR WIN, playing random"
return randomMove(gameBoard),1
else:
return move,0
'''
'@param gameBoard - underlying matrix representing game grid
'@param playerTurn - playerID in game 1 or 2)
'@return best local move as determiend by our formula, None otherwise
'@spec formula: best slot based on scoreBoard
'@calling aif.scoreBoard, aif.isSafeToPlay
'@caller bestLocalMovePlus, gl.gamePlay
'''
def bestLocalMove(gameBoard, playerTurn):
#score board for this player and opponent
myScores, yourScores, candidateSlots= aif.scoreBoard(gameBoard, playerTurn)
#get positions
for score in sorted(candidateSlots.keys(), reverse=True):
nextBests= candidateSlots[score]
for x,y,player in nextBests:
if aif.isSafeToPlay((x,y), yourScores, gameBoard):
return x,y
else:
#print "CANNOT PLAY AT ", x, y,"-----------------------------------"
pass
'''
'@param gameBoard - underlying matrix representing game grid
'@param playerTurn - playerID in game 1 or 2)
'@return best move based on ourformula and current state, or random if cannot determine the best one
'@calling randomMovePlusPlus, bestLocalMove
'@caller gl.gamePlay, lookaAheadOne, lookAheadOnePlus
'''
def bestLocalMovePlus(gameBoard, playerTurn):
move,isRandom= randomMovePlusPlus(gameBoard, playerTurn)
if isRandom:
best= bestLocalMove(gameBoard, playerTurn)
if best:
return best, not isRandom
else:
#print "GOING TO PLAY RANDOMLY"
return move, not isRandom
else:
return move, not isRandom
'''
'@param gameBoard - underlying matrix representing game grid
'@param playerTurn - playerID in game 1 or 2)
'@return best move based on our formula and one look ahead, or random if cannot determine the best one
'@spec formula: best board based on isBetterState
'@calling bestLocalMovePlus, aif.isBetterState, aif.getValidMoves
'@caller gl.gamePlay
'''
def lookAheadOne(gameBoard, playerTurn):
#get all possible moves on gameBoard
possibleMoves= aif.getValidMoves(gameBoard)
#get best move based on state of resulting boards
bestMove, isBlockOrWin= bestLocalMovePlus(gameBoard, playerTurn)
if isBlockOrWin:
#there cannot be a better play than a block or a win
#print "FOUND IS BLOCK OR WIN FROM BESTLOCALMOVEPLUS"
return bestMove, isBlockOrWin
#find the best play that is neither a block or a win
bestBoard= py.copy(gameBoard)
bestBoard[bestMove]= playerTurn #py.zeros(py.shape(gameBoard))
for x,y in possibleMoves:
#play move
newBoard= py.copy(gameBoard)
newBoard[x,y]= playerTurn
isNewBetter, myScore, yourScore= aif.isBetterState(newBoard, bestBoard, playerTurn)
if isNewBetter == 1:
#print "FOUND SOMETHING BETTER THAN BESTLOCALMOVEPLUS: ", x,y
bestMove= (x,y)
bestBoard= newBoard
#return move leading to that state
return bestMove, isBlockOrWin
'''
'@param gameBoard - underlying matrix representing game grid
'@param playerTurn - playerID in game 1 or 2)
'@return best move based on our formula and two look aheads, or random if cannot determine the best one
'@spec formula: best board based on isBetterState
'@calling lookAheadOne, aif.getValidMoves, aif.getOpponent, aif.isBetterState
'@caller lookAheadThrice
'''
def lookAheadTwice(gameBoard, playerTurn):
#get all possible moves on gameBoard
possibleMoves= aif.getValidMoves(gameBoard)
#get best move based on state of resulting boards
myBestMove, isBlockOrWin= lookAheadOne(gameBoard, playerTurn)
if isBlockOrWin:
#print "FOUND IS BLOCK OR WIN FROM LOOKAHEADONE"
return myBestMove, isBlockOrWin
bestBoard= py.copy(gameBoard) #board state after opponent makes a move
#play my best move based on local state
bestBoard[myBestMove]= playerTurn
opponentTurn= aif.getOpponent(playerTurn)
yourBestMove, _= lookAheadOne(bestBoard, opponentTurn)
#play opponent's best move based on local state
bestBoard[yourBestMove]= opponentTurn
for x,y in possibleMoves:
#play move
newBoard= py.copy(gameBoard)
newBoard[x,y]= playerTurn
#get opponent's best move based on this new state
opponentMove, _= lookAheadOne(newBoard, opponentTurn)
opponentBoard= py.copy(newBoard)
opponentBoard[opponentMove]= opponentTurn
#score state opponent led to
isNewBetter, myScore, yourScore= aif.isBetterState(opponentBoard, bestBoard, opponentTurn)
if isNewBetter == 1:
#print "FOUND SOMETHING BETTER THAN lookAheadOne: ", x,y
myBestMove= (x,y)
bestBoard= opponentBoard
yourBestMove= opponentMove
return myBestMove, isBlockOrWin
'''
'@param gameBoard - underlying matrix representing game grid
'@param playerTurn - playerID in game 1 or 2)
'@return best move based on our formula and two look aheads, or random if cannot determine the best one
'@spec formula: best board based on isBetterState
'@calling lookAheadOne, lookAheadTwice, aif.getValidMoves, aif.getOpponent, aif.isBetterState
'@caller gl.gamePlay
'''
def lookAheadThrice(gameBoard, playerTurn):
#get all possible moves on gameBoard
possibleMoves= aif.getValidMoves(gameBoard)
#get best move based on state of resulting boards
myBestMove, isBlockOrWin= lookAheadTwice(gameBoard, playerTurn)
if isBlockOrWin:
#print "NOT GONNA BOTHER, FOUND BLOCK OR WIN"
return myBestMove, isBlockOrWin
bestBoard= py.copy(gameBoard)
#play my best move based on local state
bestBoard[myBestMove]= playerTurn
opponentTurn= aif.getOpponent(playerTurn)
yourBestMove, _= lookAheadTwice(bestBoard, opponentTurn)
#print "lookAheadThrice -- yourBestMove ", yourBestMove
#play opponent's best move based on local state
bestBoard[yourBestMove]= opponentTurn
myOtherBestMove, _= lookAheadTwice(bestBoard, playerTurn)
bestBoard[myOtherBestMove]= playerTurn
for x,y in possibleMoves:
#play move
newBoard= py.copy(gameBoard)
newBoard[x,y]= playerTurn
#get opponent's best move based on this new state
opponentMove, _= lookAheadTwice(newBoard, opponentTurn)
newBoard[opponentMove]= opponentTurn
#get my next best move based on this new state
myMove, _= lookAheadOne(newBoard, playerTurn)
newBoard[myMove]= playerTurn
#score this 'final' state
isNewBetter, myScore, yourScore= aif.isBetterState(newBoard, bestBoard, playerTurn)
#print isNewBetter, myScore, yourScore
if isNewBetter == 1:
#print "FOUND SOMETHING BETTER THAN lookAheadTwice: ", x,y
bestBoard= newBoard
myBestMove= (x,y)
return myBestMove, not isBlockOrWin
'''
'@param gameBoard - underlying matrix representing game grid
'@param playerTurn - playerID in game 1 or 2)
'@return best move based on our formula and one look ahead, or random if cannot determine the best one
'@spec formula: best board based on isSafeToPlay and numb sequentialCells (3)
'@calling bestLocalMovePlus, aif.getValidMoves, aif.scoreBoard, aif.getOpponent, glf.getSequentialCellsPlus, aif.isSafeToPlay
'@caller gamePlay
'''
def lookAheadOnePlus(gameBoard, playerTurn):
#get all possible moves on gameBoard
possibleMoves= aif.getValidMoves(gameBoard)
#get best move based on state of resulting boards
bestMove, isBlockOrWin= bestLocalMovePlus(gameBoard, playerTurn)
x,y= bestMove
#score board for this player and opponent
myOrScores, yourOrScores, orCandidateSlots= aif.scoreBoard(gameBoard, playerTurn)
if not aif.isSafeToPlay((x,y), yourOrScores, gameBoard):
#CAN USE THIS TO PREVENT CONNECT FOUR TRAPS
#print "BEST MOVE IS A LOSS?!?!?!"
pass
if isBlockOrWin:
#there cannot be a better play than a block or a win
#print "FOUND IS BLOCK OR WIN FROM BESTLOCALMOVEPLUS"
return bestMove, isBlockOrWin
#find the best play that is neither a block or a win
bestBoard= py.copy(gameBoard)
bestBoard[bestMove]= playerTurn #py.zeros(py.shape(gameBoard))
opponentTurn= aif.getOpponent(playerTurn)
for x,y in possibleMoves:
#play move
newBoard= py.copy(gameBoard)
newBoard[x,y]= playerTurn
oldSequentialCells= glf.getSequentialCellsPlus( bestBoard, 3 )
oldWinOpportunities= oldSequentialCells[playerTurn]
oldLoseOpportunities= oldSequentialCells[opponentTurn]
newSequentialCells= glf.getSequentialCellsPlus( newBoard, 3 )
newWinOpportunities= newSequentialCells[playerTurn]
newLoseOpportunities= newSequentialCells[opponentTurn]
myScores, yourScores, candidateSlots= aif.scoreBoard(newBoard, playerTurn)
if aif.isSafeToPlay((x,y), yourOrScores, gameBoard) and len(newLoseOpportunities) < len(oldLoseOpportunities):
#print "FOUND SOMETHING BETTER THAN BESTLOCALMOVEPLUS: ", x,y
bestMove= (x,y)
bestBoard= newBoard
#return move leading to that state
return bestMove, isBlockOrWin
'''
'@param gameBoard - underlying matrix representing game grid
'@param playerTurn - playerID in game 1 or 2)
'@return best move based on our formula and one look ahead, or random if cannot determine the best one
'@spec formula: best board based on is playable and sequentiallCells (3)
'@calling lookAheadOnePlus, aif.getValidMoves, aif.scoreBoard, aif.getOpponent,
glf.getSequentialCellsPlus, aif.isSafeToPlay, aif.isSafeToPlayPlus, aif.preventTrapPlus replaced by aif.blockTrap
'@caller gl.gamePlay, lookAheadThricePlus
'''
def lookAheadTwicePlus(gameBoard, playerTurn):
#get all possible moves on gameBoard
possibleMoves= aif.getValidMoves(gameBoard)
#get best move based on state of resulting boards
bestMove, isBlockOrWin= lookAheadOnePlus(gameBoard, playerTurn)
x,y= bestMove
#score board for this player and opponent
myOrScores, yourOrScores, orCandidateSlots= aif.scoreBoard(gameBoard, playerTurn)
if not aif.isSafeToPlay((x,y), yourOrScores, gameBoard):
#print "lookAheadTwicePlus: -- BEST MOVE IS A LOSS?!?!?!"
pass
if isBlockOrWin:
#there cannot be a better play than a block or a win
return bestMove, isBlockOrWin
bestBoard= py.copy(gameBoard)
bestBoard[bestMove]= playerTurn
trapFlag= 0
slot= (-1,-1)
opponentTurn= aif.getOpponent(playerTurn)
opponentPossibleMoves= aif.getValidMoves(bestBoard)
for x,y in opponentPossibleMoves:
temp= py.copy(bestBoard)
temp[x,y]= opponentTurn
t, s= aif.blockTrap(gameBoard,bestMove, (x,y), temp, playerTurn)
if t == 1 and aif.isSafeToPlayPlus( s, playerTurn, gameBoard ):
#print " PREVENTING TRAAAAAAAAAP IN DEFAULT LOOKAHEADTWICEPLUS ----------------------------"
return s, 2
elif t == -1:
trapFlag= t
slot= s
break
elif t == 0:
pass
#get best move for opponent
yourBestMove, _= lookAheadOnePlus(bestBoard, opponentTurn)
bestBoard[yourBestMove]= opponentTurn
#find the best play that is neither a block or a win
for x,y in possibleMoves:
if trapFlag == -1 and (x,y) == slot:
#print "AVOIDING AVOINDING AVOIDING AVOIDING AVOIDING"
pass
else:
#play move
newBoard= py.copy(gameBoard)
newBoard[x,y]= playerTurn
#get opponent move
opponentMove, _= lookAheadOnePlus(gameBoard, opponentTurn)
newBoard[opponentMove]= opponentTurn
if trapFlag == -1 and bestMove == slot and (x,y) != slot and aif.isSafeToPlayPlus( (x,y), playerTurn, gameBoard ):
#print "REPLACING BAD BEST MOVE ", bestMove, " LEADING TO TRAP WITH ", x,y
bestMove= (x,y)
bestBoard= newBoard
trapFlag= 0
oldSequentialCells= glf.getSequentialCellsPlus( bestBoard, 3 )
oldWinOpportunities= oldSequentialCells[playerTurn]
oldLoseOpportunities= oldSequentialCells[opponentTurn]
newSequentialCells= glf.getSequentialCellsPlus( newBoard, 3 )
newWinOpportunities= newSequentialCells[playerTurn]
newLoseOpportunities= newSequentialCells[opponentTurn]
myScores, yourScores, candidateSlots= aif.scoreBoard(newBoard, playerTurn)
if aif.isSafeToPlay((x,y), yourOrScores, gameBoard) and len(newLoseOpportunities) < len(oldLoseOpportunities):
#print "FOUND SOMETHING BETTER THAN LOOKAHEADONE: ", x,y
bestMove= (x,y)
bestBoard= newBoard
#return move leading to that state
if trapFlag == -1 and bestMove == slot:
#print "OOOOOOOOOOOOH NOOOOooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo DON'T PLAY THERE"
pass
return bestMove, isBlockOrWin
'''
'@param gameBoard - underlying matrix representing game grid
'@param playerTurn - playerID in game 1 or 2)
'@return best move based on our formula and one look ahead, or random if cannot determine the best one
'@spec formula: best board based on isPlayable and number of sequentialCells (4)
'@calling lookAheadTwicePlus, aif.getValidMoves, aif.scoreBoard, aif.getOpponent, glf.getSequentialCellsPlus, aif.isSafeToPlay,
aif.preventTrapPlus
'@caller gamePlay
'''
def lookAheadThricePlus(gameBoard, playerTurn):
#get all possible moves on gameBoard
possibleMoves= aif.getValidMoves(gameBoard)
#get best move based on state of resulting boards
bestMove, isBlockOrWin= lookAheadTwicePlus(gameBoard, playerTurn)
x,y= bestMove
#score board for this player and opponent
myOrScores, yourOrScores, orCandidateSlots= aif.scoreBoard(gameBoard, playerTurn)
if not aif.isSafeToPlay((x,y), yourOrScores, gameBoard):
#print "lookAheadThricePlus: -- BEST MOVE IS A LOSS?!?!?!"
pass
if isBlockOrWin:
#there cannot be a better play than a block or a win
#print "lookAheadThricePlus: --- FOUND IS BLOCK OR WIN FROM BESTLOCALMOVEPLUS"
return bestMove, isBlockOrWin
elif isBlockOrWin == 2:
return bestMove, 2
bestBoard= py.copy(gameBoard)
bestBoard[bestMove]= playerTurn #py.zeros(py.shape(gameBoard))
trapFlag= 0
slot= (-1,-1)
opponentTurn= aif.getOpponent(playerTurn)
opponentPossibleMoves= aif.getValidMoves(bestBoard)
for x,y in opponentPossibleMoves:
temp= py.copy(bestBoard)
temp[x,y]= opponentTurn
t, s= aif.preventTrapPlus(gameBoard,bestMove, (x,y), temp, playerTurn)
if t == 1:
#print " PREVENTING TRAAAAAAAAAP IN DEFAULT LOOKAHEADTHRICEPLUS ----------------------------"
return s, 2
elif t == -1:
trapFlag= t
slot= s
break
elif t == 0:
pass
#get best move for opponent
yourBestMove, _= lookAheadTwicePlus(bestBoard, opponentTurn)
bestBoard[yourBestMove]= opponentTurn
myOtherBestMove, _= lookAheadTwicePlus(bestBoard, playerTurn)
bestBoard[myOtherBestMove]= playerTurn
#find the best play that is neither a block or a win
for x,y in possibleMoves:
if (x,y) == slot:
#print "AVOIDING AVOINDING AVOIDING AVOIDING AVOIDING"
pass
else:
#play move
newBoard= py.copy(gameBoard)
newBoard[x,y]= playerTurn
newBoardAfterMyFirstTurn= py.copy(newBoard)
#get opponent move
opponentMove, _= lookAheadTwicePlus(gameBoard, opponentTurn)
newBoard[opponentMove]= opponentTurn
flag2,slot2= aif.preventTrapPlus(gameBoard, (x,y), opponentMove, newBoard, playerTurn)
if trapFlag == -1 and flag2 != -1 and aif.isSafeToPlayPlus( (x,y), playerTurn, gameBoard ):
#print "ThricePlus: REPLACING BAD BEST MOVE ", bestMove, " LEADING TO TRAP WITH ", x,y
bestMove= (x,y)
bestBoard= newBoard
trapFlag= 0
#get my next best move based on this new state
#TODO if final state has trap or is loss, discard this original move completely. If not, move on to compare this state to the best state seen.
#TODO check if no traps for opponent but trap for self, then play there
myMove, _= lookAheadTwicePlus(newBoard, playerTurn)
newBoard[myMove]= playerTurn
flag3,slot3= aif.preventTrapPlus(newBoardAfterMyFirstTurn, opponentMove, myMove, newBoard, opponentTurn)
if flag3 == 1 and aif.isSafeToPlayPlus( slot3, playerTurn, gameBoard ):
#Trap already active in our favor, use it!
return slot3, 1
if flag3 == -1 and aif.isSafeToPlayPlus( slot3, playerTurn, gameBoard ):
#opponent made a move that activated the trap: consider our move that led to it as a valid place to play
pass
if flag2== -1:
pass
else:
oldSequentialCells= glf.getSequentialCellsPlus( bestBoard, 4 )
oldWinOpportunities= oldSequentialCells[playerTurn]
oldLoseOpportunities= oldSequentialCells[opponentTurn]
newSequentialCells= glf.getSequentialCellsPlus( newBoard, 4 )
newWinOpportunities= newSequentialCells[playerTurn]
newLoseOpportunities= newSequentialCells[opponentTurn]
myScores, yourScores, candidateSlots= aif.scoreBoard(newBoard, playerTurn)
if aif.isSafeToPlay((x,y), yourOrScores, gameBoard) and len(newLoseOpportunities) < len(oldLoseOpportunities):
bestMove= (x,y)
bestBoard= newBoard
#return move leading to that state
if trapFlag == -1 and bestMove == slot:
#print "OOOOOOOOOOOOH NOOOOooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo DON'T PLAY THERE"
pass
return bestMove, isBlockOrWin
########################################### START OFFENSIVE PLAYERS ###############################################
'''
'@param gameBoard - matrix representing game grid
'@param playerTurn - player making the next move:the caller
'@return best offensive move to make according to our formula, or random if cannot find one
'@spec formula: make chains of coins in order to get closer to 4 in a row
'@calling randomMovePlusPlus, aif.getValidMoves, aif.uselessSlotFilter,
'@caller gl.gameplay
'''
def randomOffense(gameBoard, playerTurn):
move,isRandom= randomMovePlusPlus( gameBoard, playerTurn )
if not isRandom:
#print "RandomOffense --- blocking"
return move, not isRandom
validMoves= aif.getValidMoves( gameBoard )
filterWorked, validMoves= aif.uselessSlotFilter( gameBoard, validMoves, playerTurn )
if filterWorked:
#choose move with higher number
move= (-1,-1)
bestVal= -1
for (x,y,value) in validMoves:
if value > bestVal:
move= (x,y)
bestVal= value
return move, not isRandom
return move, not isRandom
'''
'@param gameBoard - matrix representing game grid
'@param playerTurn - player making the next move:the caller
'@return best offensive move to make according to our formula, or best defensive move from lookAheadTwicePlus
'@spec formula: make chains of coins in order to get closer to 4 in a row
'@calling lookAheadTwicePlus, aif.getValidMoves, aif.uselessSlotFilter, aif.blockSingleLineTrap
'@caller gl.gameplay, randomOffenseOneWithTwicePlus
'''
def randomOffenseWithTwicePlus(gameBoard, playerTurn):
move,isBlockOrWin= lookAheadTwicePlus(gameBoard,playerTurn)
if isBlockOrWin == 2 or isBlockOrWin:
return move, isBlockOrWin
'''isSingleLineTrap, blockingMove= aif.blockSingleLineTrap(gameBoard, playerTurn)
if isSingleLineTrap:
return blockingMove, isSingleLineTrap'''
validMoves= aif.getValidMoves( gameBoard )
filterWorked, validMoves= aif.uselessSlotFilter( gameBoard, validMoves, playerTurn )
if filterWorked:
#choose move with higher number
bestVal= -1
for (x,y,value) in validMoves:
if value > 1 and value > bestVal and aif.isSafeToPlayPlus( (x,y), playerTurn, gameBoard ):
move= (x,y)
bestVal= value
return move, 0
return move, 0
'''
'@param gameBoard - matrix representing game grid
'@param playerTurn - player making the next move:the caller
'@return best offensive move to make according to our formula, or best defensive move from randomOffenseWithTwicePlus
'@spec formula: make chains of coins in order to get closer to 4 in a row, by looking one step ahead
'@calling randomOffenseWithTwicePlus, aif.getValidMoves, aif.uselessSlotFilter, aif.isSafeToPlayPlus
'@caller gl.gameplay, randomOffenseOneWithTwicePlus
'''
def randomOffenseOneWithTwicePlus( gameBoard, playerTurn ):
move,isBlockOrWin= randomOffenseWithTwicePlus(gameBoard,playerTurn)
if isBlockOrWin == 2 or isBlockOrWin:
return move, isBlockOrWin
#consider all my possible moves
validMoves= aif.getValidMoves( gameBoard )
bestMove= move
bestNumPlayerIDs= -1
for (x,y) in validMoves:
#simulate this move, and assume opponent plays 'best' possible move.
#the best move from me is the one leaving the state with a higher
#sequence of my playerID as a valid win AFTER opponent's 'best' move
tempBoard= py.copy(gameBoard)
tempBoard[x,y]= playerTurn
opponentTurn= aif.getOpponent(playerTurn)
oppMove, _= randomOffenseWithTwicePlus(tempBoard, opponentTurn)
tempBoard[oppMove]= opponentTurn
#get new valid moves and new uselessSlotFilter values. Compare with
#best seen thus far
newValidMoves= aif.getValidMoves( tempBoard )
filterWorked, newValidMoves= aif.uselessSlotFilter( tempBoard, newValidMoves, playerTurn )
if filterWorked:
for (_,_,value) in newValidMoves:
if value > 1 and value > bestNumPlayerIDs and aif.isSafeToPlayPlus( (x,y), playerTurn, gameBoard ):
bestMove= (x,y)
bestNumPlayerIDs= value
return bestMove, 0
'''
'@param matrix representing game grid
'@param playerTurn - player making the next move:the caller
'@return best move as determined by aif.nextMove
'@calling aif.nextMove
'@caller gl.gamePlay
'@NOTE NOT USED
'''
def forwardEval( gameBoard, playerTurn ):
'''move, flag= randomOffenseWithTwicePlus( gameBoard, playerTurn )
if flag != 0:
return move, flag'''
move2, _,_= aif.nextMove( gameBoard, 3, playerTurn )
#if move2:
return move2, 0
'''
'@param trainPlies - training data in list form Each element is a list of 42 slots for the board followed by a label.
'@param gameBoard - our original 6x7 matrix representation of our board
'@param playerTurn - the player running the analysis
'@return the best local move as determined by knn (no look-ahead)
'@calling aif.getValidMoves
'@caller gl.gamePlay
'''
def knnPlayer( trainPlies, gameBoard, playerTurn ):
bestMove= (None,None)
bestAcc= -float("inf")
#consider your next step
validMoves= aif.getValidMoves( gameBoard )
for (x, y) in validMoves:
gameBoard[x,y]= playerTurn
plie= []
numrows, numColumns= py.shape(gameBoard)
for i in range(0,numColumns):
plie+= reversed( gameBoard[0:numrows,i] )
acc= aif.knn( 150, trainPlies, plie, playerTurn )
if acc > bestAcc:
#probably win for us
bestMove= (x,y)
bestAcc= acc
gameBoard[x,y]= 0
return bestMove,0
'''
'@param trainPlies - training data in list form Each element is a list of 42 slots for the board followed by a label
'@param gameBoard - our original 6x7 matrix representation of our board
'@param playerTurn - the player running the analysis
'@return the best move as determined by minimax algorithm with weighted-knn as a scoring function
'@spec this creates a game tree and assigns scores to each node using the scoring function and minimax.
depth of the tree is determined by a tunable parameter to aif.Tree (number of turns)
'@caller gl.gamePlay
'@calling aif.Tree
'''
def minimaxKnn( trainPlies, gameBoard, playerTurn ):
depth= min( 4, 42- py.count_nonzero( gameBoard ) )
gameTree= aif.Tree(gameBoard, depth, trainPlies, playerTurn, "knn")
childrenNodes= gameTree.structure[ gameTree.structure[ 0 ] ]
bestAcc= gameTree.structure[ 0 ].value
bestMove= (None,None)
for child in childrenNodes:
if child.value == bestAcc:
bestMove= child.move
return bestMove, 0
else:
pass
'''
'@param trainPlies - NOT USED training data in list form Each element is a list of 42 slots for the board followed by a label
'@param gameBoard - our original 6x7 matrix representation of our board
'@param playerTurn - the player running the analysis
'@return the best move as determined by minimax algorithm with getSequentialCellsPlus as a scoring function
'@spec this creates a game tree and assigns scores to each node using the scoring function and minimax.
depth of the tree is determined by a tunable parameter to aif.Tree (number of turns)
'@caller gl.gamePlay
'@calling aif.Tree
'''
def minimaxSeqCellsPlus( trainPlies, gameBoard, playerTurn ):
depth= min( 4, 42- py.count_nonzero( gameBoard ) )
gameTree= aif.Tree(gameBoard, depth, trainPlies, playerTurn, "getSequentialCellsPlus")
childrenNodes= gameTree.structure[ gameTree.structure[ 0 ] ]
bestAcc= gameTree.structure[ 0 ].value
bestMove= (None,None)
for child in childrenNodes:
if child.value == bestAcc:
bestMove= child.move
return bestMove, 0
else:
pass