我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用game.Grid()。
def initializeVisibilityMatrix(self): global VISIBILITY_MATRIX_CACHE if reduce(str.__add__, self.layoutText) not in VISIBILITY_MATRIX_CACHE: from game import Directions vecs = [(-0.5,0), (0.5,0),(0,-0.5),(0,0.5)] dirs = [Directions.NORTH, Directions.SOUTH, Directions.WEST, Directions.EAST] vis = Grid(self.width, self.height, {Directions.NORTH:set(), Directions.SOUTH:set(), Directions.EAST:set(), Directions.WEST:set(), Directions.STOP:set()}) for x in range(self.width): for y in range(self.height): if self.walls[x][y] == False: for vec, direction in zip(vecs, dirs): dx, dy = vec nextx, nexty = x + dx, y + dy while (nextx + nexty) != int(nextx) + int(nexty) or not self.walls[int(nextx)][int(nexty)] : vis[x][y][direction].add((nextx, nexty)) nextx, nexty = x + dx, y + dy self.visibility = vis VISIBILITY_MATRIX_CACHE[reduce(str.__add__, self.layoutText)] = vis else: self.visibility = VISIBILITY_MATRIX_CACHE[reduce(str.__add__, self.layoutText)]
def __init__(self, layoutText): self.width = len(layoutText[0]) self.height= len(layoutText) self.walls = Grid(self.width, self.height, False) self.food = Grid(self.width, self.height, False) self.capsules = [] self.agentPositions = [] self.numGhosts = 0 self.processLayoutText(layoutText) self.layoutText = layoutText self.totalFood = len(self.food.asList()) # self.initializeVisibilityMatrix()
def halfGrid(grid, red): halfway = grid.width / 2 halfgrid = Grid(grid.width, grid.height, False) if red: xrange = range(halfway) else: xrange = range(halfway, grid.width) for y in range(grid.height): for x in xrange: if grid[x][y]: halfgrid[x][y] = True return halfgrid
def __init__(self, layoutText, numAgents = 2): self.width = len(layoutText[0]) self.height = len(layoutText) self.obstacles = Grid(self.width, self.height, False) self.agentPositions = [] self.numPursuers = 0 self.numAgents = numAgents self.processLayoutText(layoutText) self.layoutText = layoutText
def __init__(self, layoutText): self.width = len(layoutText[0]) self.height= len(layoutText) self.walls = Grid(self.width, self.height, False) self.food = Grid(self.width, self.height, False) self.capsules = [] self.agentPositions = [] self.numGhosts = 0 self.processLayoutText(layoutText) self.layoutText = layoutText # self.initializeVisibilityMatrix()
def cornersHeuristic(state, problem): """ A heuristic for the CornersProblem that you defined. state: The current search state (a data structure you chose in your search problem) problem: The CornersProblem instance for this layout. This function should always return a number that is a lower bound on the shortest path from the state to a goal of the problem; i.e. it should be admissible (as well as consistent). """ corners = problem.corners # These are the corner coordinates walls = problem.walls # These are the walls of the maze, as a Grid (game.py) "*** YOUR CODE HERE ***" def euclid(x, y): return ((x[0]-y[0])**2 + (x[1]-y[1])**2)**0.5 def manhattan(x, y): return (abs(x[0]-y[0]) + abs(x[1]-y[1])) current_position = state[:2] corners_state = state[2:] corners_not_seen = [corners[i] for i in range(4) if corners_state[i]==0] #print('start', state[:2]) #print('corners not seen', corners_not_seen) val = 0 # heuristic value to return for i in range(len(corners_not_seen)): d_euclid = [[c, manhattan(current_position, c)] for c in corners_not_seen] d_euclid.sort(key=lambda x: x[1]) #print d_euclid[0][1] val += d_euclid[0][1] # move to the closest corner current_position = d_euclid[0][0] # update the current position to the new corner position corners_not_seen.remove(d_euclid[0][0]) # we have seen this corner now #print('corners not seen at end', corners_not_seen) #print('dist to goal', val) return val