def __bfs(cls, x_s, y_s, x_t, y_t): state_processed = ConfigReader.getConfig_int('path', 'state_processed') state_untreated = ConfigReader.getConfig_array('path', 'state_untreated') state_pending = ConfigReader.getConfig_int('path', 'state_pending') # to be processed list_pending = queue.Queue() # queue: [(x, y, length),...] list_pending.put((x_s, y_s, 0)) # record the path having been processed map_record = copy.copy(MapService.getInitializedMap()) # loop while not list_pending.empty(): (x, y, l) = list_pending.get() if (x, y) == (x_t, y_t): return l map_record[x][y] = state_processed for dir in ['up', 'down', 'left', 'right']: loc = cls.getNextLoc((x, y), dir) if loc == (x_t, y_t): return l + 1 # if not wall if loc != False and map_record[loc[0]][ loc[1]] in state_untreated: map_record[loc[0]][loc[1]] = state_pending list_pending.put((loc[0], loc[1], l + 1))
def __init__(self): self.__config = ConfigReader() self.__screen_width = self.__config.getConfig_int( 'default', 'screen_width') self.__screen_height = self.__config.getConfig_int( 'default', 'screen_height') self.__unit_length = self.__config.getConfig_int( 'default', 'unit_length') self.__unit_width = self.__config.getConfig_int( 'default', 'unit_width') self.__unit_height = self.__config.getConfig_int( 'default', 'unit_height') self.__screen = None self.__fig = None self.__wall = None self.__path_dot = None self.__path_black = None self.__screen = pygame.display.set_mode( (self.__screen_width, self.__screen_height)) self.__fig = FigureService() self.__wall = self.__fig.getFig_wall() self.__path_dot = self.__fig.getFig_dot() self.__path_black = self.__fig.getFig_path()
def getShortestPath(cls, loc_s, loc_t): x_s, y_s = loc_s x_t, y_t = loc_t if ConfigReader.getConfig('map', 'map_path_shortest') == 'bfs': return cls.__bfs(x_s, y_s, x_t, y_t) elif ConfigReader.getConfig('map', 'map_path_shortest') == 'dfs': return cls.__dfs([(x_s, y_s)], x_t, y_t, ConfigReader.getConfig_float( 'path', 'routh_bound_upper'))
def getNextLoc(cls, loc, dir, step=1): x, y = loc [x_, y_] = np.array([x, y]) + np.array( ConfigReader.getConfig_array('action', dir)) x_, y_ = (x_ % ConfigReader.getConfig_int('default', 'unit_height'), y_ % ConfigReader.getConfig_int('default', 'unit_width')) if cls.isWall(x_, y_): return False else: return x_, y_
def __init__(self): # init config ConfigReader.loadConfig() # init map MapService.initMap() # thread lock self.__condition = threading.Condition() # init service pygame.init() self.__music = MusicService() self.__fig = FigureService() self.__interface = Interface() # init objs State.initState([Pacman(self.__condition)], [Ghost(self.__condition), Ghost(self.__condition)])
class FigureLoader: def __init__(self): self.__config = ConfigReader() self.__unit_length = self.__config.getConfig_int('default', 'unit_length') self.__path = '../data/figures/' def getFigure(self, figure): fig = pygame.image.load(self.__path + figure).convert_alpha() return pygame.transform.scale(fig, (self.__unit_length, self.__unit_length))
def __init__(self, threadCond): super(Pacman, self).__init__(Pacman.Num, threadCond) self.__num = Pacman.Num Pacman.Num += 1 self.__name = 'pacman' self.__dir = 'left' self.__x, self.__y = [(11, 1), (22, 11)][self.__num] self.__strategy = Strategy( 'PACMAN', ConfigReader.getConfig('setting', 'strategy')) # record the next action self.__x_next, self.__y_next = self.__x, self.__y self.__action = self.__dir self.__fig = FigureService()
def __init__(self, threadCond): super(Ghost, self).__init__(Ghost.Num, threadCond) self.__num = Ghost.Num Ghost.Num += 1 self.__name = ['red', 'blue', 'pink', 'yellow' ][self.__num] self.__dir = 'left' self.__x, self.__y = [(10, 11), (12, 13)][self.__num] self.__strategy = Strategy('GHOST', ConfigReader.getConfig('setting', 'strategy')) # record the next action self.__x_next, self.__y_next = self.__x, self.__y self.__action = self.__dir self.__fig = FigureService()
class GoogleMapsService(): def __init__(self): self.config_reader = ConfigReader() self.configuration = self.config_reader.read_config() self.maps_api_key = self.configuration['GOOGLE_PLACES_API_KEY'] self.gmaps = googlemaps.Client(key=self.maps_api_key) self.index = 0 def find_nearest(self, food_type: str, location: str): maploc = self.gmaps.geocode(address=location) location = maploc[0]['geometry']['location'] loc_str = str(location['lat']) + "," + str(location['lng']) place_result = self.gmaps.places_nearby(location=loc_str, keyword=food_type, radius=10000, type='restaurant') self.results = place_result['results'] return self.results[0] def find_next(self): self.index = self.index + 1 return self.results[self.index] if len( self.results) > self.index else None
class Interface(): def __init__(self): self.__config = ConfigReader() self.__screen_width = self.__config.getConfig_int( 'default', 'screen_width') self.__screen_height = self.__config.getConfig_int( 'default', 'screen_height') self.__unit_length = self.__config.getConfig_int( 'default', 'unit_length') self.__unit_width = self.__config.getConfig_int( 'default', 'unit_width') self.__unit_height = self.__config.getConfig_int( 'default', 'unit_height') self.__screen = None self.__fig = None self.__wall = None self.__path_dot = None self.__path_black = None self.__screen = pygame.display.set_mode( (self.__screen_width, self.__screen_height)) self.__fig = FigureService() self.__wall = self.__fig.getFig_wall() self.__path_dot = self.__fig.getFig_dot() self.__path_black = self.__fig.getFig_path() def initialize(self): pygame.display.set_caption("Catch Me If You CAN") # draw screen # background self.__screen.fill((0, 0, 0)) # draw map for i in range(self.__unit_height): for j in range(self.__unit_width): if (MapService.getLoc(i, j) == 1): self.__screen.blit( self.__wall, (j * self.__unit_length, i * self.__unit_length)) elif (MapService.getLoc(i, j) == 0): self.__screen.blit( self.__path_dot, (j * self.__unit_length, i * self.__unit_length)) pygame.display.flip() def draw_object(self, x, y, fig): self.__screen.blit(fig, (y * self.__unit_length, x * self.__unit_length)) def draw_pac_move(self, fig, loc_last, loc_next): # erase last loc self.__screen.blit(self.__path_black, (loc_last[1] * self.__unit_length, loc_last[0] * self.__unit_length)) # go forward and eat dot (if it has) self.__screen.blit(fig, (loc_next[1] * self.__unit_length, loc_next[0] * self.__unit_length)) MapService.eatDots(loc_next[0], loc_next[1]) def draw_ghost_move(self, fig, loc_last, loc_next): # erase last loc path_before = MapService.getLoc(loc_last[0], loc_last[1]) if path_before == ConfigReader.getConfig_int('map', 'map_empty'): fig_path = self.__path_black elif path_before == ConfigReader.getConfig_int('map', 'map_dots'): fig_path = self.__path_dot self.__screen.blit(fig_path, (loc_last[1] * self.__unit_length, loc_last[0] * self.__unit_length)) # go forward and eat dot (if it has) self.__screen.blit(fig, (loc_next[1] * self.__unit_length, loc_next[0] * self.__unit_length)) def flip(self): pygame.display.flip()
def __init__(self): self.__config = ConfigReader() self.__unit_length = self.__config.getConfig_int('default', 'unit_length') self.__path = '../data/figures/'
def isWall(cls, x, y): return cls.getLoc(x, y) == ConfigReader.getConfig_int('map', 'map_wall')
def eatDots(cls, x, y): cls.__map[x][y] = ConfigReader.getConfig_int('map', 'map_empty')
def __init__(self): self.config_reader = ConfigReader() self.configuration = self.config_reader.read_config() self.maps_api_key = self.configuration['GOOGLE_PLACES_API_KEY'] self.gmaps = googlemaps.Client(key=self.maps_api_key) self.index = 0
def __init__(self): self.__config = ConfigReader() self.__map = np.array([[ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 ], [ 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1 ], [ 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1 ], [ 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1 ], [ 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 ], [ 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1 ], [ 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1 ], [ 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1 ], [ 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 ], [ 1, 0, 1, 1, 1, 1, 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