Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. class CornersProblem(search.SearchProblem):
  2.     """
  3.   This search problem finds paths through all four corners of a layout.
  4.  
  5.   You must select a suitable state space and successor function
  6.   """
  7.  
  8.     def __init__(self, startingGameState):
  9.         self.visitedCorners=(0,0,0,0)
  10.         self.walls = startingGameState.getWalls()
  11.         self.startingPosition = startingGameState.getPacmanPosition()
  12.         top, right = self.walls.height-2, self.walls.width-2
  13.         self.corners = ((1,1), (1,top), (right, 1), (right, top))
  14.         for corner in self.corners:
  15.             if not startingGameState.hasFood(*corner):
  16.                 print 'Warning: no food in corner ' + str(corner)
  17.         self._expanded = 0 # DO NOT CHANGE; Number of search nodes expanded
  18.        
  19.     def getStartState(self):
  20.        
  21.         return (self.startingPosition,self.visitedCorners);
  22.        
  23.  
  24.     def isGoalState(self, state):
  25.        
  26.         position,visitedCorners=state
  27.         if visitedCorners==(1,1,1,1):
  28.             return 1
  29.        
  30.     def getSuccessors(self, state):
  31.        
  32.         successors = []
  33.         position,visitedCorners=state
  34.         x,y=position
  35.         for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
  36.             dx,dy=Actions.directionToVector(action)
  37.             nextx, nexty = int(x + dx), int(y + dy)
  38.             hitsWall = self.walls[nextx][nexty]
  39.             if not hitsWall :
  40.                 NextNode=(nextx, nexty)
  41.                 if NextNode in self.corners:
  42.                     a = list(visitedCorners)
  43.                     a[self.corners.index(NextNode)]=1
  44.                     visitedCorners = tuple(a)
  45.                 Nextcost=1
  46.                 successor=((NextNode,visitedCorners),action,Nextcost)
  47.                 successors.append(successor)
  48.         return successors
  49.  
  50.  
  51.     def getCostOfActions(self, actions):
  52.         """
  53.       Returns the cost of a particular sequence of actions.  If those actions
  54.       include an illegal move, return 999999.  This is implemented for you.
  55.       """
  56.         if actions == None: return 999999
  57.         x,y= self.startingPosition
  58.         for action in actions:
  59.             dx, dy = Actions.directionToVector(action)
  60.             x, y = int(x + dx), int(y + dy)
  61.             if self.walls[x][y]: return 999999
  62.         return len(actions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement