Ejemplo n.º 1
0
 def get_supporting_piece(board, position, team):
     x, y = utils.get_coords(position)
     if team == 'lower':
         if utils.in_bounds(utils.get_a1(x, y-1)):
             if board[x][y-1] != '__' and board[x][y-1].team == team:
                 return board[x][y-1]
     else:
         if utils.in_bounds(utils.get_a1(x, y+1)):
             if board[x][y+1] != '__' and board[x][y+1].team == team:
                 return board[x][y+1]
     return '__'
Ejemplo n.º 2
0
    def get_possible_moves(board, position, team, promoted, supported = True, enhance_piece = None):
        possible_moves = set()
        if enhance_piece is not None:
            possible_moves |= enhance_piece.get_possible_moves(board, position, team, suporting_piece.promoted, False)
        if supported:
            suporting_piece = Piece.get_supporting_piece(board, position, team)
            if suporting_piece != '__':
                possible_moves |= suporting_piece.get_possible_moves(board, position, team, suporting_piece.promoted, False)
        if promoted:
            possible_moves |= King.get_possible_moves(board, position, team, False, False)
        x, y = utils.get_coords(position)

        for i in range(1, len(board)):
            new_pos = utils.get_a1(x + i, y + i)
            if utils.in_bounds(new_pos):
                pz = board[x+i][y+i]
                if pz != '__':
                    if pz.team != team:
                        possible_moves.add(new_pos)
                    break
                else:
                    possible_moves.add(new_pos)

        for i in range(1, len(board)):
            new_pos = utils.get_a1(x - i, y + i)
            if utils.in_bounds(new_pos):
                pz = board[x-i][y+i]
                if pz != '__':
                    if pz.team != team:
                        possible_moves.add(new_pos)
                    break
                else:
                    possible_moves.add(new_pos)
        for i in range(1, len(board)):
            new_pos = utils.get_a1(x + i, y - i)
            if utils.in_bounds(new_pos):
                pz = board[x+i][y-i]
                if pz != '__':
                    if pz.team != team:
                        possible_moves.add(new_pos)
                    break
                else:
                    possible_moves.add(new_pos)
        for i in range(1, len(board)):
            new_pos = utils.get_a1(x - i, y - i)
            if utils.in_bounds(new_pos):
                pz = board[x-i][y-i]
                if pz != '__':
                    if pz.team != team:
                        possible_moves.add(new_pos)
                    break
                else:
                    possible_moves.add(new_pos)
        return possible_moves
Ejemplo n.º 3
0
 def is_in_bounds(origin, dest):
     """
     Check if origin, destination are in bounds
     :param origin: origin square
     :param dest: destination square
     :return: True if in bounds, False otherwise
     """
     try:
         o = Board.sq_to_position(origin)
         d = Board.sq_to_position(dest)
         return in_bounds(o) and in_bounds(d)
     except PositionException:
         return False
Ejemplo n.º 4
0
    def get_possible_moves(board, position, team, promoted, supported = True, enhance_piece = None):
        possible_moves = set()
        if enhance_piece is not None:
            possible_moves |= enhance_piece.get_possible_moves(board, position, team, suporting_piece.promoted, False)
        if supported:
            suporting_piece = Piece.get_supporting_piece(board, position, team)
            if suporting_piece != '__':
                possible_moves |= suporting_piece.get_possible_moves(board, position, team, suporting_piece.promoted, False)
        if promoted:
            possible_moves |= GoldGeneral.get_possible_moves(board, position, team, False, False)
            return possible_moves

        x, y = utils.get_coords(position)

        for i in range(-1, 2):
            for j in range(-1, 2):
                new_pos = utils.get_a1(x + i, y + j)
                if team == 'UPPER':
                    new_pos = utils.get_a1(x + i, y - j)

                if utils.in_bounds(new_pos) and not (i == 0 and j == 0) \
                    and not (i == -1 and j == 0) and not (i == 1 and j == 0) \
                    and not (i == 0 and j == -1):
                    possible_moves.add(new_pos)
        return possible_moves
Ejemplo n.º 5
0
 def set_coord(self, piece, a1=None):
     ''' Sets coordinate of a piece. If a1 is None, defaults to piece.position. '''
     if a1 is None:
         a1 = piece.position
     if utils.in_bounds(a1):
         x, y = utils.get_coords(a1)
         self.board[x][y] = piece
     else:
         print("Error setting coordinate.")
Ejemplo n.º 6
0
 def get_piece_at_pos(self, a1):
     ''' Returns piece object at an a1 location. '''
     if utils.in_bounds(a1):
         x, y = utils.get_coords(a1)
         if isinstance(self.board[x][y], Piece):
             return self.board[x][y]
         else:
             return '__'
     else:
         print("Coordinate not in bounds.")
         return False
Ejemplo n.º 7
0
    def get_possible_moves(board, position, team, promoted, supported = True, enhance_piece = None):
        possible_moves = set()
        if enhance_piece is not None:
            possible_moves |= enhance_piece.get_possible_moves(board, position, team, suporting_piece.promoted, False)
        if supported:
            suporting_piece = Piece.get_supporting_piece(board, position, team)
            if suporting_piece != '__':
                possible_moves |= suporting_piece.get_possible_moves(board, position, team, suporting_piece.promoted, False)

        if promoted:
            possible_moves += GoldGeneral.get_possible_moves(board, position, team, False, False)
            print(possible_moves)

            return possible_moves

        x, y = utils.get_coords(position)

        new_pos = utils.get_a1(x, y + 1)
        if team == 'UPPER':
            new_pos = utils.get_a1(x, y - 1)
        if utils.in_bounds(new_pos):
            possible_moves.add(new_pos)
        print(possible_moves)
        return possible_moves
Ejemplo n.º 8
0
def get_adj_seats_in_site(lines, row, col):
    adj = {
        'occ': 0,
        'emp': 0,
    }

    dirs = {
        (0, 1): None,
        (1, 1): None,
        (1, 0): None,
        (1, -1): None,
        (0, -1): None,
        (-1, -1): None,
        (-1, 0): None,
        (-1, 1): None
    }

    for i in range(1, max([len(lines), len(lines[0])])):
        needs_update = False
        for d in dirs:
            idx = (row + d[0] * i, col + d[1] * i)
            if dirs[d] is None and in_bounds(lines, idx[0], idx[1]):
                needs_update = True
                if lines[idx[0]][idx[1]] in ['#', 'L']:
                    dirs[d] = lines[idx[0]][idx[1]]
                    updated = True
        if not needs_update:
            break

    for d in dirs:
        if dirs[d] == '#':
            adj['occ'] += 1
        if dirs[d] == 'L':
            adj['emp'] += 1

    return adj
Ejemplo n.º 9
0
def remove_out_of_bounds(moves_list):
    """ Remove out of bounds 2-tuple coordinates from a moves list """
    for coord in moves_list:
        if not in_bounds(coord):
            moves_list.remove(coord)
    return list(set(moves_list))
Ejemplo n.º 10
0
 def test_pos_not_in_bounds(self):
     self.assertFalse(in_bounds((-1, 4)))