Exemplo n.º 1
0
    def get_long_castling_move(self, board):
        if (self.color == Piece.WHITE and board.white_king_moved):
            return 0
        if (self.color == Piece.BLACK and board.black_king_moved):
            return 0

        piece = board.get_piece(self.x - 4, self.y)
        if (piece != 0):
            if (piece.color == self.color
                    and piece.piece_type == Rook.PIECE_TYPE):
                if (board.get_piece(self.x - 1, self.y) == 0
                        and board.get_piece(self.x - 2, self.y) == 0
                        and board.get_piece(self.x - 3,
                                            self.y) == 0):  #1st condition
                    #The 2nd condition break the IA, if commented the IA will stop crushing
                    #This may be due to the amount of memory
                    #if( not board.is_check(self.color) ): #2nd condition
                    if (not (self.moved)
                            and not (piece.moved)):  #3nd condition
                        if (board.imaginary_check(self.color, self.x - 1,
                                                  self.y) == False
                                and board.imaginary_check(
                                    self.color, self.x - 2, self.y) == False
                                and board.imaginary_check(
                                    self.color, self.x - 3,
                                    self.y) == False):  #4th condition
                            #print("You can performe Long castling")
                            return ai.Move(self.x, self.y, self.x - 2, self.y,
                                           True)

        return 0
Exemplo n.º 2
0
    def get_possible_horizontal_moves(self, board):
        moves = []

        # Moves to the right of the piece.
        for i in range(1, 8 - self.x):
            piece = board.get_piece(self.x + i, self.y)
            moves.append(self.get_move(board, self.x + i, self.y))

            if (piece != 0):
                break

        # Moves to the left of the piece.
        for i in range(1, self.x + 1):
            piece = board.get_piece(self.x - i, self.y)
            moves.append(self.get_move(board, self.x - i, self.y))
            if (piece != 0):
                break

        # Downward moves.
        for i in range(1, 8 - self.y):
            piece = board.get_piece(self.x, self.y + i)
            moves.append(self.get_move(board, self.x, self.y + i))
            if (piece != 0):
                break

        # Upward moves.
        for i in range(1, self.y + 1):
            piece = board.get_piece(self.x, self.y - i)
            moves.append(self.get_move(board, self.x, self.y - i))
            if (piece != 0):
                break

        return self.remove_null_from_list(moves)
Exemplo n.º 3
0
    def get_short_castling_move(self, board):
        if (self.color == Piece.WHITE and board.white_king_moved):
            return 0
        if (self.color == Piece.BLACK and board.black_king_moved):
            return 0

        piece = board.get_piece(self.x + 3, self.y)
        if (piece != 0):
            if (piece.color == self.color
                    and piece.piece_type == Rook.PIECE_TYPE):
                if (board.get_piece(self.x + 1, self.y) == 0
                        and board.get_piece(self.x + 2,
                                            self.y) == 0):  #1st condition
                    if (not board.is_check(self.color)):  #2nd condition
                        if (not (self.moved)
                                and not (piece.moved)):  #3nd condition
                            if (board.imaginary_check(self.color, self.x + 1,
                                                      self.y) == False
                                    and board.imaginary_check(
                                        self.color, self.x + 2,
                                        self.y) == False):  #4th condition
                                #print("You can performe Short castling")
                                return ai.Move(self.x, self.y, self.x + 2,
                                               self.y, True)

        return 0
Exemplo n.º 4
0
    def get_possible_horizontal_moves(self, board):
        moves = []

        # Right
        for i in range(1, 8 - self.x):
            piece = board.get_piece(self.x + i, self.y)
            moves.append(self.get_move(board, self.x + i, self.y))

            if (piece != 0):
                break

        # Left
        for i in range(1, self.x + 1):
            piece = board.get_piece(self.x - i, self.y)
            moves.append(self.get_move(board, self.x - i, self.y))
            if (piece != 0):
                break

        # Down
        for i in range(1, 8 - self.y):
            piece = board.get_piece(self.x, self.y + i)
            moves.append(self.get_move(board, self.x, self.y + i))
            if (piece != 0):
                break

        # Up
        for i in range(1, self.y + 1):
            piece = board.get_piece(self.x, self.y - i)
            moves.append(self.get_move(board, self.x, self.y - i))
            if (piece != 0):
                break

        return self.remove_null_from_list(moves)
