def __init__(self, ghostAgents, display, rules, layout, percentRandomize=0.5): Game.__init__(self, ghostAgents, display, rules) initState = GameState() initState.initialize(layout, len(ghostAgents), percentRandomize) self.state = initState self.initialState = initState.deepCopy()
def newGame(self, layout, pacmanAgent, ghostAgents, display, quiet=False, catchExceptions=False): from pacman import GameState agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize(layout, len(ghostAgents)) game = GameExtended(agents, display, self, catchExceptions=catchExceptions) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet return game
def getMovement(layoutText, agentIndex, difficulty): gameState = GameState() numGhosts = layoutText.count('G') layoutText = layoutText.split('\n') layoutText = filter(None, layoutText) layout = Layout(layoutText) gameState.initialize(layout, numGhosts) if difficulty == 1: agent = ReflexAgent() agent.index = agentIndex direction = agent.getAction(gameState) elif difficulty == 2: agent = MinimaxAgent() agent.index = agentIndex direction = agent.getAction(gameState) elif difficulty == 3: agent = AlphaBetaAgent() agent.index = agentIndex agent.depth = 3 direction = agent.getAction(gameState) else: agent = RandomAgent() agent.index = agentIndex direction = agent.getAction(gameState) if direction == 'North': return 1 elif direction == 'South': return 2 elif direction == 'West': return 3 else: return 4
def generateGameState(args): layout = generateLayout(args) gameState = GameState() numGhostAgents = 1 gameState.initialize(layout, numGhostAgents) return gameState
def generateAllStates( length, ghostNum=1): #length of all possible spaces. Do not set the ghost num allStatesWithoutP = [] for k in range(0, 4**length): layout = util.base10toN(k, 4, length) allStatesWithoutP.append(layout) allValidStates = [] for k in allStatesWithoutP: zerocount = 0 for x in range(0, len(k)): if k[x] == "0": zerocount += 1 if zerocount == (ghostNum + 1): allValidStates.append(k) allLayouts = [] for k in allValidStates: #hardcoded for only ONE GHOST!! tempstring1 = "" tempstring2 = "" switcher = True for x in range(0, len(k)): if k[x] == "0": if switcher: tempstring1 += "4" tempstring2 += "5" else: tempstring1 += "5" tempstring2 += "4" switcher = False else: tempstring1 += k[x] tempstring2 += k[x] allLayouts.append(tempstring1) allLayouts.append(tempstring2) for k in range(0, len(allLayouts)): state = allLayouts[k] newstate = "%" for x in range(0, len(state)): if state[x] == "1": newstate += " " elif state[x] == "2": newstate += "." elif state[x] == "3": newstate += "o" elif state[x] == "4": newstate += "P" elif state[x] == "5": newstate += "G" newstate += "%" layouttext = [] layouttext.append("%" * (length + 2)) #HARDCODE layouttext.append(newstate) layouttext.append("%" * (length + 2)) #HARDCODE allLayouts[k] = layouttext #print layouttext allStates = [] for k in range(0, len(allLayouts)): layout = Layout(allLayouts[k]) gameState = GameState() gameState.initialize(layout, 1) #ghost hardcoded allStates.append(gameState) return allStates
def initPolicy(self, game, agentIndex=0): """Interactively allow user to choose an MDP policy.""" from keyboardAgents import KeyboardAgent from graphicsUtils import keys_pressed from searchAgents import searchGen import itertools agent = game.agents[agentIndex] def pollChoice(state): blink_show = True blink_delay = 3 blink_tick = 0 self.update(state.data) action = None while not action: action = agent.getAction(state) time.sleep(0.1) if blink_tick == blink_delay - 1: blink_show = not blink_show self.update(state.data, blink_show) blink_tick = (blink_tick + 1) % blink_delay return action wallsDict = game.state.getWalls().asValDict() availablePos = set(wallsDict[False]) foodDict = game.state.getFood().asPosDict() delKeys = [] # Eliminate entries that refer to locations without food for k, v in foodDict.items(): if v in [0, False]: delKeys.append(k) for k in delKeys: del foodDict[k] minStates = [] for r in range(len(foodDict.items()) + 1): for foodState in itertools.combinations(foodDict.items(), r): # import pdb; pdb.set_trace() print(foodState) occupiedByFood = set() for pos, _ in foodState: occupiedByFood.add(pos) for pos in availablePos - occupiedByFood: minStates.append((pos, frozenset(foodState))) policy = dict() for s in minStates: state = GameState() state.data = game.state.data.deepCopy( ) # Passing data into __init__ makes # shallow copy instead of deep. state.data.agentStates[0].configuration.pos = s[0] state.data.food = Grid(state.data.food.width, state.data.food.height, 0) for (x, y), foodVal in s[1]: state.data.food[x][y] = foodVal print(s) policy[s] = pollChoice(state) self.blinkOn = False return policy