def __mapBlockSurrounding(self, block, blocks): """ Maps the surrounding of the block given """ encoded_map = bitmap.BitMap(8) #8 Surrounding blocks total_walls = 0 #Make list of all surrounding blocks in board bx, by = block surrounding = [ (sur_x, sur_y) for sur_x in range(bx - self.size, bx + self.size * 2, self.size) for sur_y in range(by - self.size, by + self.size * 2, self.size) if sur_x != bx or sur_y != by ] #Encode the surroundings into the bitmap for index, (x, y) in enumerate(surrounding): if (snake.Block(self.size, x, y) in blocks or x <= self.leftBoundry or y <= 0 or x >= self.rightBoundry or y >= self.gameHeight): encoded_map.set(index) return encoded_map
def __init__(self, size): """Initialize SectorMap. Args: size: The number of sectors to account for. """ self.map_size = size self.map = bitmap.BitMap(size)
def encodeState(cls, snake_obj, food): """ Encodes a state into the following format: direction,quadrantOfFood,BottomRight,Right,TopRight,Bottom,Top,BottomLeft,Left,TopLeft Direction: 00 - Up 01 - Left 10 - Down 11 - Right Quadrant: 00 - I 01 - II 10 - III 11 - IV Surrounding bits: 1 - Obstacle 0 - Safe returns the encoded string as a bitmap """ # Note, in range() the stop parameter is set as x + width * 2 because the end value is not inclusive # Here, we are Getting the 8 blocks surrounding the head (in the order of the encoding) and for each of # the 8 blocks we are attaching every piece of the tail. This will give 8 * len(tail) values to compare. minimum_value = 1 if len(snake_obj.tail) == 0 else len(snake_obj.tail) #To fix mod by 0 error #surrounding = QTable.__mapSurrounding(snake_obj, minimum_value) # 2 bits for direction, 2 bits for quadrant, one bit each for 8 square locations around snake encoded_map=bitmap.BitMap(12) # Checks each block in the surrounding and checks if it is near a wall, or near a piece of the tail #bit_position=0 #for (index, (x, y, block)) in enumerate(surrounding): # if index % minimum_value == 0 and index != 0: # Don't increment immediately # bit_position += 1 # if (x < snake_obj.game.leftBoundry or y < 0 or x == snake_obj.game.rightBoundry or y == constant.WINDOW_HEIGHT # or (block != None and block.colliderect(snake.Block(constant.BLOCK_SIZE, x, y))) # and not encoded_map.test(bit_position)): # encoded_map.set(bit_position) cls.__encodeSurrounding(encoded_map, minimum_value, snake_obj) bit_position = 7 QTable.__encodeFoodPosition(snake_obj, food, encoded_map, bit_position+1) QTable.__encodeDirection(snake_obj, encoded_map, bit_position+3) return encoded_map.tostring()[4:] #We only need 12 bits, not 16
def __mapBlockSurrounding(self, block, blocks): encoded_map = bitmap.BitMap(8) #8 Surrounding blocks total_walls = 0 #Make list of all surrounding blocks in board bx, by = block surrounding = [(sur_x, sur_y) for sur_x in range(bx - self.block_width, bx + self.block_width * 2, self.block_width) for sur_y in range(by - self.block_width, by + self.block_width * 2, self.block_width) if sur_x != bx or sur_y != by] #Encode the surroundings into the bitmap for index, (x, y) in enumerate(surrounding): if (blog_2.Block(self.block_width, x, y) in blocks or x <= 0 or y <= 0 or x >= self.width or y >= self.height ): encoded_map.set(index) return encoded_map
import bitmap as bm m1 = bm.BitMap([0]) m1.readBitmap('bitmap.jpg') m1.scale(1) m1.show(20)
def __init__(self, maps, bit_map_path, nbytes): self._map_paths = maps self._bit_map_path = bit_map_path self._bit_map = bitmap.BitMap((nbytes + self.BLOCK - 1) // self.BLOCK)
import bitmap b = bitmap.BitMap() b.filename = 'medium' b.Load_File() if 1: b.Separate_Chars() b.first_char = '!' # '!' or '+' b.Save_Font() else: b.Save_Image() #bitmap.Write_String('!','~')
def encodeState(self, snake_obj, food): encoded_map = bitmap.BitMap(12) bit_position = 0 leftBoundry = 0 rightBoundry = 800 bottomBoundry = 600 topBoundry = 0 #Encode the surrounding #Go over the columns from left to right for x in range(snake_obj.x - snake_obj.width, snake_obj.x + snake_obj.width * 2, snake_obj.width): #Go over the squares from top to bottom for y in range(snake_obj.y - snake_obj.width, snake_obj.y + snake_obj.width * 2, snake_obj.width): if (x, y) == (snake_obj.x, snake_obj.y): continue #Loop over the tail for block in [snake_obj] + snake_obj.tail: #Check if that square has a tail block or hits a wall if blog_2.Block(snake_obj.width, x, y).colliderect(block) or y == bottomBoundry or \ y == topBoundry or x == leftBoundry or x == rightBoundry: encoded_map.set(bit_position) break bit_position += 1 #Enocde Quadrants #food is on the right of the head and above or equal to the head if food.x > snake_obj.x and food.y <= snake_obj.y: bit_position += 2 #00 #Food is on the left or equal to the head and above the head elif food.x <= snake_obj.x and food.y < snake_obj.y: encoded_map.set(bit_position) bit_position += 2 #Food is on the left of the head and below or equal to the head elif food.x < snake_obj.x and food.y >= snake_obj.y: bit_position += 1 encoded_map.set(bit_position) bit_position += 1 #Food is on the right of or equal to the head and below the head else: encoded_map.set(bit_position) encoded_map.set(bit_position + 1) bit_position += 2 #Encode Direction if snake_obj.direction.name == "UP": bit_position += 2 elif snake_obj.direction.name == "LEFT": encoded_map.set(bit_position) bit_position += 2 elif snake_obj.direction.name == "DOWN": bit_position += 1 encoded_map.set(bit_position) bit_position += 1 else: encoded_map.set(bit_position) encoded_map.set(bit_position + 1) bit_position += 2 return encoded_map.tostring( )[4:] #We only need 12 bits, but the bitmap defaults to hold bytes (16 bits).