-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check_For_Win.py
174 lines (161 loc) · 7.68 KB
/
Check_For_Win.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
#Winning Condition
from Game_Mode import*
#Class that analyzes the state of the board
class CheckForWin(object):
#Slightly adapted from: https://www.cs.cmu.edu/~112/notes/2d-list-case-studies.html
@staticmethod
def wordSearch(board, word, piecesInARow, countTarget):
(rows, cols) = (len(board), len(board[0]))
for row in range(rows):
for col in range(cols):
result = CheckForWin.wordSearchFromCell(board, word, row, col,piecesInARow, countTarget)
if (result != None):
return result
return None
#Slightly adapted from: https://www.cs.cmu.edu/~112/notes/2d-list-case-studies.html
@staticmethod
def wordSearchFromCell(board, word, startRow, startCol, piecesInARow, countTarget):
count = 0
possibleDirections = 8 # 3^2 - 1
for dir in range(possibleDirections):
result = CheckForWin.wordSearchFromCellInDirection(board, word,
startRow, startCol, dir, piecesInARow)
if (result != None):
count += 1
if (count == countTarget):
return result
return None
#Slightly adapted from: https://www.cs.cmu.edu/~112/notes/2d-list-case-studies.html
@staticmethod
def wordSearchFromCellInDirection(board, word, startRow, startCol, dir, piecesInARow):
(rows, cols) = (len(board), len(board[0]))
dirs = [ (-1, -1), (-1, 0), (-1, +1),
( 0, -1), ( 0, +1),
(+1, -1), (+1, 0), (+1, +1) ]
(drow,dcol) = dirs[dir]
winStreak = piecesInARow
for i in range(winStreak):
row = startRow + i*drow
col = startCol + i*dcol
if ((row < 0) or (row >= rows) or
(col < 0) or (col >= cols) or
(board[row][col] != word)):
return None
return word
#Inspired from: https://www.cs.cmu.edu/~112/notes/2d-list-case-studies.html
#function that takes returns 1) the longest run of two players, with the abilty
#of the current cell being sandwiched, and 2) the number connecting
#pieces with respect to that cell
#Ex: oo_oo is counted as 5 in a row
@staticmethod
def countPiecesFromCellInTwoDir(board, player1, player2, startRow, startCol):
#player1
countP1 = 0
longestRunP1 = 0
#player2
countP2 = 0
longestRunP2 = 0
possibleOrientations = 4
for dirOr in range(possibleOrientations):
longestRunP1, countP1 = CheckForWin.countPiecesFromCellInTwoDirHelper(board,
player1, longestRunP1,countP1, startRow, startCol, dirOr)
longestRunP2, countP2 = CheckForWin.countPiecesFromCellInTwoDirHelper(board,
player2, longestRunP2,countP2, startRow, startCol, dirOr)
return longestRunP1, countP1, longestRunP2, countP2
#Helper function that combines the possible pieces in a row of any directional
#orientation
@staticmethod
def countPiecesFromCellInTwoDirHelper(board, player, playerRun, playerCount,
startRow, startCol, dirOr):
run1, run2 = CheckForWin.countPiecesInTwoDirOr(board, player,
startRow, startCol, dirOr)
playerRun = max(playerRun, (run1 + run2)-1)
playerCount += playerRun//2
return playerRun, playerCount
#Inspired from: https://www.cs.cmu.edu/~112/notes/2d-list-case-studies.html
@staticmethod
def countPiecesInTwoDirOr(board, player, startRow, startCol, dirOr):
"""
(-1, -1), (-1, 0), (-1, +1),
( 0, -1), Piece, ( 0, +1),
(+1, -1), (+1, 0), (+1, +1) ]"""
#Based on above directions,check in respective directinal oreinetation:
# decreasing diagonals increasing horizontal
dirs = [[(-1,-1),(1,1)], [(1,-1),(-1,1)] ,
# vertical horizontal
[(-1,0),(1,0)], [(0,-1),(0,1)]]
orientation = dirs[dirOr]
(drow1,dcol1) = dirs[dirOr][0]
run1 = CheckForWin.countPiecesInTwoDir(board, player,
startRow, startCol, drow1,dcol1)
(drow2,dcol2) = dirs[dirOr][1]
run2 = CheckForWin.countPiecesInTwoDir(board, player,
startRow, startCol, drow2,dcol2)
return run1, run2
#Inspired from: https://www.cs.cmu.edu/~112/notes/2d-list-case-studies.html
@staticmethod
def countPiecesInTwoDir(board, player, startRow, startCol, drow, dcol):
(rows, cols) = (len(board), len(board[0]))
winningRun = 6
if player == "black":
accountForMissingAIPiece = 0
else:
accountForMissingAIPiece = 1
for i in range(winningRun):
row = startRow + i*drow + drow*accountForMissingAIPiece
col = startCol + i*dcol + dcol*accountForMissingAIPiece
if ((row < 0) or (row >= rows) or
(col < 0) or (col >= cols) or
(board[row][col] != player)):
return i + accountForMissingAIPiece
return winningRun
#Modified from: https://www.cs.cmu.edu/~112/notes/2d-list-case-studies.html
##Does not account for oo_oo rows -> will report as 2
@staticmethod
def countPiecesFromCellInOneDir(board, player1, player2, startRow, startCol):
#player1
countP1 = 0
longestRunP1 = 0
#player2
countP2 = 0
longestRunP2 = 0
possibleDirections = 8 # 3^2 - 1
for dir in range(possibleDirections):
longestRunP1, countP1 = CheckForWin.countPiecesFromCellHelper(board,
player1, longestRunP1,countP1, startRow, startCol, dir)
longestRunP2, countP2 = CheckForWin.countPiecesFromCellHelper(board,
player2, longestRunP2,countP2, startRow, startCol, dir)
return longestRunP1, countP1, longestRunP2, countP2
#Helper function that takes calculates the longest run, with the abilty
#of the current cell being sandwiched, and the connecting pieces of that cell
#Ex: oo_oo is counted as 5 in a row
@staticmethod
def countPiecesFromCellHelper(board, player, playerRun, playerCount,
startRow, startCol, dir):
result = CheckForWin.countPiecesInDir(board, player,
startRow, startCol, dir)
playerRun = max(playerRun, result)
playerCount += result
return playerRun, playerCount
#Modified from: https://www.cs.cmu.edu/~112/notes/2d-list-case-studies.html
@staticmethod
def countPiecesInDir(board, player, startRow, startCol, dir):
(rows, cols) = (len(board), len(board[0]))
dirs = [ (-1, -1), (-1, 0), (-1, +1),
( 0, -1), ( 0, +1),
(+1, -1), (+1, 0), (+1, +1) ]
(drow,dcol) = dirs[dir]
winStreak = 6
if player == "black":
accountForMissingAIPiece = 0
else:
accountForMissingAIPiece = 1
for i in range(winStreak):
row = startRow + i*drow + drow*accountForMissingAIPiece
col = startCol + i*dcol + dcol*accountForMissingAIPiece
if ((row < 0) or (row >= rows) or
(col < 0) or (col >= cols) or
(board[row][col] != player)):
return i + accountForMissingAIPiece
winStreak += 1
return winStreak