def chooseAction(self, gameState: GameState) -> str: """ Picks among legal actions randomly. """ actions = gameState.getLegalActions(self.index) return random.choice(actions)
def chooseAction(self, gameState: GameState) -> str: capMultiplier = 3 if self.red: capsules = gameState.getRedCapsules() else: capsules = gameState.getBlueCapsules() foodList = list(self.getFood(gameState)) actions = gameState.getLegalActions(self.index) meanX = 0 meanY = 0 for i in range(len(foodList)): meanX = meanX + foodList[i][0] meanY = meanY + foodList[i][1] for cap in capsules: meanX = capMultiplier * meanX + cap[0] meanY = capMultiplier * meanY + cap[1] meanX = int(meanX / len(foodList)) meanY = int(meanY / len(foodList)) myState = gameState.getAgentState(self.index) myPos = myState.getPosition() curDistance = abs(myPos[0] - meanX) + abs(myPos[1] - meanY) '''if meanX > myPos[0]: if actions.__contains__('East'): return 'East' if meanX <= myPos[0] if actions.__contains__('West'): return 'West' if meanY > myPos[1]: if actions.__contains__('East'): return 'East' if meanY <= myPos[1] if actions.__contains__('West'): return 'West' ''' for action in actions: gameState.generateSuccessor(self.index, action) newPos = myState.getPosition() dist = abs(newPos[0] - meanX) + abs(newPos[1] - meanY) if dist < curDistance: return action return random.choice(actions)
def chooseAction(self, gameState: GameState) -> str: actions = gameState.getLegalActions(self.index) return random.choice(actions)
def chooseAction(self, gameState: GameState) -> str: actions = gameState.getLegalActions(self.index) food = self.getOpponentFood(gameState).asList() myPos = gameState.getAgentPosition(self.index) if self.target is not None: self.target.dist = self.getMazeDistance( (self.target.pos.x, self.target.pos.y), myPos ) targets = [Target(Position(x, y), self.getMazeDistance( (x, y), myPos)) for x, y in food] target: Target = min(targets, key=lambda target: target.dist) if self.target is None or target.dist < self.target.dist or len(self.path) == 0 or self.path is None: self.target = target maze = Maze(gameState.getWalls()) path = [] pos = myPos while True: directions = [ (pos[0], pos[1] + 1, "North"), (pos[0], pos[1] - 1, "South"), (pos[0] + 1, pos[1], "East"), (pos[0] - 1, pos[1], "West") ] maze[pos[0]][pos[1]].visited = True possibleMoves = [d for d in directions if not maze[d[0]][d[1]].isWall and not maze[d[0]][d[1]].visited] # print("path", path) # print("pos", pos) # print("possibleMoves", possibleMoves) if len(possibleMoves) == 0: if len(path) == 0: print("failed") sys.exit(-1) lastMove = path.pop() if lastMove == "North": pos = (pos[0], pos[1] - 1) if lastMove == "South": pos = (pos[0], pos[1] + 1) if lastMove == "East": pos = (pos[0] - 1, pos[1]) if lastMove == "West": pos = (pos[0] + 1, pos[1]) continue d = possibleMoves[0] path.append(d[2]) maze[d[0]][d[1]].visited = True pos = (d[0], d[1]) # but did we win? if pos[0] == self.target.pos.x and pos[1] == self.target.pos.y: # print("yay") # print("path", path) self.path = path break if gameState.isOnRedTeam(self.index): print(self.target, self.path) move = self.path[0] self.path = self.path[1:] return move
def chooseAction(self, gameState: GameState) -> str: actions = gameState.getLegalActions(self.index) return Directions.STOP