http://courses.cs.washington.edu/courses/cse473/14au/pacman/ps1/search.htmlhttp:
ID: 3570378 • Letter: H
Question
http://courses.cs.washington.edu/courses/cse473/14au/pacman/ps1/search.htmlhttp://courses.cs.washington.edu/courses/cse473/14au/pacman/ps1/search.html
Can one edit the search and search agents.py file in the above url mentioned
we can find SEARCH.ZIP in above url where we have to unzip that file and edit search and search agents to get that game
Process to do that is mentioned in that project
Explanation / Answer
""" Pacman.py holds the logic for the classic pacman game along with the main code to run a game. This file is divided into three sections: (i) Your interface to the pacman world: Pacman is a complex environment. You probably don't want to read through all of the code we wrote to make the game runs correctly. This section contains the parts of the code that you will need to understand in order to complete the project. There is also some code in game.py that you should understand. (ii) The hidden secrets of pacman: This section contains all of the logic code that the pacman environment uses to decide who can move where, who dies when things collide, etc. You shouldn't need to read this section of code, but you can if you want. (iii) Framework to start a game: The final section contains the code for reading the command you use to set up the game, then starting up a new game, along with linking in all the external parts (agent functions, graphics). Check this section out to see all the options available to you. To play your first game, type 'python pacman.py' from the command line. The keys are 'a', 's', 'd', and 'w' to move (or arrow keys). Have fun! """ from game import GameStateData from game import Game from game import Directions from game import Actions from util import nearestPoint from util import manhattanDistance import sys, util, types, time, random ################################################### # YOUR INTERFACE TO THE PACMAN WORLD: A GameState # ################################################### class GameState: """ A GameState specifies the full game state, including the food, capsules, agent configurations and score changes. GameStates are used by the Game object to capture the actual state of the game and can be used by agents to reason about the game. Much of the information in a GameState is stored in a GameStateData object. We strongly suggest that you access that data via the accessor methods below rather than referring to the GameStateData object directly. Note that in classic Pacman, Pacman is always agent 0. """ #################################################### # Accessor methods: use these to access state data # #################################################### def getLegalActions( self, agentIndex=0 ): """ Returns the legal actions for the agent specified. """ if self.isWin() or self.isLose(): return [] if agentIndex == 0: # Pacman is moving return PacmanRules.getLegalActions( self ) else: return GhostRules.getLegalActions( self, agentIndex ) def generateSuccessor( self, agentIndex, action): """ Returns the successor state after the specified agent takes the action. """ # Check that successors exist if self.isWin() or self.isLose(): raise Exception('Can't generate a successor of a terminal state.') # Copy current state state = GameState(self) # Let agent's logic deal with its action's effects on the board if agentIndex == 0: # Pacman is moving state.data._eaten = [False for i in range(state.getNumAgents())] PacmanRules.applyAction( state, action ) else: # A ghost is moving GhostRules.applyAction( state, action, agentIndex ) # Time passes if agentIndex == 0: state.data.scoreChange += -TIME_PENALTY # Penalty for waiting around else: GhostRules.decrementTimer( state.data.agentStates[agentIndex] ) # Resolve multi-agent effects GhostRules.checkDeath( state, agentIndex ) # Book keeping state.data._agentMoved = agentIndex state.data.score += state.data.scoreChange return state def getLegalPacmanActions( self ): return self.getLegalActions( 0 ) def generatePacmanSuccessor( self, action ): """ Generates the successor state after the specified pacman move """ return self.generateSuccessor( 0, action ) def getPacmanState( self ): """ Returns an AgentState object for pacman (in game.py) state.pos gives the current position state.direction gives the travel vector """ return self.data.agentStates[0].copy() def getPacmanPosition( self ): return self.data.agentStates[0].getPosition() def getGhostStates( self ): return self.data.agentStates[1:] def getGhostState( self, agentIndex ): if agentIndex == 0 or agentIndex >= self.getNumAgents(): raise "Invalid index passed to getGhostState" return self.data.agentStates[agentIndex] def getGhostPosition( self, agentIndex ): if agentIndex == 0: raise "Pacman's index passed to getGhostPosition" return self.data.agentStates[agentIndex].getPosition() def getNumAgents( self ): return len( self.data.agentStates ) def getScore( self ): return self.data.score def getCapsules(self): """ Returns a list of positions (x,y) of the remaining capsules. """ return self.data.capsules def getNumFood( self ): return self.data.food.count() def getFood(self): """ Returns a Grid of boolean food indicator variables. Grids can be accessed via list notation, so to check if there is food at (x,y), just call currentFood = state.getFood() if currentFood[x][y] == True: ... """ return self.data.food def getWalls(self): """ Returns a Grid of boolean wall indicator variables. Grids can be accessed via list notation, so to check if there is food at (x,y), just call walls = state.getWalls() if walls[x][y] == True: ... """ return self.data.layout.walls def hasFood(self, x, y): return self.data.food[x][y] def hasWall(self, x, y): return self.data.layout.walls[x][y] def isLose( self ): return self.data._lose def isWin( self ): return self.data._win ############################################# # Helper methods: # # You shouldn't need to call these directly # ############################################# def __init__( self, prevState = None ): """ Generates a new state by copying information from its predecessor. """ if prevState != None: # Initial state self.data = GameStateData(prevState.data) else: self.data = GameStateData() def deepCopy( self ): state = GameState( self ) state.data = self.data.deepCopy() return state def __eq__( self, other ): """ Allows two states to be compared. """ return self.data == other.data def __hash__( self ): """ Allows states to be keys of dictionaries. """ return hash( str( self ) ) def __str__( self ): return str(self.data) def initialize( self, layout, numGhostAgents=1000 ): """ Creates an initial game state from a layout array (see layout.py). """ self.data.initialize(layout, numGhostAgents) ############################################################################ # THE HIDDEN SECRETS OF PACMAN # # # # You shouldn't need to look through the code in this section of the file. # ############################################################################ SCARED_TIME = 40 # Moves ghosts are scared COLLISION_TOLERANCE = 0.7 # How close ghosts must be to Pacman to kill TIME_PENALTY = 1 # Number of points lost each round class ClassicGameRules: """ These game rules manage the control flow of a game, deciding when and how the game starts and ends. """ def newGame( self, layout, pacmanAgent, ghostAgents, display ): agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize( layout, len(ghostAgents) ) game = Game(agents, display, self) game.state = initState return game def process(self, state, game): """ Checks to see whether it is time to end the game. """ if state.isWin(): self.win(state, game) if state.isLose(): self.lose(state, game) def win( self, state, game ): print "Pacman emerges victorious! Score: %d" % state.data.score game.gameOver = True def lose( self, state, game ): print "Pacman died! Score: %d" % state.data.score game.gameOver = True class PacmanRules: """ These functions govern how pacman interacts with his environment under the classic game rules. """ PACMAN_SPEED=1 def getLegalActions( state ): """ Returns a list of possible actions. """ return Actions.getPossibleActions( state.getPacmanState().configuration, state.data.layout.walls ) getLegalActions = staticmethod( getLegalActions ) def applyAction( state, action ): """ Edits the state to reflect the results of the action. """ legal = PacmanRules.getLegalActions( state ) if action not in legal: raise "Illegal action", action pacmanState = state.data.agentStates[0] # Update Configuration vector = Actions.directionToVector( action, PacmanRules.PACMAN_SPEED ) pacmanState.configuration = pacmanState.configuration.generateSuccessor( vector ) # Eat next = pacmanState.configuration.getPosition() nearest = nearestPoint( next ) if manhattanDistance( nearest, next ) 1: scores = [game.state.getScore() for game in games] wins = [game.state.isWin() for game in games] print 'Average Score:', sum(scores) / float(numGames) print 'Scores: ', ', '.join([str(score) for score in scores]) print 'Win Rate: ', wins.count(True) / float(numGames) print 'Record: ', ', '.join([ ['Loss', 'Win'][int(w)] for w in wins]) return games if __name__ == '__main__': """ The main function called when pacman.py is run from the command line: > python pacman.py See the usage string for more details. > python pacman.py --help """ args = readCommand( sys.argv[1:] ) # Get game components based on input runGames( **args )Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.