def replayGame(layout, actions, display): import pacmanAgents import ghostAgents rules = ClassicGameRules() agents = [pacmanAgents.GreedyAgent()] + [ ghostAgents.RandomGhost(i + 1) for i in range(layout.getNumGhosts()) ] game = rules.newGame(layout, agents[0], agents[1:], display) state = game.state display.initialize(state.data) for action in actions: # Execute the action state = state.getNextState(*action) # Change the display display.update(state.data) # Allow for game specific conditions (winning, losing, etc.) rules.process(state, game) display.finish()
def replayGame( layout, actions, display ): import pacmanAgents, ghostAgents import csv global GSTATE, PSA rules = ClassicGameRules() agents = [pacmanAgents.GreedyAgent()] + [ghostAgents.RandomGhost(i+1) for i in range(layout.getNumGhosts())] game = rules.newGame( layout, agents[0], agents[1:], display ) state = game.state display.initialize(state.data) c = 0 prevState = state for action in actions: # Execute the action state = state.generateSuccessor( *action ) # Change the display display.update( state.data ) # Allow for game specific conditions (winning, losing, etc.) rules.process(state, game) #after pacman moves, pause if c%2 == 0 : PSA[c/2] = (prevState.__str__(),action[1]) #print(prevState.__str__()) time.sleep(1) #record state we respond to prevState = state GSTATE += 1 #if flask, get here c+=1 display.finish()
def replayGame(layout, actions, display): import pacmanAgents, ghostAgents from graphicsDisplay import saveFrame rules = ClassicGameRules() agents = [pacmanAgents.GreedyAgent()] + [ ghostAgents.RandomGhost(i + 1) for i in range(layout.getNumGhosts()) ] game = rules.newGame(layout, agents[0], agents[1:], display) state = game.state display.initialize(state.data) for action in actions: # Execute the action state = state.generateSuccessor(*action) # Change the display display.update(state.data) # Allow for game specific conditions (winning, losing, etc.) rules.process(state, game) #save video (bruno) saveFrame() display.finish()
def buildExperience(self, layoutName, displayActive=False, limit=None): import pacmanAgents, ghostAgents from pacman import ClassicGameRules from game import Directions import layout theLayout = layout.getLayout(layoutName) if theLayout == None: raise Exception("The layout " + layoutName + " cannot be found") display = None # Choose a display format if not displayActive: import textDisplay display = textDisplay.NullGraphics() else: import graphicsDisplay display = graphicsDisplay.PacmanGraphics(frameTime=0.01) rules = ClassicGameRules() agents = [pacmanAgents.GreedyAgent()] + [ ghostAgents.DirectionalGhost(i + 1) for i in range(theLayout.getNumGhosts()) ] game = rules.newGame(theLayout, agents[0], agents[1:], display) initialState = game.state display.initialize(initialState.data) exploredStateHashes = {initialState.__hash__()} pendingStates = {initialState} counter = 0 while pendingStates: pendingState = pendingStates.pop() for action in pendingState.getLegalActions(): if action == Directions.STOP: continue try: # Execute the action newState = util.getSuccessor(agents, display, pendingState, action) reward = newState.data.score - pendingState.data.score self.remember(pendingState, action, reward, newState) counter += 1 if not (newState.isWin() or newState.isLose( )) and newState.__hash__() not in exploredStateHashes: exploredStateHashes.add(newState.__hash__()) pendingStates.add(newState) except Exception, e: #print(e) pass if counter % 100 == 0: self.persist() if counter % 2000 == 0: print("Explored " + str(counter) + " states") if limit is not None and counter > limit: break