-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcell_checker.py
233 lines (212 loc) · 8.41 KB
/
cell_checker.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
import numpy as np
import cv2
import random
def isSpaceFree(board, position):
# Return true if the passed move is free on the passed board.
return board[position] == '-'
def makeMove(board, letter, position):
board[position] = letter
def isWinner(bo, le):
# Given a board and a player's letter, this function returns True if that player has won.
# We use bo instead of board and le instead of letter so we don't have to type as much.
return ((bo[6] == le and bo[7] == le and bo[8] == le) or # across the top
(bo[3] == le and bo[4] == le and bo[5] == le) or # across the middle
(bo[0] == le and bo[1] == le and bo[2] == le) or # across the bottom
(bo[6] == le and bo[3] == le and bo[0] == le) or # down the left side
(bo[7] == le and bo[4] == le and bo[1] == le) or # down the middle
(bo[8] == le and bo[5] == le and bo[2] == le) or # down the right side
(bo[6] == le and bo[4] == le and bo[2] == le) or # diagonal
(bo[8] == le and bo[4] == le and bo[0] == le)) # diagonal
def chooseRandomMoveFromList(board, positionList):
# Returns a valid move from the passed list on the passed board.
# Returns None if there is no valid move.
possiblePositions = []
for i in positionList:
if isSpaceFree(board, i):
possiblePositions.append(i)
if len(possiblePositions) != 0:
return random.choice(possiblePositions)
else:
return None
def getNextMove(board):
# First, check if we can win in the next move
for i in range(9):
copy = board[:]
if isSpaceFree(copy, i):
makeMove(copy, "X", i)
if isWinner(copy, "X"):
return i
# Check if the player could win on his next move, and block them.
for i in range(9):
copy = board[:]
if isSpaceFree(copy, i):
makeMove(copy, "O", i)
if isWinner(copy, "O"):
return i
# Try to take one of the corners, if they are free.
move = chooseRandomMoveFromList(board, [0, 2, 6, 8])
if move != None:
return move
# Try to take the center, if it is free.
if isSpaceFree(board, 4):
return 5
# Move on one of the sides.
return chooseRandomMoveFromList(board, [1, 3, 5, 7])
def isBoardNotFull(board):
return "-" in board
cap = cv2.VideoCapture(0)
OFFSET = 25
TicTacToe_Board_Val=[255, 255, 255, 255, 255, 255, 255, 255, 255]
TicTacToe_Board_Bin=["-", "-", "-", "-", "-", "-", "-", "-", "-"]
RECT_LG = {
"x1":100,
"y1":40,
"x2":540,
"y2":440
}
RECT_1 = {
"x1":100+OFFSET,
"y1":40+OFFSET,
"x2":247-OFFSET,
"y2":173-OFFSET
}
RECT_2 = {
"x1":247+OFFSET,
"y1":40+OFFSET,
"x2":393-OFFSET,
"y2":173-OFFSET
}
RECT_3 = {
"x1":393+OFFSET,
"y1":40+OFFSET,
"x2":540-OFFSET,
"y2":173-OFFSET
}
RECT_4 = {
"x1":100+OFFSET,
"y1":173+OFFSET,
"x2":247-OFFSET,
"y2":307-OFFSET
}
RECT_5 = {
"x1":247+OFFSET,
"y1":173+OFFSET,
"x2":393-OFFSET,
"y2":307-OFFSET
}
RECT_6 = {
"x1":393+OFFSET,
"y1":173+OFFSET,
"x2":540-OFFSET,
"y2":307-OFFSET
}
RECT_7 = {
"x1":100+OFFSET,
"y1":307+OFFSET,
"x2":247-OFFSET,
"y2":440-OFFSET
}
RECT_8 = {
"x1":247+OFFSET,
"y1":307+OFFSET,
"x2":393-OFFSET,
"y2":440-OFFSET
}
RECT_9 = {
"x1":393+OFFSET,
"y1":307+OFFSET,
"x2":540-OFFSET,
"y2":440-OFFSET
}
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Draw rectangle grid
cv2.rectangle(gray, (RECT_LG["x1"], RECT_LG["y1"]), (RECT_LG["x2"], RECT_LG["y2"]), (255,255,0), 2)
cv2.rectangle(gray, (RECT_1["x1"], RECT_1["y1"]), (RECT_1["x2"], RECT_1["y2"]), (0,255,0), 2)
cv2.rectangle(gray, (RECT_2["x1"], RECT_2["y1"]), (RECT_2["x2"], RECT_2["y2"]), (0,255,0), 2)
cv2.rectangle(gray, (RECT_3["x1"], RECT_3["y1"]), (RECT_3["x2"], RECT_3["y2"]), (0,255,0), 2)
cv2.rectangle(gray, (RECT_4["x1"], RECT_4["y1"]), (RECT_4["x2"], RECT_4["y2"]), (0,255,0), 2)
cv2.rectangle(gray, (RECT_5["x1"], RECT_5["y1"]), (RECT_5["x2"], RECT_5["y2"]), (0,255,0), 2)
cv2.rectangle(gray, (RECT_6["x1"], RECT_6["y1"]), (RECT_6["x2"], RECT_6["y2"]), (0,255,0), 2)
cv2.rectangle(gray, (RECT_7["x1"], RECT_7["y1"]), (RECT_7["x2"], RECT_7["y2"]), (0,255,0), 2)
cv2.rectangle(gray, (RECT_8["x1"], RECT_8["y1"]), (RECT_8["x2"], RECT_8["y2"]), (0,255,0), 2)
cv2.rectangle(gray, (RECT_9["x1"], RECT_9["y1"]), (RECT_9["x2"], RECT_9["y2"]), (0,255,0), 2)
# Display the result+++++++++++++++++++++++++++++++++++ing frame
cv2.imshow('Camera Feed',gray)
k = cv2.waitKey(1)
if (k%256 == 27):
# ESC pressed
print("Escape hit, closing...")
break
elif k%256 == 32:
# SPACE pressed
print("Images taken.")
#Save images
RECT_1_img = "img/RECT_1.png"
cv2.imwrite(RECT_1_img, gray[RECT_1["y1"]:RECT_1["y2"], RECT_1["x1"]:RECT_1["x2"]])
RECT_2_img = "img/RECT_2.png"
cv2.imwrite(RECT_2_img, gray[RECT_2["y1"]:RECT_2["y2"], RECT_2["x1"]:RECT_2["x2"]])
RECT_3_img = "img/RECT_3.png"
cv2.imwrite(RECT_3_img, gray[RECT_3["y1"]:RECT_3["y2"], RECT_3["x1"]:RECT_3["x2"]])
RECT_4_img = "img/RECT_4.png"
cv2.imwrite(RECT_4_img, gray[RECT_4["y1"]:RECT_4["y2"], RECT_4["x1"]:RECT_4["x2"]])
RECT_5_img = "img/RECT_5.png"
cv2.imwrite(RECT_5_img, gray[RECT_5["y1"]:RECT_5["y2"], RECT_5["x1"]:RECT_5["x2"]])
RECT_6_img = "img/RECT_6.png"
cv2.imwrite(RECT_6_img, gray[RECT_6["y1"]:RECT_6["y2"], RECT_6["x1"]:RECT_6["x2"]])
RECT_7_img = "img/RECT_7.png"
cv2.imwrite(RECT_7_img, gray[RECT_7["y1"]:RECT_7["y2"], RECT_7["x1"]:RECT_7["x2"]])
RECT_8_img = "img/RECT_8.png"
cv2.imwrite(RECT_8_img, gray[RECT_8["y1"]:RECT_8["y2"], RECT_8["x1"]:RECT_8["x2"]])
RECT_9_img = "img/RECT_9.png"
cv2.imwrite(RECT_9_img, gray[RECT_9["y1"]:RECT_9["y2"], RECT_9["x1"]:RECT_9["x2"]])
#Get value of each image
TicTacToe_Board_Val[0] = np.average(gray[RECT_1["y1"]:RECT_1["y2"], RECT_1["x1"]:RECT_1["x2"]])
TicTacToe_Board_Val[1] = np.average(gray[RECT_2["y1"]:RECT_2["y2"], RECT_2["x1"]:RECT_2["x2"]])
TicTacToe_Board_Val[2] = np.average(gray[RECT_3["y1"]:RECT_3["y2"], RECT_3["x1"]:RECT_3["x2"]])
TicTacToe_Board_Val[3] = np.average(gray[RECT_4["y1"]:RECT_4["y2"], RECT_4["x1"]:RECT_4["x2"]])
TicTacToe_Board_Val[4] = np.average(gray[RECT_5["y1"]:RECT_5["y2"], RECT_5["x1"]:RECT_5["x2"]])
TicTacToe_Board_Val[5] = np.average(gray[RECT_6["y1"]:RECT_6["y2"], RECT_6["x1"]:RECT_6["x2"]])
TicTacToe_Board_Val[6] = np.average(gray[RECT_7["y1"]:RECT_7["y2"], RECT_7["x1"]:RECT_7["x2"]])
TicTacToe_Board_Val[7] = np.average(gray[RECT_8["y1"]:RECT_8["y2"], RECT_8["x1"]:RECT_8["x2"]])
TicTacToe_Board_Val[8] = np.average(gray[RECT_9["y1"]:RECT_9["y2"], RECT_9["x1"]:RECT_9["x2"]])
for i in range(len(TicTacToe_Board_Val)):
if TicTacToe_Board_Bin[i]!="X":
if TicTacToe_Board_Val[i] < 120:
TicTacToe_Board_Bin[i]="O"
elif TicTacToe_Board_Val[i] > 120:
TicTacToe_Board_Bin[i]="-"
print(TicTacToe_Board_Val[0:3])
print(TicTacToe_Board_Val[3:6])
print(TicTacToe_Board_Val[6:9])
print("Detected 'O' move. (User)")
print(TicTacToe_Board_Bin[0:3])
print(TicTacToe_Board_Bin[3:6])
print(TicTacToe_Board_Bin[6:9])
print('')
pos=getNextMove(TicTacToe_Board_Bin)
if isWinner(TicTacToe_Board_Bin, "O"):
print("'O' has won the game.")
break
elif not(isBoardNotFull(TicTacToe_Board_Bin)):
print("Board is full. Game is tied.")
break
else:
makeMove(TicTacToe_Board_Bin, "X", pos)
print("Response 'X' move. (Computer)")
print(TicTacToe_Board_Bin[0:3])
print(TicTacToe_Board_Bin[3:6])
print(TicTacToe_Board_Bin[6:9])
print('')
if isWinner(TicTacToe_Board_Bin, "X"):
print("'X' has won the game.")
break
elif not(isBoardNotFull(TicTacToe_Board_Bin)):
print("Board is full. Game is tied.")
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()