def move_tile_on_grid(self, tile: Tile, coordinates: Coordinates): if self.tiles_from_index.get(tile.get_id()) is None: raise TileNotExistsException("tile: (" + str(tile) + ") was not added") tile_on_coordinates = self.get_tile_from_grid(coordinates) if tile_on_coordinates is not None: if tile_on_coordinates.id != tile.id: raise TileTakenException( tile_on_coordinates, f"there is already tile: ({str(tile)}) on {str(coordinates)}" ) # else it means we move to same tile we were on previous_coordinates = self.coordinates_from_index.get(tile.get_id()) if previous_coordinates is None: raise TileNotExistsException("tile: (" + str(tile) + ") did not have coordinates") self.coordinates_from_index[tile.get_id()] = coordinates.copy() self.tile_grid[ previous_coordinates.get_array_index()] = BaseGrid.empty_tile_id self.tile_grid[coordinates.get_array_index()] = tile.get_id() self.tiles_from_index[tile.get_id()] = tile if tile.get_type() == TileType.BLOCK: self.block_tile_grid[ previous_coordinates.get_array_index()] = False self.block_tile_grid[coordinates.get_array_index()] = True
def add_tile_to_grid(self, tile: Tile, coordinates: Coordinates): if self.tiles_from_index.get(tile.get_id()) is None: self.add_new_tile(tile) else: tile_coordinates = self.coordinates_from_index.get(tile.get_id()) if tile_coordinates is not None: raise AddDuplicateTileError("tile " + str(tile) + " already exists on " + str(tile_coordinates)) tile_on_coordinates = self.get_tile_from_grid(coordinates) if tile_on_coordinates is not None: raise TileTakenException( tile_on_coordinates, f"there is already tile: ({str(tile)}) on {str(coordinates)}") self.tile_grid[coordinates.get_array_index()] = tile.get_id() self.coordinates_from_index[tile.get_id()] = coordinates if tile.get_type() == TileType.BLOCK: self.block_tile_grid[coordinates.get_array_index()] = True
def take_block(self, tile: Tile): if self.inner_block is not None: raise HasInnerBlockError("cannot get block if already has") if tile.get_type() != TileType.BLOCK: raise WrongBlockTypeError("robot can only take block type tiles") self.inner_block = tile