예제 #1
0
def main():
    run = True
    clock = pygame.time.Clock()
    board = Board()
    turn = 1
    winner = None
    while run:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if (turn == -1):
                choice = random.choice(board.get_legal_moves())
                board.move(choice[0], choice[1], -1)
                turn = 1
                winner = board.check_winner(WIN)

            if event.type == pygame.MOUSEBUTTONDOWN and winner == None:
                pos = pygame.mouse.get_pos()
                row, col = get_row_col_from_mouse(pos)
                if turn == 1 and board.move(row, col, 1):
                    turn = -1
                    winner = board.check_winner(WIN)
                if winner == "Draw":
                    print("It is a Draw!")
                elif winner != None:
                    print(str(winner) + " wins!")
        board.draw(WIN)
        pygame.display.update()

    pygame.quit()
예제 #2
0
    def test_diagonal_winner(self):
        board = Board()

        # Main diagonal
        for x in range(board._rows):
            board.accept_move(x, x, board.player_o)

        self.assertEqual(board.check_winner(), board.player_o)

        board.reset()

        # Alternate diagonal
        for x in range(board._rows):
            board.accept_move(board._cols - 1 - x, x, board.player_o)

        self.assertEqual(board.check_winner(), board.player_o)
예제 #3
0
    def test_row_winner(self):
        board = Board()

        for x in range(board._cols):
            board.accept_move(x, 0, board.player_o)
            board.accept_move(x, 1, board.player_o)
            board.accept_move(x, 2, board.player_o)
            self.assertEqual(board.check_winner(), board.player_o,
                             f"Error: player O should have won.")
            board.reset()
예제 #4
0
    def test_col_winner(self):
        board = Board()

        for y in range(board._rows):
            board.accept_move(0, y, board.player_x)
            board.accept_move(1, y, board.player_x)
            board.accept_move(2, y, board.player_x)
            self.assertEqual(board.check_winner(), board.player_x,
                             f"Error: player X should have won.")
            board.reset()