Exemplo n.º 5
0
    def step_possible(self, pieces, column_step_multiplier, row_step_multiplier, step_limit=None, allow_capture=True, allow_non_capture=True):
        moves = []
        while True:
            try_steps = len(moves) + 1
            column = self.column + try_steps * column_step_multiplier
            row = self.row + try_steps * row_step_multiplier
            box = board.get_box(row, column)
            capture = board.get_piece(pieces, box)
            move = board.get_move(self, box, capture, None)

            if not (0 <= row <= 7) or not (0 <= column <= 7):
                break

            piece = board.get_piece(pieces, box)
            if piece and piece.color != self.color and allow_capture:
                moves.append(move)
                break
            if not piece and allow_non_capture:
                moves.append(move)
                if step_limit and len(moves) >= step_limit:
                    break
            else:
                break

        return moves
Exemplo n.º 6
0
    def get_possible_moves(self, board):
        moves = []

        # Direction the pawn can move in.
        direction = -1
        if (self.color == Piece.BLACK):
            direction = 1

        # The general 1 step forward move.
        if (board.get_piece(self.x, self.y + direction) == 0):
            moves.append(self.get_move(board, self.x, self.y + direction))

        # The Pawn can take 2 steps as the first move.
        if (self.is_starting_position()
                and board.get_piece(self.x, self.y + direction) == 0
                and board.get_piece(self.x, self.y + direction * 2) == 0):
            moves.append(self.get_move(board, self.x, self.y + direction * 2))

        # Eating pieces.
        piece = board.get_piece(self.x + 1, self.y + direction)
        if (piece != 0):
            moves.append(self.get_move(board, self.x + 1, self.y + direction))

        piece = board.get_piece(self.x - 1, self.y + direction)
        if (piece != 0):
            moves.append(self.get_move(board, self.x - 1, self.y + direction))

        return self.remove_null_from_list(moves)
Exemplo n.º 7
0
    def get_bottom_castling_move(self, board):
        if (self.color == Piece.WHITE and board.white_king_moved):
            return 0
        if (self.color == Piece.BLACK and board.black_king_moved):
            return 0

        piece = board.get_piece(self.x, self.y+4)
        if (piece != 0):
            if (piece.color == self.color and piece.piece_type == Rook.PIECE_TYPE):
                if (board.get_piece(self.x, self.y+1) == 0 and board.get_piece(self.x, self.y+2) == 0 and board.get_piece(self.x, self.y+3) == 0):
                    return ai.Move(self.x, self.y, self.x, self.y+2, True)

        return 0
Exemplo n.º 8
0
 def moveIsTaken(self, board: board.Board, row: int, column: int) -> bool:
     "checks if move is empty on the board"
     piece = board.get_piece(row, column)
     if piece.get_kind() == '.':
         return False
     else:
         return True
Exemplo n.º 9
0
    def handle_turn(self, pieces, turn, click, enemy):
        if not click:
            return

        selected_move = None
        for move in self.possible_from_selected:
            if move['box'] == click:
                selected_move = move
                break

        if selected_move:
            self.selected = None
            self.possible_from_selected = []
            turn(selected_move)

        else:
            piece = board.get_piece(pieces, click)
            if piece and piece.color == self.color:
                self.selected = click
                self.possible_from_selected = [
                    move
                    for move in filter(lambda move: move['piece'] == piece,
                                       self.possible_moves)
                ]
            else:
                self.selected = None
                self.possible_from_selected = []
