def get_possible_moves(self, piece_manager): possible_moves = [] for i in range(1, 3): for j in range(1, 3): if i is not j: if self.x + i < 8 and self.y + j < 8 and not piece_manager.occupied_by_friend( self.x + i, self.y + j, self.piece_side): possible_moves.append( PossibleMove(self.x + i, self.y + j, MoveType.NORMAL)) if self.x - i >= 0 and self.y - j >= 0 and not piece_manager.occupied_by_friend( self.x - i, self.y - j, self.piece_side): possible_moves.append( PossibleMove(self.x - i, self.y - j, MoveType.NORMAL)) if self.x + i < 8 and self.y - j >= 0 and not piece_manager.occupied_by_friend( self.x + i, self.y - j, self.piece_side): possible_moves.append( PossibleMove(self.x + i, self.y - j, MoveType.NORMAL)) if self.x - i >= 0 and self.y + j < 8 and not piece_manager.occupied_by_friend( self.x - i, self.y + j, self.piece_side): possible_moves.append( PossibleMove(self.x - i, self.y + j, MoveType.NORMAL)) return possible_moves
def check_select_piece(self, coord, player): '''Select piece if possible: display possible moves''' selected_piece = player.get_piece(coord) # check if piece found if selected_piece: self.piece_selected = selected_piece self.select = True # update selected case self.handeln_case(coord) # display possible moves poss_moves = self.ChessGame.get_possibles_moves(selected_piece) if selected_piece.name == 'king': # add castle moves can_long, can_short = PossibleMove.get_castle(player.color) self.handeln_castle_case(player.color, can_long, can_short) elif selected_piece.name == 'pawn': # look for en passant move en_passant_coord = PossibleMove.get_en_passant(selected_piece) if en_passant_coord != None: poss_moves.append(en_passant_coord) for coord in poss_moves: case = Board.get_case(coord) case.possible_move = True
def highlight_if_can_move_to(self, x, y, piece_manager, possible_moves): optional_piece = piece_manager.check_for_piece(x, y) if optional_piece is None: possible_moves.append(PossibleMove(x, y, MoveType.NORMAL)) return True elif optional_piece.piece_side is not self.piece_side: possible_moves.append(PossibleMove(x, y, MoveType.NORMAL)) return False elif optional_piece.piece_side is self.piece_side: return False
def get_possibles_moves(cls, piece): '''Get all possible moves (for a piece)''' if piece.name == 'pawn': movements = PossibleMove.get(piece, False) # for pawn attacking and non-attacking moves are completely differents attacks = PossibleMove.get(piece, True) for coord in attacks: if cls.players[inv_c(piece.color)].get_piece(coord): movements.append(coord) else: movements = PossibleMove.get(piece, True) # get movements as if the piece was attacking -> include capture moves return movements
def handeln_movement(cls, piece, coord): '''Handeln pieces movement''' coord = tuple(coord) is_attacking = cls.is_attacking(piece, coord) as_moved = False # special moves if piece.name == 'king': # look for castle if cls.check_castle_moves(piece): as_moved = True elif piece.name == 'pawn': # look for en passant move en_passant_coord = PossibleMove.get_en_passant(piece) if coord == en_passant_coord: cls.handeln_en_passant(piece, coord) as_moved = True if not as_moved: # if wasn't a special move if cls.check_can_move(piece, coord, is_attacking): cls.move(piece, coord, is_attacking) as_moved = True # in case of move -> end turn if as_moved: cls.end_turn(piece.color) return True
def check_for_check(cls, color, kingcheck=True): '''Check if king is attacked by an opponent piece''' king_coord = cls.players[color].king.coord # check if king coord is attack by an opponent piece for piece in cls.players[inv_c(color)].pieces: if king_coord in PossibleMove.get(piece, True, kingcheck=kingcheck): return True
def check_pawn_promotion(cls, piece): '''Check if pawn is promoting -> exec control: promote(piece) func''' # line of promotion line = PossibleMove.get_line(inv_c(piece.color)) if piece.y == line: # promote cls.controls[piece.color].promote(piece)
def check_castle_moves(cls, king): line = PossibleMove.get_line(king.color) can_long, can_short = PossibleMove.get_castle(king.color) if can_long: rock = cls.players[king.color].get_piece((0, line)) # execute long castle king.move((2,line)) rock.move((3, line)) return True if can_short: rock = cls.players[king.color].get_piece((7, line)) # execute short castle king.move((6,line)) rock.move((5, line)) return True
def check_can_move(cls, piece, coord, is_attacking): ''' Check if the given piece can move at the given destination ''' # first check if other pieces on coord if cls.players[piece.color].get_piece(coord): # other piece of same color on destination case -> can't move return False if coord in PossibleMove.get(piece, is_attacking): return True
def get_possible_moves(self, piece_manager): possible_moves = [] # Changes directions for black or white promotion_row = 0 direction = -1 if self.piece_side is PieceSide.WHITE: direction = 1 promotion_row = 7 move_type = MoveType.NORMAL if self.y + (1 * direction) is promotion_row: move_type = move_type.PROMOTION if not piece_manager.check_occupied(self.x, self.y + (1 * direction)): possible_moves.append( PossibleMove(self.x, self.y + (1 * direction), move_type)) # Checks for diagonals and right with enemy pieces if self.x + 1 < 8 and piece_manager.occupied_by_enemy( self.x + 1, self.y + (1 * direction), self.piece_side): possible_moves.append( PossibleMove(self.x + 1, self.y + (1 * direction), move_type)) # Checks for diagonals and left with enemy pieces if self.x - 1 > -1 and piece_manager.occupied_by_enemy( self.x - 1, self.y + 1 * direction, self.piece_side): possible_moves.append( PossibleMove(self.x - 1, self.y + (1 * direction), move_type)) # Checks for being on the first square and being able to move two ahead as long as it was not blocked before if not piece_manager.check_occupied( self.x, self.y + (2 * direction)) and not piece_manager.check_occupied( self.x, self.y + (1 * direction)) and self.moves is 0: possible_moves.append( PossibleMove(self.x, self.y + 2 * direction, move_type)) # Checks for diagonals and right with enemy pieces for en pessant if self.x + 1 < 8 and self.check_en_pessant( self.x + 1, self.y, self.piece_side, piece_manager): possible_moves.append( PossibleMove(self.x + 1, self.y + (1 * direction), MoveType.EN_PASSANT)) # Checks for diagonals and left with enemy pieces for en pessant if self.x - 1 > -1 and self.check_en_pessant( self.x - 1, self.y, self.piece_side, piece_manager): possible_moves.append( PossibleMove(self.x - 1, self.y + (1 * direction), MoveType.EN_PASSANT)) return possible_moves
def get_possible_moves(self, piece_manager): possible_moves = [] for i in range(-1, 2): for j in range(-1, 2): translated_x, translated_y = self.x + i, self.y + j if 0 <= translated_x < 8 and 0 <= translated_y < 8 and not ( i is 0 and j is 0): self.highlight_if_can_move_to(translated_x, translated_y, piece_manager, possible_moves) if self.moves is 0: # Checks to castle on left if piece_manager.check_for_piece( self.x - 2, self.y) is None and piece_manager.check_for_piece( self.x - 1, self.y) is None and type( piece_manager.check_for_piece(self.x - 3, self.y) ) is Rook and piece_manager.check_for_piece( self.x - 3, self.y).moves is 0: possible_moves.append( PossibleMove(self.x - 2, self.y, MoveType.KING_SIDE_CASTLE)) # Checks to castle on right if piece_manager.check_for_piece( self.x + 3, self.y) is None and piece_manager.check_for_piece( self.x + 2, self.y) is None and piece_manager.check_for_piece( self.x + 1, self.y) is None and type( piece_manager.check_for_piece( self.x + 4, self.y) ) is Rook and piece_manager.check_for_piece( self.x + 4, self.y).moves is 0: possible_moves.append(self.x + 2, self.y, MoveType.QUEEN_SIDE_CASTLE) return possible_moves
def handeln_castle_case(self, color, can_long, can_short): line = PossibleMove.get_line(color) if can_long: case = Board.get_case((2,line)) case.possible_move = True