Created
March 1, 2017 20:44
-
-
Save guilhermemauro/c013fad888483ba6f1acad617ae23ef8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
write_comp = False | |
cap = cv2.VideoCapture(1) | |
while(game): | |
fields = [] | |
ret, frame = cap.read() | |
#mirros the image to stay in the same position of screen computer | |
#maybe necessary to change | |
frame = cv2.flip(frame,1) | |
frame = cv2.flip(frame,0) | |
hW,wW = frame.shape[0:2] | |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | |
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
cnt = findBiggestContour(cv2.Canny(frame_gray, 100,200)) | |
x,y,h,w = cv2.boundingRect(cnt) | |
#cv2.rectangle(frame,(x,y),(x+h,y+w),(0,255,0),2) | |
column = w/3 | |
line = h/3 | |
points = [] | |
key = 0 | |
for num_column in range(0,4): | |
for num_line in range(0,4): | |
points.append(((num_line*line)+x,(num_column*column)+y)) | |
for number in range(0, 11): | |
if number != 3 and number != 7: | |
field = [points[number][1], points[number+5][1], points[number][0],points[number+5][0]] | |
fields.append(field) | |
for field in fields: | |
cv2.circle(frame, (field[2], field[0]), 3, (0,0,255), -1) | |
if choose_user == True: | |
result = getPlayer(fields, frame) | |
if result == True: | |
print "O jogador venceu!" | |
cv2.putText(frame, "JOGADOR VENCEU!",(wW/3,hW/2), FONTE, 1,(0,255,0),2,cv2.LINE_AA) | |
cv2.imshow('Tic Tac Toe',frame) | |
game = False | |
elif result == "EMPATE": | |
cv2.putText(frame, "HOUVE EMPATE!",(wW/3,hW/2), FONTE, 1,(0,255,0),2,cv2.LINE_AA) | |
cv2.imshow('Tic Tac Toe',frame) | |
print "Houve empate!" | |
game = False | |
elif choose_user == False: | |
tic.inteligence(computer) | |
result = tic.verifyWinner(computer) | |
print tic.game[0] | |
print tic.game[1] | |
print tic.game[2] | |
if result == True: | |
print "O computador venceu!" | |
cv2.putText(frame, "COMPUTADOR VENCEU!",(wW/3,hW/2), FONTE, 1,(0,255,0),2,cv2.LINE_AA) | |
cv2.imshow('Tic Tac Toe',frame) | |
game = False | |
elif result == "EMPATE": | |
print "Houve empate!" | |
cv2.putText(frame, "HOUVE EMPATE!",(wW/3,hW/2), FONTE, 1,(0,255,0),2,cv2.LINE_AA) | |
cv2.imshow('Tic Tac Toe',frame) | |
game = False | |
write_comp = True | |
choose_user = True | |
if write_comp == True: | |
if computer == 1: | |
text = "X" | |
else: | |
text = "O" | |
gameboard = np.array(tic.game[0]+tic.game[1]+tic.game[2]) | |
indexs = np.where(gameboard==computer)[0] | |
for index in indexs: | |
coord = (fields[index][2]+50, fields[index][0]+100) | |
cv2.putText(frame, text,coord, FONTE, 3,(50,0,255),2,cv2.LINE_AA) | |
cv2.imshow('Tic Tac Toe',frame) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
if game == False: | |
time.sleep(3) | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment