Exemplo n.º 1
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
Exemplo 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
Exemplo n.º 3
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 '__'
Exemplo n.º 4
0
 def king_in_check(self, player_turn):
     ''' Returns True is player's King is currently in check. '''
     for a in range(const.BOARD_SIZE):
         for b in range(const.BOARD_SIZE):
             piece = self.board[a][b]
             if isinstance(piece, King) and piece.team == player_turn:
                 if self.pos_in_check(utils.get_a1(a, b), player_turn):
                     return True
     return False
Exemplo n.º 5
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
Exemplo n.º 6
0
def find_available_moves(board_obj, player_turn):
    ''' Searches for moves to get out of check. '''
    available_moves = []
    for a in range(const.BOARD_SIZE):
        for b in range(const.BOARD_SIZE):
            # Moves
            piece = board_obj.board[a][b]
            if isinstance(piece, Piece) and piece.team == player_turn:
                for move in piece.get_possible_moves(board_obj.board,
                                                     piece.position,
                                                     piece.team,
                                                     piece.promoted):
                    move_str = 'move ' + piece.position + ' ' + move

                    if board_obj.can_move_piece(piece.position, move,
                                                player_turn, True):
                        if simulate_move(deepcopy(board_obj), piece, move,
                                         player_turn, True):
                            available_moves.append(move_str + ' promote')

                    if board_obj.can_move_piece(piece.position, move,
                                                player_turn, False):
                        if simulate_move(deepcopy(board_obj), piece, move,
                                         player_turn, False):
                            available_moves.append(move_str)
            # Drops
            elif piece == '__':
                captures = board_obj.upper_captures if player_turn == 'UPPER' else board_obj.lower_captures
                for pz in captures:
                    pos = utils.get_a1(a, b)
                    drop_str = 'drop ' + str(pz).lower() + ' ' + pos
                    if board_obj.can_drop_piece(player_turn, pz, pos):
                        if simulate_drop(deepcopy(board_obj), player_turn, pz,
                                         pos):
                            available_moves.append(drop_str)
    return sorted(available_moves)