예제 #1
0
def generate_dynamic_move(
    board: List[List],
    ptype: int,
    board_size: int,
    glist,
) -> Dict[Tuple[int, int], List[Tuple[int, int]]]:
    """Generate dynamic programming move from all `ptype` valid moves but does not execute it.

    Args:
    board: (List[List[Piece]]) State of the game.
    ptype: (int) type of piece for which random move will be generated
    board_size: (int) size of board
    """
    valid_moves = Rules.generate_valid_moves(board, ptype, board_size)
    #chooses random piece from pieces available
    dyn_from_row, dyn_from_col = random.choice(list(valid_moves.keys()))

    print('starting x:', dyn_from_row)
    print('starting y:', dyn_from_col)

    flag = 0
    #identifies which piece we are moving with flag
    decision = list(valid_moves.keys())
    if (dyn_from_row, dyn_from_col) == decision[0]:
        flag = True
        # score1 piece
    elif (dyn_from_row, dyn_from_col) == decision[1]:
        flag = False
        # score2 piece

    knight = smSearch(KnightMoves((dyn_from_row, dyn_from_col), flag,
                                  glist))  #current position
    print(knight, "robot")
    valid_moves = Rules.generate_valid_moves(board, ptype, board_size)
    print(valid_moves, "valid moves available for robot")
    if len(knight) < 2:
        return dyn_from_row, dyn_from_col, dyn_from_row, dyn_from_col, flag
    next_move = knight[1][1]
    dyn_to_row = next_move[0]
    dyn_to_col = next_move[1]
    validlist = valid_moves[(dyn_from_row, dyn_from_col)]
    print("valid list:", validlist)
    for i in range(len(validlist)):
        if (dyn_to_row, dyn_to_col) == validlist[i]:
            print("valid move within DP guidelines")
            return dyn_from_row, dyn_from_col, dyn_to_row, dyn_to_col, flag  #new position
        elif i == len(validlist):
            #if piece will eat the other piece move randomly even if DP agent
            print("DP move about to eat other piece so valid random move")
            dyn_to_row, dyn_to_col = random.choice(valid_moves[(dyn_from_row,
                                                                dyn_from_col)])
            return dyn_from_row, dyn_from_col, dyn_to_row, dyn_to_col, flag
예제 #2
0
def generate_random_move(
    board: List[List],
    ptype: int,
    board_size: int,
):
    """Generate random move from all `ptype` valid moves but does not execute it.

    Args:
        board: (List[List[Piece]]) State of the game.
        ptype: (int) type of piece for which random move will be generated
        board_size: (int) size of board
    """
    valid_moves = Rules.generate_valid_moves(board, ptype, board_size)
    #chooses random piece from pieces available
    rand_from_row, rand_from_col = random.choice(list(valid_moves.keys()))

    print('starting x:', rand_from_row)
    print('starting y:', rand_from_col)

    flag = 0
    #identifies which piece we are moving with flag
    decision = list(valid_moves.keys())
    if (rand_from_row, rand_from_col) == decision[0]:
        flag = True
        # print("objective 1")
    elif (rand_from_row, rand_from_col) == decision[1]:
        flag = False
        # print("objective 2")

    rand_to_row, rand_to_col = random.choice(valid_moves[(rand_from_row,
                                                          rand_from_col)])
    return rand_from_row, rand_from_col, rand_to_row, rand_to_col, flag
예제 #3
0
 def _can_opponent_move(
     board_list: List[List],
     opponent_ptype: int,
     board_size: int,
 ) -> bool:
     if len(
             Rules.generate_valid_moves(board_list, opponent_ptype,
                                        board_size)) == 0:
         return False
     else:
         return True
예제 #4
0
    def act(self, state):
        raw_state = state
        state = board_list2numpy(state, self.board_enc)
        state = np.reshape(state, (-1, 8, 8, 1))

        if np.random.rand() <= self.epsilon:
            valid_moves = Rules.generate_valid_moves(raw_state, self.ptype,
                                                     len(raw_state))
            rand_from_row, rand_from_col = random.choice(
                list(valid_moves.keys()))
            rand_to_row, rand_to_col = random.choice(
                valid_moves[(rand_from_row, rand_from_col)])
            action = (rand_from_row, rand_from_col, rand_to_row, rand_to_col)
        else:
            pred = self.model.predict(state)[0]
            action = self.get_action_index(pred)

            p_from_row = int(action[0])
            p_from_col = int(action[1])
            p_to_row = int(action[2])
            p_to_col = int(action[3])

            if not Rules.validate_move(raw_state, p_from_row, p_from_col,
                                       p_to_row, p_to_col):
                print('-', end='', flush=True)
                valid_moves = Rules.generate_valid_moves(
                    raw_state, self.ptype, len(raw_state))
                rand_from_row, rand_from_col = random.choice(
                    list(valid_moves.keys()))
                rand_to_row, rand_to_col = random.choice(
                    valid_moves[(rand_from_row, rand_from_col)])
                action = (rand_from_row, rand_from_col, rand_to_row,
                          rand_to_col)
            else:
                print('@', end='', flush=True)

        return int(action[0]), int(action[1]), int(action[2]), int(action[3])
예제 #5
0
def generate_random_move(
    board: List[List],
    ptype: int,
    board_size: int,
):
    """Generate random move from all `ptype` valid moves but does not execute it.

    Args:
        board: (List[List[Piece]]) State of the game.
        ptype: (int) type of piece for which random move will be generated
        board_size: (int) size of board
    """
    valid_moves = Rules.generate_valid_moves(board, ptype, board_size)
    rand_from_row, rand_from_col = random.choice(list(valid_moves.keys()))
    rand_to_row, rand_to_col = random.choice(valid_moves[(rand_from_row, rand_from_col)])
    return rand_from_row, rand_from_col, rand_to_row, rand_to_col
예제 #6
0
    def test_get_between_position(self):
        rules = Rules()
        # UP LEFT
        assert rules.get_between_position(4, 4, 3, 3) == (None, None)
        assert rules.get_between_position(4, 4, 2, 2) == (3, 3)

        # UP RIGHT
        assert rules.get_between_position(4, 4, 3, 5) == (None, None)
        assert rules.get_between_position(4, 4, 2, 6) == (3, 5)

        # DOWN LEFT
        assert rules.get_between_position(4, 4, 5, 3) == (None, None)
        assert rules.get_between_position(4, 4, 6, 2) == (5, 3)

        # DOWN RIGHT
        assert rules.get_between_position(4, 4, 5, 5) == (None, None)
        assert rules.get_between_position(4, 4, 6, 6) == (5, 5)