def betterEvaluationFunction(currentGameState): """ Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable evaluation function (question 5). DESCRIPTION: <write something here so we know what you did> """ "*** YOUR CODE HERE ***" # util.raiseNotDefined() newPos = currentGameState.getPacmanPosition() newFood = currentGameState.getFood() newGhostStates = currentGameState.getGhostStates() newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates] "*** YOUR CODE HERE ***" currFood = currentGameState.getFood() Fooddist = [] for food in currFood.asList(): if manhattan( newPos, food ) == 0: # if next position is food then pursue it. Reciprocal will give high value Fooddist.append(1) else: Fooddist.append(manhattan( newPos, food)) # store distances to food in array for ghost in newGhostStates: if manhattan(newPos, ghost.getPosition() ) == 0: # Avoid if ghost is in the next position is ghost return -9999999 return currentGameState.getScore() + (1 / min(Fooddist)) + 1 / len( newScaredTimes) # maximize score and reduce distance to next food
def voronoi_ish(self, rects): nearest = defaultdict(set) for rect in rects: p = self.points[0] dist = util.manhattan(rect.center, p) for q in self.points[1:]: d = util.manhattan(rect.center, q) if d < dist: dist = d p = q nearest[p] |= set(self.get_rect(rect)) for (i, p) in enumerate(self.points): for q in self.points[i+1:]: self.borders += nearest[p] & nearest[q]
def __init__(self, seed_points, seed_rooms, space, num_rooms, paths=1): self.seed_points = seed_points self.seed_rooms = seed_rooms points = seed_points[:] points += [room.center for room in seed_rooms] self.n_seeds = len(points) list_space = list(space) # Pick points to draw buildings around while len(points) < num_rooms + len(seed_rooms): p = random.choice(list_space) points.append(p) for q in points[:-1]: if util.manhattan(p, q) < 4: points.remove(p) break self.space = space self.cells = [] self.interior = [] self.borders = [] #self.subdivide(boundary) self.points = points #self.voronoi_ish(self.get_wrecked(boundary)) self.buildings = self.make_buildings() self.edges = [(i,i+1) for i in range(len(points)-1)]
def evaluationFunction(self, currentGameState, action): """ Design a better evaluation function here. The evaluation function takes in the current and proposed successor GameStates (pacman.py) and returns a number, where higher numbers are better. The code below extracts some useful information from the state, like the remaining food (newFood) and Pacman position after moving (newPos). newScaredTimes holds the number of moves that each ghost will remain scared because of Pacman having eaten a power pellet. Print out these variables to see what you're getting, then combine them to create a masterful evaluation function. """ # Useful information you can extract from a GameState (pacman.py) successorGameState = currentGameState.generatePacmanSuccessor(action) newPos = successorGameState.getPacmanPosition() newFood = successorGameState.getFood() newGhostStates = successorGameState.getGhostStates() newScaredTimes = [ ghostState.scaredTimer for ghostState in newGhostStates ] "*** YOUR CODE HERE ***" currFood = currentGameState.getFood() Fooddist = [] for food in currFood.asList(): if Directions.STOP in action: # if action is stop then do not pursue it return high -ve value return -99999999 if manhattan( newPos, food ) == 0: # if next position is food then pursue it. Reciprocal will give high value Fooddist.append(1) else: Fooddist.append(manhattan( newPos, food)) # store distances to food in array for ghost in newGhostStates: if manhattan(newPos, ghost.getPosition( )) == 0: # Avoid if ghost is in the next position is ghost return -9999999 return currentGameState.getScore() + ( 1 / min(Fooddist) ) # maximize score and reduce distance to next food
def nearest_food(self, coord): if len(self.food) == 0: return None shortest = sys.maxsize for food in self.food: distance = manhattan(food, coord) if distance < shortest: shortest = distance nearest = food return nearest
def is_low_risk(self, coord): if self.board.get_weight(coord) >= Weight.LONG_SNAKE_HEAD.value: # long snake, high risk return False if len(self.board.safe_neighbors(coord)) > 0: # has exit strategy, low risk return True future_tail = self.my_body[-2] if self.my_health == 100: # tail is not going to move future_tail = self.my_tail if self.board.token(coord) != TokenType.FOOD and manhattan(coord, future_tail) <= 1: # tail will be safe exit, low risk return True # Coord has food and there is no exit strategy, high risk return False
def __init__(self, boundary, space, num_rooms, paths=1): points = [] list_space = list(space) # Pick points to draw buildings around while len(points) < num_rooms: p = random.choice(list_space) for q in points: if util.manhattan(p, q) < 3: continue points.append(p) self.space = space self.cells = [] self.interior = [] self.borders = [] #self.subdivide(boundary) self.points = points #self.voronoi_ish(self.get_wrecked(boundary)) self.borders = self.make_buildings() self.edges = [(i,i+1) for i in range(len(points)-1)]
def distance(self): return manhattan(self.x1, self.y1, self.x2, self.y2)
def move(self, direction): self.steps += 1 self.stepsSinceApple += 1 oldManhattanDistance = util.manhattan(self.cells[0], self.game.applePos, self.game.width) nextPos = self.cells[0] # get head if direction == 24 and (self.length == 1 or self.direction != 25): #up nextPos -= self.game.width self.direction = 24 if nextPos < 0: self.game.over = True return -1 elif direction == 25 and (self.length == 1 or self.direction != 24): #down nextPos += self.game.width self.direction = 25 if nextPos//self.game.width >= self.game.height: self.game.over = True return -1 elif direction == 26 and (self.length == 1 or self.direction != 27): #right nextPos += 1 self.direction = 26 if nextPos%self.game.width == 0: self.game.over = True return -1 elif direction == 27 and (self.length == 1 or self.direction != 26): #left nextPos -= 1 self.direction = 27 if nextPos%self.game.width == self.game.width-1: self.game.over = True return -1 else: return self.move(self.direction) if self.game.board[nextPos] == Game.SNAKE: self.game.over = True return -1 self.cells.appendleft(nextPos) newManhattanDistance = util.manhattan(self.cells[0], self.game.applePos, self.game.width) if newManhattanDistance > oldManhattanDistance: self.stepsAway += 1 if self.deferGrow >= 1: self.deferGrow -= 1 self.length += 1 # check if we ate apple if self.game.board[nextPos] == Game.APPLE: self.grow() newApple = random.randint(0, self.game.width*self.game.height-1) while(self.game.board[newApple] != Game.EMPTY): newApple = random.randint(0, self.game.width*self.game.height-1) self.game.applePos = newApple self.game.score += 100 self.stepsSinceApple = 0 # update board self.game.board = [Game.EMPTY for _ in range(self.game.width*self.game.height)] for i in range(self.length): self.game.board[self.cells[i]] = Game.SNAKE self.game.board[self.game.applePos] = Game.APPLE return 0
def heuristic(coord, goal): return manhattan(coord, goal)
def distance_to_ride_start(self, ride): return manhattan(self.x, self.y, ride.x1, ride.y1)
def distance_to(self, x, y): return manhattan(self.x, self.y, x, y)