-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluationUtilities.py
627 lines (574 loc) · 30.1 KB
/
evaluationUtilities.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
def checkColumn(board, val, col):
#only returns a count if there is an available slot above the streak of values
#start from the botton and decrement counter
counting = False
count = 0
for row in range(5, -1, -1):
if board[row][col] == val:
counting = True
count += 1
elif counting:
counting = False
if board[row][col] == " ":
#if empty spot above, nothing else in this column
if row == 0:
#top element in column is space, so count must be at least 3 for it to be valid
if count >= 3:
break
else:
count = 0
elif row == 1:
#second highest element in column is space, so count must be at least 2, with 2 empty spaces above, to be valid
if count >= 2:
break
else:
count = 0
else:
break
else:
#opponent value above streak, so dont want to include the count cause you cant do anything with this
count = 0
return count
def checkRow(board, val, row):
leftSet = False
leftIndices = []
rightSet = False
rightIndices = []
retDict = {2:0, 3:0, 4:0}
for col in range(7):
if board[row][col] == val:
if not leftSet:
#the first occurence of the val in the row
leftSet = True
leftIndices.append(col)
else:
if leftSet and col != 0:
#the last value in a streak
rightIndices.append(col-1)
#set to false to keep searching for more streaks
leftSet = False
if col == 6 and leftSet:
#loop reached the end of a row without getting a space or opponent value, so this is the desired value
rightIndices.append(col)
#check each pair of indices to evaluate the score
if len(leftIndices) != len(rightIndices):
print "ROW CHECK ERROR"
else:
for i in range(len(leftIndices)):
#check how many available spots on both sides of streak, if less than 4, then this is not a valid streak
leftSpotsFree = 0
leftPlayableSpotsFree = 0
rightSpotsFree = 0
rightPlayableSpotsFree = 0
leftIndex = leftIndices[i]
rightIndex = rightIndices[i]
#add 1 to account for zero indexing
streakLength = rightIndex - leftIndex + 1
if streakLength < 2:
#dont care about single value
continue
if streakLength >= 4:
retDict[4] += 1
continue
if leftIndex == 0:
#obviously no left spots available
pass
elif leftIndex == 1:
if board[row][leftIndex-1] == " ":
leftSpotsFree += 1
if row == 5 or board[row+1][leftIndex-1] != ' ':
leftPlayableSpotsFree += 1
else:
#need at most 2 empty spots, since streaks < 2 haven't gotten this far
if board[row][leftIndex-2] == " " and board[row][leftIndex-1] == " ":
leftSpotsFree += 2
if row == 5 or (board[row+1][leftIndex-2] != ' ' and board[row+1][leftIndex-1] != ' '):
leftPlayableSpotsFree += 2
elif board[row+1][leftIndex-1] != ' ':
leftPlayableSpotsFree += 1
elif board[row][leftIndex-1] == " ":
leftSpotsFree += 1
if row == 5 or board[row+1][leftIndex-1] != ' ':
leftPlayableSpotsFree += 1
if rightIndex == 6:
pass
elif rightIndex == 5:
if board[row][rightIndex+1] == " ":
rightSpotsFree += 1
if row == 5 or board[row+1][rightIndex+1] != ' ':
rightPlayableSpotsFree += 1
else:
if board[row][rightIndex+2] == " " and board[row][rightIndex+1] == " ":
rightSpotsFree += 2
if row == 5 or (board[row+1][rightIndex+2] != ' ' and board[row+1][rightIndex+1] != ' '):
rightPlayableSpotsFree += 2
elif board[row+1][rightIndex+1] != ' ':
rightPlayableSpotsFree += 1
elif board[row][rightIndex+1] == " ":
rightSpotsFree += 1
if row == 5 or board[row+1][rightIndex+1] != ' ':
rightPlayableSpotsFree += 1
if (streakLength + leftSpotsFree + rightSpotsFree) >= 4:
#increment the number of streakLength streaks
if streakLength >= 4:
streakLength = 4
if leftPlayableSpotsFree != 0 or rightPlayableSpotsFree != 0:
retDict[streakLength] += 1
#check for gaps
for i in range(len(leftIndices)):
if i != (len(leftIndices) - 1):
#not the last element
leftIndex = leftIndices[i]
rightIndex = rightIndices[i]
nextLeftIndex = leftIndices[i+1]
nextRightIndex = rightIndices[i+1]
#if X at 2 and 4, gap at index 3 and gapLength == 1 (minus 1 for 0 indexing)
gapLength = nextLeftIndex - rightIndex - 1
if gapLength == 1:
#gap detected, need to check if it is empty
gapIndex = rightIndex + gapLength
if board[row][gapIndex] == " " and (row == 5 or board[row+1][gapIndex] != ' '):
#valid gap detected
leftStreak = rightIndex - leftIndex + 1
rightStreak = nextRightIndex - nextLeftIndex + 1
streakLength = leftStreak + rightStreak
if streakLength >= 4:
streakLength = 3
retDict[streakLength] += 1
return retDict
def checkUpperRightDiagonals(board, val):
checkBoard = [[False for x in range(7)] for y in range(6)]
retDict = {2:0, 3:0, 4:0}
leftColIndices = []
rightColIndices = []
topRowIndices = []
bottomRowIndices = []
#iterate through every element in the array. If desired value found, then look up and to the right,
#then down to the left to get the end indices of the diagonal streak
for row in range(6):
for col in range(7):
if board[row][col] == val:
#avoid checking diagonals that have already been checked
if not checkBoard[row][col]:
checkBoard[row][col] = True
tempRow = row
tempCol = col
counting = True
#check up and to the right
if row == 0 or col == 6:
rightColIndices.append(col)
topRowIndices.append(row)
else:
while(counting):
tempRow -= 1
tempCol += 1
if tempRow == 0 or tempCol == 6:
if board[tempRow][tempCol] == val:
#value found at edge of matrix
rightColIndices.append(tempCol)
topRowIndices.append(tempRow)
checkBoard[tempRow][tempCol] = True
else:
#previous value in diagonal was end of streak
rightColIndices.append(tempCol-1)
topRowIndices.append(tempRow+1)
#at edge of matrix, so stop counting
counting = False
else:
#not at upper right edge of matrix
if board[tempRow][tempCol] == val:
#just set this value to checked, and continue searching the diagonal
checkBoard[tempRow][tempCol] = True
else:
#previous value in diagonal was end of streak
counting = False
rightColIndices.append(tempCol-1)
topRowIndices.append(tempRow+1)
#check down and to the left
if row == 5 or col == 0:
leftColIndices.append(col)
bottomRowIndices.append(row)
else:
counting = True
tempRow = row
tempCol = col
while(counting):
tempRow += 1
tempCol -= 1
if tempRow == 5 or tempCol == 0:
#at edge of matrix
if board[tempRow][tempCol] == val:
#this is a desired value at the edge of the matrix
checkBoard[tempRow][tempCol] = True
leftColIndices.append(tempCol)
bottomRowIndices.append(tempRow)
else:
#edge of matrix, so previous value was desired
leftColIndices.append(tempCol+1)
bottomRowIndices.append(tempRow-1)
counting = False
else:
#not at edge of matrix
if board[tempRow][tempCol] == val:
#just set this value to checked, and continue searching the diagonal
checkBoard[tempRow][tempCol] = True
else:
#previous value in diagonal was end of streak
counting = False
leftColIndices.append(tempCol+1)
bottomRowIndices.append(tempRow-1)
#iterate through sets of diagonal streak end points
if len(leftColIndices) != len(rightColIndices) and len(leftColIndices) != len(topRowIndices) and len(leftColIndices) != len(bottomRowIndices):
print "ERROR: upper right diagonal index lists not same size"
else:
#will hold the length of the streak that the gap is between
gapCheck = [[0 for x in range(7)] for y in range(6)]
#check each set of indices, each set represents one diagonal streak
for i in range(len(leftColIndices)):
leftIndex = leftColIndices[i]
rightIndex = rightColIndices[i]
topIndex = topRowIndices[i]
bottomIndex = bottomRowIndices[i]
if leftIndex != rightIndex:
streakLength = rightIndex - leftIndex + 1
#check for free spots on either end of the streak
leftFreeSpots = 0
leftPlayableFreeSpots = 0
rightFreeSpots = 0
rightPlayableFreeSpots = 0
if streakLength >= 4:
retDict[4] += 1
continue
#check free spots down and to the left
if leftIndex == 0:
pass
elif leftIndex == 1:
if bottomIndex != 5:
if board[bottomIndex+1][leftIndex-1] in (' ', val):
leftFreeSpots += 1
try:
if board[bottomIndex+2][leftIndex-1] != ' ':
leftPlayableFreeSpots += 1
except:
pass
else:
#look 2 cells in this direction
if bottomIndex != 4 and bottomIndex != 5:
if board[bottomIndex+1][leftIndex-1] == ' ' and board[bottomIndex+2][leftIndex-2] == val:
#there is a gap, since at least 2 on one side, can assume it goes 2 + gap + 1, so 3 streak
try:
if board[bottomIndex+2][leftIndex-1] != ' ':
leftPlayableFreeSpots += 1
if gapCheck[bottomIndex+1][leftIndex-1] == 0:
retDict[3] += 1
gapCheck[bottomIndex+1][leftIndex-1] = 3
elif gapCheck[bottomIndex+1][leftIndex-1] < 3:
retDict[gapCheck[bottomIndex+1][leftIndex-1]] -= 1
retDict[3] += 1
gapCheck[bottomIndex+1][leftIndex-1] = 3
continue
except IndexError:
pass
#also consider val a free space when looking 2 cells away, to account for gaps
elif board[bottomIndex+1][leftIndex-1] == ' ' and board[bottomIndex+2][leftIndex-2] == ' ':
leftFreeSpots += 2
if board[bottomIndex+2][leftIndex-1] != ' ':
leftPlayableFreeSpots += 1
try:
if board[bottomIndex+3][leftIndex-2] != ' ':
leftPlayableFreeSpots += 1
except:
if bottomIndex+3 == 5:
leftPlayableFreeSpots += 1
elif board[bottomIndex+1][leftIndex-1] == ' ':
leftFreeSpots += 1
if board[bottomIndex+2][leftIndex-1] != ' ':
leftPlayableFreeSpots += 1
elif bottomIndex != 5:
#bottom index = 4
if board[bottomIndex+1][leftIndex-1] == ' ':
leftFreeSpots += 1
leftPlayableFreeSpots += 1
#check free spots up and to the right
if rightIndex == 6:
pass
elif rightIndex == 5:
if topIndex != 0:
if board[topIndex-1][rightIndex+1] == ' ':
rightFreeSpots += 1
try:
if board[topIndex][rightIndex+1] != ' ':
rightPlayableFreeSpots += 1
except:
pass
else:
if topIndex != 1 and topIndex != 0:
if board[topIndex-1][rightIndex+1] == ' ' and board[topIndex-2][rightIndex+2] == val:
try:
if board[topIndex][rightIndex+1] != ' ':
if gapCheck[topIndex-1][rightIndex+1] == 0:
retDict[3] += 1
gapCheck[topIndex-1][rightIndex+1] = 3
elif gapCheck[topIndex-1][rightIndex+1] < 3:
retDict[gapCheck[topIndex-1][rightIndex+1]] -= 1
retDict[3] += 1
gapCheck[topIndex-1][rightIndex+1] = 3
continue
except IndexError:
pass
elif board[topIndex-1][rightIndex+1] == ' ' and board[topIndex-2][rightIndex+2] == ' ':
rightFreeSpots += 2
if board[topIndex][rightIndex+1] != ' ':
rightPlayableFreeSpots += 1
if board[topIndex-1][rightIndex+2] != ' ':
rightPlayableFreeSpots += 1
elif board[topIndex-1][rightIndex+1] == ' ':
rightFreeSpots += 1
if board[topIndex][rightIndex+1] != ' ':
rightPlayableFreeSpots += 1
elif topIndex != 0:
#topIndex = 1
if board[topIndex-1][rightIndex+1] == ' ':
rightFreeSpots += 1
if board[topIndex][rightIndex+1] != ' ':
rightPlayableFreeSpots += 1
if (streakLength + leftFreeSpots + rightFreeSpots) >= 4:
#a possible 4 in a row could occur, so increment the number of valid streaks
if streakLength >= 4:
streakLength = 4
if leftPlayableFreeSpots != 0 or rightPlayableFreeSpots != 0:
retDict[streakLength] += 1
else:
#leftIndex == rightIndex:
if topIndex > 1 and rightIndex < 4:
if board[topIndex-1][rightIndex+1] == ' ' and board[topIndex-2][rightIndex+2] == val:
if board[topIndex][rightIndex+1] != ' ':
if gapCheck[topIndex-1][rightIndex+1] == 0:
gapCheck[topIndex-1][rightIndex+1] = 2
retDict[2] += 1
if bottomIndex < 4 and leftIndex > 1:
if board[bottomIndex+1][leftIndex-1] == ' ' and board[bottomIndex+2][leftIndex-2] == val:
#there is a gap, check if gap can be filled on next move
if board[bottomIndex+2][leftIndex-1] != ' ':
if gapCheck[bottomIndex+1][leftIndex-1] == 0:
#if no streak set at this gap, set to 2
retDict[2] += 1
gapCheck[bottomIndex+1][leftIndex-1] = 2
return retDict
def checkUpperLeftDiagonals(board, val):
#Basically same algorithm as previous function, just with opposite orientation
#look there for comments
checkBoard = [[0 for x in range(7)] for y in range(6)]
retDict = {2:0, 3:0, 4:0}
leftColIndices = []
rightColIndices = []
topRowIndices = []
bottomRowIndices = []
for row in range(6):
for col in range(7):
if board[row][col] == val:
if not checkBoard[row][col]:
checkBoard[row][col] = True
tempRow = row
tempCol = col
counting = True
leftSet = False
rightSet = False
#check up and to the left
if row == 0 or col == 0:
leftColIndices.append(col)
topRowIndices.append(row)
else:
while(counting):
tempRow -= 1
tempCol -= 1
if tempRow == 0 or tempCol == 0:
if board[tempRow][tempCol] == val:
leftColIndices.append(tempCol)
topRowIndices.append(tempRow)
checkBoard[tempRow][tempCol] = True
else:
leftColIndices.append(tempCol+1)
topRowIndices.append(tempRow+1)
counting = False
else:
if board[tempRow][tempCol] == val:
checkBoard[tempRow][tempCol] = True
else:
counting = False
leftColIndices.append(tempCol+1)
topRowIndices.append(tempRow+1)
#check down and to the right
if row == 5 or col == 6:
rightColIndices.append(col)
bottomRowIndices.append(row)
else:
counting = True
tempRow = row
tempCol = col
#check down and to the left
while(counting):
tempRow += 1
tempCol += 1
if tempRow == 5 or tempCol == 6:
if board[tempRow][tempCol] == val:
checkBoard[tempRow][tempCol] = True
rightColIndices.append(tempCol)
bottomRowIndices.append(tempRow)
else:
rightColIndices.append(tempCol-1)
bottomRowIndices.append(tempRow-1)
counting = False
else:
if board[tempRow][tempCol] == val:
checkBoard[tempRow][tempCol] = True
else:
counting = False
rightColIndices.append(tempCol-1)
bottomRowIndices.append(tempRow-1)
#iterate through sets of diagonal streak end points
if len(leftColIndices) != len(rightColIndices) and len(leftColIndices) != len(topRowIndices) and len(leftColIndices) != len(bottomRowIndices):
print "ERROR: upper right diagonal index lists not same size"
else:
gapCheck = [[False for x in range(7)] for y in range(6)]
for i in range(len(leftColIndices)):
leftIndex = leftColIndices[i]
rightIndex = rightColIndices[i]
topIndex = topRowIndices[i]
bottomIndex = bottomRowIndices[i]
if leftIndex != rightIndex:
streakLength = rightIndex - leftIndex + 1
leftFreeSpots = 0
leftPlayableFreeSpots = 0
rightFreeSpots = 0
rightPlayableFreeSpots = 0
if streakLength >= 4:
retDict[4] += 1
continue
#check free spots up and to the left
if leftIndex == 0:
pass
elif leftIndex == 1:
if topIndex != 0:
if board[topIndex-1][leftIndex-1] == ' ':
leftFreeSpots += 1
if board[topIndex][leftIndex-1] != ' ':
leftPlayableFreeSpots += 1
else:
if topIndex != 1 and topIndex != 0:
if board[topIndex-1][leftIndex-1] == ' ' and board[topIndex-2][leftIndex-2] == val:
try:
if board[topIndex][leftIndex-1] != ' ':
if gapCheck[topIndex-1][leftIndex-1] == 0:
retDict[3] += 1
gapCheck[topIndex-1][leftIndex-1] = 3
elif gapCheck[topIndex-1][leftIndex-1] < 3:
retDict[gapCheck[topIndex-1][leftIndex-1]] -= 1
retDict[3] += 1
gapCheck[topIndex-1][leftIndex-1] = 3
continue
except IndexError:
pass
elif board[topIndex-1][leftIndex-1] == ' ' and board[topIndex-2][leftIndex-2] == ' ':
leftFreeSpots += 2
if board[topIndex][leftIndex-1] != ' ':
leftPlayableFreeSpots += 1
if board[topIndex-1][leftIndex-2] != ' ':
leftPlayableFreeSpots += 1
elif board[topIndex-1][leftIndex-1] == ' ':
leftFreeSpots += 1
if board[topIndex][leftIndex-1] != ' ':
leftPlayableFreeSpots += 1
elif bottomIndex != 0:
#bottom index = 4
if board[topIndex-1][leftIndex-1] == ' ':
leftFreeSpots += 1
if board[topIndex][leftIndex-1] != ' ':
leftPlayableFreeSpots += 1
#check free spots down and to the right
if rightIndex == 6:
pass
elif rightIndex == 5:
if bottomIndex != 5:
if board[bottomIndex+1][rightIndex+1] == ' ':
rightFreeSpots += 1
try:
if board[bottomIndex+2][rightIndex+1] != ' ':
rightPlayableFreeSpots += 1
except:
pass
else:
if bottomIndex != 4 and bottomIndex != 5:
if board[bottomIndex+1][rightIndex+1] == ' ' and board[bottomIndex+2][rightIndex+2] == val:
try:
if board[bottomIndex+2][rightIndex+1] != ' ':
if gapCheck[bottomIndex+1][rightIndex+1] == 0:
retDict[3] += 1
gapCheck[bottomIndex+1][rightIndex+1] = 3
elif gapCheck[bottomIndex+1][rightIndex+1] < 3:
retDict[gapCheck[bottomIndex+1][rightIndex+1]] -= 1
retDict[3] += 1
gapCheck[bottomIndex+1][rightIndex+1] = 3
continue
except IndexError:
pass
elif board[bottomIndex+1][rightIndex+1] == ' ' and board[bottomIndex+2][rightIndex+2] == ' ':
rightFreeSpots += 2
if board[bottomIndex+2][rightIndex+1] != ' ':
rightPlayableFreeSpots += 1
try:
if board[bottomIndex+3][rightIndex+2] != ' ':
rightPlayableFreeSpots += 1
except:
if bottomIndex+3 == 5:
rightPlayableFreeSpots += 1
elif board[bottomIndex+1][rightIndex+1] == ' ':
rightFreeSpots += 1
if board[bottomIndex+2][rightIndex+1] != ' ':
rightPlayableFreeSpots += 1
elif bottomIndex != 5:
#bottom index = 4
if board[topIndex+1][rightIndex+1] == ' ':
rightFreeSpots += 1
rightPlayableFreeSpots += 1
if (streakLength + leftFreeSpots + rightFreeSpots) >= 4:
if streakLength >= 4:
streakLength = 4
if leftPlayableFreeSpots != 0 or rightPlayableFreeSpots != 0:
retDict[streakLength] += 1
else:
if topIndex > 1 and leftIndex > 1:
if board[topIndex-1][leftIndex-1] == ' ' and board[topIndex-2][leftIndex-2] == val:
if board[topIndex][leftIndex-1] != ' ':
if gapCheck[topIndex-1][leftIndex-1] == 0:
retDict[2] += 1
gapCheck[topIndex-1][leftIndex-1] = 2
if bottomIndex < 4 and rightIndex < 4:
if board[bottomIndex+1][rightIndex+1] == ' ' and board[bottomIndex+2][rightIndex+2] == val:
#there is a gap, check if gap can be filled on next move
if board[bottomIndex+2][rightIndex+1] != ' ':
if gapCheck[bottomIndex+1][rightIndex+1] == 0:
retDict[2] += 1
gapCheck[bottomIndex+1][rightIndex+1] = 2
return retDict
def checkVerticalMatches(board, valToCheck, colScore):
for col in range(7):
count = checkColumn(board, valToCheck, col) #returns [0-4]
if count > 1:
colScore[count] += 1
def checkHorizontalMatches(board, valToCheck, rowScore):
for row in range(6):
streakCountDict = checkRow(board, valToCheck, row)
rowScore[2] += streakCountDict[2]
rowScore[3] += streakCountDict[3]
rowScore[4] += streakCountDict[4]
def checkUpperRightDiagMatches(board, valToCheck, upRightDiagScore):
streakCountDict = checkUpperRightDiagonals(board, valToCheck)
upRightDiagScore[2] += streakCountDict[2]
upRightDiagScore[3] += streakCountDict[3]
upRightDiagScore[4] += streakCountDict[4]
def checkUpperLeftDiagMatches(board, valToCheck, upLeftDiagScore):
streakCountDict = checkUpperLeftDiagonals(board, valToCheck)
upLeftDiagScore[2] += streakCountDict[2]
upLeftDiagScore[3] += streakCountDict[3]
upLeftDiagScore[4] += streakCountDict[4]