Exemplo n.º 1
0
def test_next_move():
    cur_ai = AI()
    current_board = BoardState.initial_state()
    answer1_board = BoardState.initial_state()
    answer2_board = BoardState.initial_state()
    current_board.load('board_3_in_a_row.pickle')
    answer1_board.load('board_3_in_a_row_ans1.pickle')
    answer2_board.load('board_3_in_a_row_ans2.pickle')
    assert (cur_ai.next_move(current_board, None)[0].board.all()
            == answer1_board.board.all() or cur_ai.next_move(
                current_board, None)[0].board == answer2_board.board.all())
Exemplo n.º 2
0
 def test_ai_vs_ai(self):
     board = BoardState.initial_state()
     while not board.ended():
         start_time = time()
         board = self.ai.next_move(board)
         finish_time = time()
         self.assertIsNotNone(board)
         elp_time = finish_time - start_time
         self.assertLess(elp_time, DraughtsTest.MAX_MOVE_TIME)
Exemplo n.º 3
0
def game_loop(screen: Surface, board: BoardState, ai: AI):
    grid_size = screen.get_size()[0] // game_set.board_size

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

            if event.type == pygame.MOUSEBUTTONUP:
                x, y = [p // grid_size for p in event.pos]
                if event.button == 1:  # do move
                    board.notification = None
                    board = board.do_move(y, x)
                elif event.button == 3:  # change figure
                    board.board[y, x] = ((board.board[y, x] + 2) % 3) - 1

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    board = BoardState.initial_state()

                if event.key == pygame.K_s:
                    board.save()

                if event.key == pygame.K_l:
                    board = BoardState.load()

                if event.key == pygame.K_z:
                    pass

                if (game_set.is_ai_enabled and event.key == pygame.K_SPACE and
                        game_set.first_player_is_ai ==
                        board.is_first_player_turn):
                    new_board = ai.next_move(board, None)[0]
                    if new_board is not None:
                        board = new_board

        if board.is_game_finished:
            board.notification = "Second" if board.is_first_player_turn \
                else "First"
            board.notification += " player won! Press R to restart"

        draw_board(screen, 0, 0, grid_size, board)
        pygame.display.flip()
Exemplo n.º 4
0
                if event.key == pygame.K_s:
                    board.save_board("checkers.txt")
                  
                if event.key == pygame.K_l:
                    board.open_saved_board("checkers.txt")

                if event.key == pygame.K_SPACE:
                    new_board = board.inverted()
                    while True:
                        new_board = ai.next_move(new_board)
                        if new_board is None or new_board.in_the_process_of_taking == False:
                            break
                    if new_board is not None:
                        board = new_board.inverted()
        draw_board(screen, 0, 0, grid_size, board)
        pygame.display.flip()

pygame.init()

screen: Surface = pygame.display.set_mode([512, 512])
ai = AI(PositionEvaluation(), search_depth=4)

game_loop(screen, BoardState.initial_state(), ai)

pygame.quit()    



  
Exemplo n.º 5
0
def game_loop(screen: Surface, board: BoardState, ai: AI):
    grid_size = screen.get_size()[0] // 8
    previous_board = board
    visible_board = board
    step = 0
    flag = True
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                mouse_click_position = event.pos

            if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
                new_x, new_y = [p // grid_size for p in event.pos]
                old_x, old_y = [p // grid_size for p in mouse_click_position]

                new_board = board.do_move(old_x, old_y, new_x, new_y)
                if new_board is not None:
                    previous_board = board
                    board = new_board.inverted()
                    if step % 2 == 0:
                        visible_board = new_board
                    else:
                        visible_board = board
                    step += 1
                if board.get_winner != 0:
                    flag = False

            #if event.type == pygame.MOUSEBUTTONUP and event.button == 3:
            #x, y = [p // grid_size for p in event.pos]
            #board.board[y, x] = (board.board[y, x] + 1 + 2) % 5 - 2  # change figure

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    visible_board = visible_board.inverted()
                elif event.key == pygame.K_n:
                    board = board.initial_state()
                    visible_board = board
                    step = 0
                elif event.key == pygame.K_s:
                    save_board(board, './src/save_board.bin')
                    save_board(visible_board, './src/save_visible.bin')
                elif event.key == pygame.K_l:
                    board = load_board('./src/save_board.bin')
                    visible_board = load_board('./src/save_visible.bin')
                    flag = True
                    if board.current_player == visible_board.current_player:
                        step = 0
                    else:
                        step = 1
                elif event.key == pygame.K_1:
                    board = load_board(
                        './src/test_boards/test_board_1_visible')
                    visible_board = load_board(
                        './src/test_boards/test_board_1_board')
                    flag = True
                    if board.current_player == visible_board.current_player:
                        step = 0
                    else:
                        step = 1
                elif event.key == pygame.K_2:
                    board = load_board(
                        './src/test_boards/test_board_2_visible')
                    visible_board = load_board(
                        './src/test_boards/test_board_2_board')
                    flag = True
                    if board.current_player == visible_board.current_player:
                        step = 0
                    else:
                        step = 1
                elif event.key == pygame.K_z:
                    board = previous_board
                elif event.key == pygame.K_SPACE:
                    new_board = ai.next_move(board)
                    if new_board is not None:
                        previous_board = board
                        board = new_board.inverted()
                        if step % 2 == 0:
                            visible_board = new_board
                        else:
                            visible_board = board
                        step += 1
                    if board.get_winner != 0:
                        flag = False

        draw_board(screen, 0, 0, grid_size, visible_board)
        if not flag:
            draw_final_message(screen, board.get_winner)

        pygame.display.flip()