def move_piece(self, origin, dest, piece_type=None): x, y = string2int_coordinates(origin) x_dest, y_dest = string2int_coordinates(dest) piece = self.board[x][y] assert piece is not None, f"No piece is present on square {origin}" # TODO: Need to handle the following cases : # 1.0 : Basic movements of the pieces # 1.1 : Captures # 2 : Castling (short and long) # 3 : En-passant # 4 : Pawn promotion assert piece.is_move_legal(dest, self), "Move is not legal." self.board[x][y] = None self.board[x_dest][y_dest] = piece piece.force_update_position(dest) return True
def get_physically_legal_destinations(self, board): coords = self.get_coordinates() coords = string2int_coordinates(coords) physically_legal_destinations = [] # Towards left side of the boqrd for i in range(1, coords[0]+1): x = coords[0] - i y = coords[1] physically_legal_destinations.append(letters[x]+str(y+1)) # Towards right side of the board for i in range(1, 8-coords[0]): x = coords[0] + i y = coords[1] physically_legal_destinations.append(letters[x]+str(y+1)) # Towards top of the boqrd for i in range(1, 8-coords[1]): x = coords[0] y = coords[1] + i physically_legal_destinations.append(letters[x]+str(y+1)) # Towards bottom of the board for i in range(1, coords[1]+1): x = coords[0] y = coords[1] - i physically_legal_destinations.append(letters[x]+str(y+1)) return physically_legal_destinations
def get_trajectory(self, new_position): coords = self.get_coordinates() coords = string2int_coordinates(coords) new_coords = string2int_coordinates(new_position) sign_x_direction = 1 if new_coords[0]>coords[0] else -1 sign_y_direction = 1 if new_coords[1]>coords[1] else -1 new_x = coords[0] new_y = coords[1] trajectory = [] while new_x != new_coords[0]: new_x += sign_x_direction new_y += sign_y_direction square = letters[new_x]+str(new_y + 1) trajectory.append(square) return trajectory
def get_physically_legal_destinations(self, board): coords = self.get_coordinates() coords = string2int_coordinates(coords) physically_legal_destinations = [] for i in [-2,-1,1,2]: for j in [-2,-1,1,2]: if np.abs(i) + np.abs(j) ==3: x = coords[0] + i y = coords[1] + j if 0<= x<8 and 0<=y<8: physically_legal_destinations.append(letters[x]+str(y+1)) return physically_legal_destinations
def add_piece(self, piece_type, color, coordinates): if piece_type == "pawn": piece = Pawn(coordinates, color) elif piece_type == "rook": piece = Rook(coordinates, color) elif piece_type == "knight": piece = Knight(coordinates, color) elif piece_type == "bishop": piece = Bishop(coordinates, color) elif piece_type == "king": piece = King(coordinates, color) elif piece_type == "queen": piece = Queen(coordinates, color) else: print(f"Piece {piece_type} does not exist") import pdb pdb.set_trace() x, y = string2int_coordinates(coordinates) self.board[x][y] = piece pass
def get_physically_legal_destinations(self, board): coords = self.get_coordinates() coords = string2int_coordinates(coords) physically_legal_destinations = [] # Towards lower left-hqnd corner for i in range(1, coords[0]+1): x = coords[0] - i y = coords[1] - i if y<0: break else: physically_legal_destinations.append(letters[x]+str(y+1)) # Towards lower right-hand corner for i in range(1, 8-coords[0]): x = coords[0] + i y = coords[1] - i if y<0: break else: physically_legal_destinations.append(letters[x]+str(y+1)) # Towards top left-hand corner for i in range(1, coords[0]+1): x = coords[0] - i y = coords[1] + i if y>7: break else: physically_legal_destinations.append(letters[x]+str(y+1)) # Towards top right-hand corner for i in range(1, 8-coords[0]): x = coords[0] + i y = coords[1] + i if y>7: break else: physically_legal_destinations.append(letters[x]+str(y+1)) return physically_legal_destinations
def get_piece_on_square(self, coordinates): x, y = string2int_coordinates(coordinates) return self.board[x][y]