def testCurrentPlayerIsO(self): game = TicTacToe() game.changePlayer() if game.currentPlayer() == 'O': result = True else: result = False self.assertEqual(result, True)
def mainLoop(camera): start_time = time.time() timer_active = False pressedBlockIndex = -1 pressedBlockNonBlackPixes = 0 game = TicTacToe() winner_pos = None isDraw = False while True: lower, upper = getTrackBarsValues() lower_bound = np.array(lower) upper_bound = np.array(upper) (_, image) = camera.read() image = cv2.flip(image, 1) hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) skinImage = extractSkinFromImage(hsv, lower_bound, upper_bound) blocks = divideImageInBlocks(skinImage, gridRows, gridCols, True) # Checking if a hand is present in one of the blocks, # If yes, then start the timer for activation blockW = blocks[0].shape[0] blockH = blocks[0].shape[1] pressedBlock = False for i in range(len(blocks)): if game.board[i] is not None: matrixPos = (i // gridRows, i % gridRows) if winner_pos is not None and i in winner_pos: drawCharacters(skinImage, game.getCharAtBoardPos(i), matrixPos, blockH, blockW, GREEN) else: drawCharacters(skinImage, game.getCharAtBoardPos(i), matrixPos, blockH, blockW) numSkinPixels = isBlockPressed(blocks[i], 0.4) if numSkinPixels > 0: if i != pressedBlockIndex: if numSkinPixels > pressedBlockNonBlackPixes: start_time = time.time() timer_active = True pressedBlockIndex = i pressedBlockNonBlackPixes = numSkinPixels pressedBlock = True if not pressedBlock: timer_active = False pressedBlockNonBlackPixes = 0 pressedBlockIndex = -1 elif pressedBlockIndex >= 0: matrixPos = (pressedBlockIndex // gridRows, pressedBlockIndex % gridRows) drawBlockRectengle(skinImage, matrixPos, blockH, blockW) if winner_pos is not None: matrixPosStart = (winner_pos[0] // gridRows, winner_pos[0] % gridRows) matrixPosEnd = (winner_pos[2] // gridRows, winner_pos[2] % gridRows) drawWinningLine(skinImage, matrixPosStart, matrixPosEnd, blockH, blockW) cv2.putText(skinImage, "Winner is " + game.getCharAtBoardPos(winner_pos[0]), (5, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, MAGENTA, 2) elif isDraw: cv2.putText(skinImage, "DRAW / TIE", (5, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, MAGENTA, 2) cv2.imshow("Final Result", skinImage) #cv2.imshow("Block 1", blocks[0]) if isDraw or winner_pos is not None: # Restart the game if hasTimerPassed(start_time, END_SCREEN_S): game.newBoard() timer_active = False pressedBlockNonBlackPixes = 0 pressedBlockIndex = -1 isDraw = False winner_pos = None else: if timer_active and hasTimerPassed(start_time, TIMER_S): print(str(TIMER_S) + " Seconds have passed!") print("Block " + str(pressedBlockIndex) + " is pressed!") if game.makeMove(game.playersTurn, pressedBlockIndex): game.printBoard() winner_pos = game.checkWinner(game.playersTurn) if winner_pos is not None: print("Winner is " + game.getPlayerChar()) elif game.checkDraw(): isDraw = True print("DRAW") game.changePlayer() else: print("INVALID MOVE") timer_active = False pressedBlockNonBlackPixes = 0 pressedBlockIndex = -1 if cv2.waitKey(1) != -1: break