Example #1
0
def get_horiz_vert(
    position: BoardPosition,
    ally_positions: List[BoardPosition],
    enemy_positions: List[BoardPosition],
):
    """Get possible horizontal and vertical positions given a position and the enemies and allies."""
    moves = []
    right_ok = True
    left_ok = True
    down_ok = True
    up_ok = True

    for offset in range(1, 8):
        # Right
        if right_ok and position.check_valid(offset, 0):
            m = position.get_offset(offset, 0)
            if m in ally_positions:
                right_ok = False
            elif m in enemy_positions:
                right_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Left
        if left_ok and position.check_valid(-offset, 0):
            m = position.get_offset(-offset, 0)
            if m in ally_positions:
                left_ok = False
            elif m in enemy_positions:
                left_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Up
        if up_ok and position.check_valid(0, offset):
            m = position.get_offset(0, offset)
            if m in ally_positions:
                up_ok = False
            elif m in enemy_positions:
                up_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Down
        if down_ok and position.check_valid(0, -offset):
            m = position.get_offset(0, -offset)
            if m in ally_positions:
                down_ok = False
            elif m in enemy_positions:
                down_ok = False
                moves.append(m)
            else:
                moves.append(m)

    return moves
Example #2
0
def get_diag(
    position: BoardPosition,
    ally_positions: List[BoardPosition],
    enemy_positions: List[BoardPosition],
):
    """Get possible diagonal positions given a position and the enemies and allies."""
    moves = []
    ul_ok = True
    ur_ok = True
    dl_ok = True
    dr_ok = True

    for offset in range(1, 8):
        # Up and right
        if ur_ok and position.check_valid(offset, offset):
            m = position.get_offset(offset, offset)
            if m in ally_positions:
                ur_ok = False
            elif m in enemy_positions:
                ur_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Up and left
        if ul_ok and position.check_valid(-offset, offset):
            m = position.get_offset(-offset, offset)
            if m in ally_positions:
                ul_ok = False
            elif m in enemy_positions:
                ul_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Down and right
        if dr_ok and position.check_valid(offset, -offset):
            m = position.get_offset(offset, -offset)
            if m in ally_positions:
                dr_ok = False
            elif m in enemy_positions:
                dr_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Down
        if dl_ok and position.check_valid(-offset, -offset):
            m = position.get_offset(-offset, -offset)
            if m in ally_positions:
                dl_ok = False
            elif m in enemy_positions:
                dl_ok = False
                moves.append(m)
            else:
                moves.append(m)

    return moves