Exemplo n.º 10
0
 def get_move(self, board, xto, yto):
     move = 0
     if (board.in_bounds(xto, yto)):
         piece = board.get_piece(xto, yto)
         if (piece != 0):
             if (piece.color != self.color):
                 move = ai.Move(self.x, self.y, xto, yto, False)
         else:
             move = ai.Move(self.x, self.y, xto, yto, False)
     return move
Exemplo n.º 11
0
    def get_possible_diagonal_moves(self, board):
        moves = []

        for i in range(1, 8):
            if (not board.in_bounds(self.x + i, self.y + i)):
                break

            piece = board.get_piece(self.x + i, self.y + i)
            moves.append(self.get_move(board, self.x + i, self.y + i))
            if (piece != 0):
                break

        for i in range(1, 8):
            if (not board.in_bounds(self.x + i, self.y - i)):
                break

            piece = board.get_piece(self.x + i, self.y - i)
            moves.append(self.get_move(board, self.x + i, self.y - i))
            if (piece != 0):
                break

        for i in range(1, 8):
            if (not board.in_bounds(self.x - i, self.y - i)):
                break

            piece = board.get_piece(self.x - i, self.y - i)
            moves.append(self.get_move(board, self.x - i, self.y - i))
            if (piece != 0):
                break

        for i in range(1, 8):
            if (not board.in_bounds(self.x - i, self.y + i)):
                break

            piece = board.get_piece(self.x - i, self.y + i)
            moves.append(self.get_move(board, self.x - i, self.y + i))
            if (piece != 0):
                break

        return self.remove_null_from_list(moves)
Exemplo n.º 12
0
 def allyFoundAtLineEnd(self, board: board.Board, enemy_line: list,
                        row_delta: int, col_delta: int,
                        ally_kind: str) -> bool:
     "used in get flippable enemies function"
     last_piece = enemy_line[len(enemy_line) - 1]
     search_row = last_piece.get_row() + row_delta
     search_col = last_piece.get_col() + col_delta
     if not rules.Rules().moveIsOnGrid(board, search_row, search_col):
         return False
     piece = board.get_piece(search_row, search_col)
     if not piece.get_kind() == ally_kind:
         return False
     return True
Exemplo n.º 13
0
 def getAdjacentPieces(self, board: board.Board, row: int,
                       column: int) -> list:
     "list of all adjacent pieces givent a row,column coordinate"
     pieces = []
     for i in range(3):
         row_delta = i - 1
         for j in range(3):
             col_delta = j - 1
             search_row = row + row_delta
             search_col = column + col_delta
             if (rules.Rules().moveIsOnGrid(board, search_row, search_col)):
                 piece = board.get_piece(search_row, search_col)
                 pieces.append(piece)
     return pieces
Exemplo n.º 14
0
    def possible_moves(self, pieces):
        possible_moves = []
        possible_moves += self.step_possible(pieces, 1, 0, 1)
        possible_moves += self.step_possible(pieces, -1, 0, 1)
        possible_moves += self.step_possible(pieces, 0, 1, 1)
        possible_moves += self.step_possible(pieces, 0, -1, 1)
        possible_moves += self.step_possible(pieces, 1, 1, 1)
        possible_moves += self.step_possible(pieces, 1, -1, 1)
        possible_moves += self.step_possible(pieces, -1, 1, 1)
        possible_moves += self.step_possible(pieces, -1, -1, 1)

        if not self.has_moved:
            castle_rooks = []
            row = 0 if self.color == PieceColor.BLACK else 7
            for piece in pieces:
                if piece.color == self.color and piece.piece_type == PieceType.ROOK and not piece.has_moved:
                    castle_rooks.append(piece)

            for rook in castle_rooks:
                lowest_column = min(rook.column + 1, self.column + 1)
                highest_column = max(rook.column, self.column)
                possible = True
                for column in range(lowest_column, highest_column):
                    piece = board.get_piece(pieces, board.get_box(row, column))
                    if piece:
                        possible = False
                        break
                    if board.is_chess(
                            board.pieces_after_move(
                                pieces,
                                board.get_move(self,
                                               board.get_box(row, column),
                                               None, None)), self.color):
                        possible = False
                        break

                if possible:
                    direction = 1 if self.column < rook.column else -1
                    extra_move = board.get_move(
                        rook,
                        board.get_box(
                            row, self.column + direction * 2 + direction * -1),
                        None, None)
                    move = board.get_move(
                        self, board.get_box(row, self.column + direction * 2),
                        None, extra_move)
                    possible_moves.append(move)

        return possible_moves
