Exemplo n.º 1
0
    def test_pawn_simple(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [100, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (6, 0)

        actual_list = []
        MOVES.generate_pawn(test_pos, test_board, actual_list)
        expected_list = [[(6, 0), (5, 0)], [(6, 0), (4, 0)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list

        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 100, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (4, 1)

        actual_list = []
        MOVES.generate_pawn(test_pos, test_board, actual_list)
        expected_list = [[(4, 1), (3, 1)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 2
0
    def test_pawn_black(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [101, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (1, 0)

        actual_list = []
        MOVES.generate_pawn(test_pos, test_board, actual_list)
        expected_list = [[(1, 0), (2, 0)], [(1, 0), (3, 0)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list

        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [101, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (2, 0)

        actual_list = []
        MOVES.generate_pawn(test_pos, test_board, actual_list)
        expected_list = [[(2, 0), (3, 0)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 3
0
    def test_bishop_black(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 331, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (6, 1)

        actual_list = []
        MOVES.generate_bishop(test_pos, test_board, actual_list)
        expected_list = [[(6, 1), (7, 0)], [(6, 1), (7, 2)], [(6, 1), (5, 0)],
                         [(6, 1), (5, 2)], [(6, 1), (4, 3)], [(6, 1), (3, 5)],
                         [(6, 1), (2, 6)], [(6, 1), (1, 7)]]
Exemplo n.º 4
0
    def test_pawn_black_capture(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 101, 0, 0, 0, 0, 0],
                       [0, 100, 0, 100, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (3, 2)

        actual_list = []
        MOVES.generate_pawn(test_pos, test_board, actual_list)
        expected_list = [[(3, 2), (4, 2)], [(3, 2), (4, 1)], [(3, 2), (4, 3)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 5
0
    def test_knight_capture(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 101, 0, 0, 0, 0, 0, 0],
                       [0, 0, 101, 0, 0, 0, 0, 0], [320, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (7, 0)

        actual_list = []
        MOVES.generate_knight(test_pos, test_board, actual_list)
        expected_list = [[(7, 0), (5, 1)], [(7, 0), (6, 2)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 6
0
    def test_bishop_capture(self):
        test_board = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 101, 0, 101, 0, 0, 0],
                      [0, 0, 0, 330, 0, 0, 0, 0], [0, 0, 101, 0, 101, 0, 0, 0]]
        test_pos = (6, 3)

        actual_list = []
        MOVES.generate_bishop(test_pos, test_board, actual_list)
        expected_list = [[(6, 3), (5, 2)], [(6, 3), (5, 4)], [(6, 3), (7, 2)],
                         [(6, 3), (7, 4)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 7
0
    def test_king_simple(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 40000, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (6, 1)

        actual_list = []
        MOVES.generate_king(test_pos, test_board, actual_list)
        expected_list = [[(6, 1), (6, 0)], [(6, 1), (6, 2)], [(6, 1), (5, 0)],
                         [(6, 1), (5, 1)], [(6, 1), (5, 2)], [(6, 1), (7, 0)],
                         [(6, 1), (7, 1)], [(6, 1), (7, 2)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 8
0
    def test_rook_capture(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 101, 0, 0, 0, 0, 0, 0],
                       [101, 500, 101, 0, 0, 0, 0, 0],
                       [0, 101, 0, 0, 0, 0, 0, 0]])
        test_pos = (6, 1)

        actual_list = []
        MOVES.generate_rook(test_pos, test_board, actual_list)
        expected_list = [[(6, 1), (6, 0)], [(6, 1), (6, 2)], [(6, 1), (7, 1)],
                         [(6, 1), (5, 1)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 9
0
    def test_bishop_simple(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 330, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (6, 3)

        actual_list = []
        MOVES.generate_bishop(test_pos, test_board, actual_list)
        expected_list = [[(6, 3), (5, 2)], [(6, 3), (5, 4)], [(6, 3), (4, 1)],
                         [(6, 3), (4, 5)], [(6, 3), (3, 0)], [(6, 3), (3, 6)],
                         [(6, 3), (2, 7)], [(6, 3), (7, 2)], [(6, 3), (7, 4)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 10
0
    def test_knight_simple(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 320, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (4, 3)

        actual_list = []
        MOVES.generate_knight(test_pos, test_board, actual_list)
        expected_list = [[(4, 3), (6, 2)], [(4, 3), (6, 4)], [(4, 3), (2, 2)],
                         [(4, 3), (2, 4)], [(4, 3), (3, 1)], [(4, 3), (3, 5)],
                         [(4, 3), (5, 1)], [(4, 3), (5, 5)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 11
0
    def test_directional(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (2, 2)
        test_direction = (-1, -1)
        test_step_size = 8
        actual_list = []
        expected_list = [[(2, 2), (1, 1)], [(2, 2), (0, 0)]]

        MOVES.directional_moves(test_pos, test_board, test_direction,
                                test_step_size, actual_list)

        assert len(actual_list) is len(expected_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 12
0
    def test_rook_simple(self):
        test_board = ([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 500, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
        test_pos = (6, 1)

        actual_list = []
        MOVES.generate_rook(test_pos, test_board, actual_list)
        expected_list = [[(6, 1), (6, 0)], [(6, 1), (6, 2)], [(6, 1), (6, 3)],
                         [(6, 1), (6, 4)], [(6, 1), (6, 5)], [(6, 1), (6, 6)],
                         [(6, 1), (6, 7)], [(6, 1), (7, 1)], [(6, 1), (5, 1)],
                         [(6, 1), (4, 1)], [(6, 1), (3, 1)], [(6, 1), (2, 1)],
                         [(6, 1), (1, 1)], [(6, 1), (0, 1)]]

        assert len(expected_list) is len(actual_list)

        for move in expected_list:
            assert move in actual_list
Exemplo n.º 13
0
    def test_start_pos(self):
        """
		pawns aren't being generated
		"""
        test_board = ([[0, 321, 0, 0, 0, 0, 0, 0], [101, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 100, 0, 0, 0, 0, 0, 0], [0, 320, 0, 0, 0, 0, 0, 0]])

        actual_list = MOVES.generate_moves(0, test_board)
        expected_list = [[(0, 1), (2, 0)], [(0, 1), (2, 2)]]
Exemplo n.º 14
0
def make_move(current_state):
    global PLAYERS_TURN   
    PLAYERS_TURN = current_state.whose_move
    print("EVAL: " + str(EVAL.eval_board(current_state.board, current_state.whose_move)))
    print("Trying to move player " , PLAYERS_TURN)

    MOVES.PLAYERS_TURN = PLAYERS_TURN
    new_state = CS.ChessState(current_state.board)

    new_state.whose_move = 1 - current_state.whose_move
    move_list = MOVES.generate_moves(PLAYERS_TURN, current_state.board)
    

    # [(old_pos, new_pos), new_state] 

    print('Calling ALPHA BETA')

    best_move = AB.runAlphaBeta(PLAYERS_TURN, current_state, 2)
    temp_move = best_move[1]
    best_state = temp_move[1]
    # print("EVAL: " + str(EVAL.eval_board(best_state.board, best_state.whose_move)) + ", " + str(temp_move))

    # print('hey check for correct form')
    # print(best_move[1])

    return best_move[1]

    try:
        print("Generating a move")
        move = random.choice(moves_list)
        move_touple = (move[0], move[1])
        update_board((new_state, move_touple))
        return [move_touple, new_state]
    except:
        print("Movement failed")
        pass
Exemplo n.º 15
0
def alphabeta(currentState, depth_left, A, B, bool_maximizingPlayer,
              player_number):
    """Finds the next best move to complete.
    
    :param node: oldpos, newpos, killpos
    :param depth_left: levels of depth left
    :type depth_left: int
    :param A: alpha, start with -9999999
    :param B: beta, start with 9999999
    :param bool_maximizingPlayer: is it maximizing turn
    :type bool_maximizingPlayer: boolean
    :return: heuristic, move
    """

    # base: no more tree discovering. Return the heuristic score for the given board state
    if (depth_left == 0):
        # return PLAYER.dumb_heuristic(currentState.board, player_number)
        val = EP.eval_board(currentState.board, player_number)
        # print('val = ' + str(val))
        return val, currentState

    # get all children moves from the given node
    children = MOVES.generate_moves(player_number, currentState.board)

    # who's turn it'll be next
    nextTurnPlayerNumber = 0
    if player_number == 0:
        nextTurnPlayerNumber = 1

    if bool_maximizingPlayer:
        v = -999999
        best_child = None
        # produce children of node
        for child in children:
            child_state = child[1]
            child_move = child[0]

            # v = max(v, alphabeta(child, depth_left - 1, A, B, False))
            returnedInformation = alphabeta(child_state, depth_left - 1, A, B,
                                            False, nextTurnPlayerNumber)
            old_v = v
            # print('returned Information: ' + str(returnedInformation))
            v = max(v, returnedInformation[0])
            # print(v)
            # check if it was changed. if it was update Node
            if v is not old_v:
                # best_child = returnedInformation[1]
                best_child = child

            A = max(A, v)
            if B <= A:
                updatePruning()
                break  # (* B cut-off *):
        # print("BEST_MOVE: " + str(v) + ", " + str(depth_left) + ", " + str(best_child[0]))
        return v, best_child
    # Minimizing player
    else:
        v = 999999
        best_child = None

        for child in children:
            # v = min(v, alphabeta(child, depth_left - 1, A, B, True))

            child_state = child[1]
            child_move = child[0]

            returnedInformation = alphabeta(child_state, depth_left - 1, A, B,
                                            True, nextTurnPlayerNumber)
            # alphabeta(working_board, depth_left - 1, A, B, True, nextTurnPlayerNumber)
            old_v = v
            v = min(v, returnedInformation[0])
            # print(v)
            # check if it was changed. if it was update best child
            if v is not old_v:
                # best_child = returnedInformation[1]
                best_child = child

            B = min(B, v)
            if B <= A:
                updatePruning()
                break  # (* A cut-off *)
        # print("BEST_MOVE (Minimizing): " + str(v) + ", " + str(depth_left))
        return v, best_child