Exemplo n.º 15
0
    def possible_moves(self, pieces):
        direction = -1 if self.color == PieceColor.WHITE else 1
        steps = 2 if not self.has_moved else 1
        moves = []
        moves += self.step_possible(pieces, 0, direction, steps, False)
        moves += self.step_possible(pieces, 1, direction, 1, True, False)
        moves += self.step_possible(pieces, -1, direction, 1, True, False)

        if self.row == (3 if direction == -1 else 4):
            to_right = board.get_piece(
                pieces, board.get_box(self.row, self.column + 1))
            if to_right and to_right.color != self.color and to_right.piece_type == PieceType.PAWN and to_right.can_be_en_passented:
                move = self.step_possible(pieces, 1, direction, 1, False)[0]
                move['capture'] = to_right
                moves.append(move)

            to_left = board.get_piece(pieces,
                                      board.get_box(self.row, self.column - 1))
            if to_left and to_left.color != self.color and to_left.piece_type == PieceType.PAWN and to_left.can_be_en_passented:
                move = self.step_possible(pieces, -1, direction, 1, False)[0]
                move['capture'] = to_left
                moves.append(move)

        return moves
Exemplo n.º 16
0
    def get_possible_moves(self, board):
        moves = []

        direction = -1
        if (self.color == Piece.BLACK):
            direction = 1

        if (board.get_piece(self.x, self.y + direction) == 0):
            moves.append(self.get_move(board, self.x, self.y + direction))

        if (self.is_starting_position()
                and board.get_piece(self.x, self.y + direction) == 0
                and board.get_piece(self.x, self.y + direction * 2) == 0):
            moves.append(self.get_move(board, self.x, self.y + direction * 2))

        piece = board.get_piece(self.x + 1, self.y + direction)
        if (piece != 0):
            moves.append(self.get_move(board, self.x + 1, self.y + direction))

        piece = board.get_piece(self.x - 1, self.y + direction)
        if (piece != 0):
            moves.append(self.get_move(board, self.x - 1, self.y + direction))

        return self.remove_null_from_list(moves)
Exemplo n.º 17
0
    def getAllMoves(self, board: board.Board, enemy_kind, ally_kind) -> list:
        "list of all available moves pieces on ally_kind"
        move = []
        for row in range(board.get_rows()):
            for col in range(board.get_columns()):
                if rules.Rules().moveIsTaken(board, row, col) == True:
                    continue
                if rules.Rules().moveIsOnGrid(board, row, col) == False:
                    continue
                if rules.Rules().moveHasFlippableEnemies(
                        board, row, col, ally_kind, enemy_kind) == False:
                    continue
                move.append(board.get_piece(row, col))

        return move
Exemplo n.º 18
0
 def getEnemiesInLine(self, board: board.Board, first_enemy: piece.Piece,
                      row_delta: int, col_delta: int) -> list:
     'runs at least once and updates the current piece at the end of looop'
     "used in getFlippableEnemies"
     pieces = []
     current_piece = first_enemy
     while True:
         if not current_piece.get_kind() == first_enemy.get_kind():
             break
         pieces.append(current_piece)
         search_row = current_piece.get_row() + row_delta
         search_col = current_piece.get_col() + col_delta
         if not rules.Rules().moveIsOnGrid(board, search_row, search_col):
             break
         current_piece = board.get_piece(search_row, search_col)
     return pieces