Beispiel #1
0
  def registerInitialState(self, gameState):
    """
    This method handles the initial setup of the
    agent to populate useful fields (such as what team
    we're on).

    A distanceCalculator instance caches the maze distances
    between each pair of positions, so your agents can use:
    self.distancer.getDistance(p1, p2)

    IMPORTANT: This method may run for at most 15 seconds.
    """

    '''
    Make sure you do not delete the following line. If you would like to
    use Manhattan distances instead of maze distances in order to save
    on initialization time, please take a look at
    CaptureAgent.registerInitialState in captureAgents.py.
    '''
    CaptureAgent.registerInitialState(self, gameState)

    '''
    Your initialization code goes here, if you need any.
    '''

    # get 'centre' of home area
    _, height = getLayoutSize(gameState)
    xLo, xHi = getHomeArea(gameState, gameState.isOnRedTeam(self.index))
    validPositions = [(x, y) for x, y
      in product(range(xLo, xHi), range(height))
      if not gameState.hasWall(x, y)]
    def getEccentricity(pos):
      return max([self.distancer.getDistance(pos, p) for p in validPositions])
    self.centre = min(validPositions, key=getEccentricity)
    self.homeRange = (range(xLo, xHi), range(height))
Beispiel #2
0
def makeEnvPredicates(gameState, isRed, posRange=None):
  if not posRange:
    width, height = getLayoutSize(gameState)
    posRange = (range(width), range(height))
  xRange, yRange = posRange

  s = ""
  # adjcent
  for x in list(xRange)[:-1]:
    s += "(adjcent posx-{} posx-{})\n".format(x, x+1)
  s += "\n"

  for y in list(yRange)[:-1]:
    s += "(adjcent posy-{} posy-{})\n".format(y, y+1)
  s += "\n"

  # same
  for x in xRange:
    s += "(same posx-{} posx-{})\n".format(x, x)
  s += "\n"

  for y in yRange:
    s += "(same posy-{} posy-{})\n".format(y, y)
  s += "\n"

  # wall
  for x, y in product(xRange, yRange):
    if gameState.hasWall(x, y):
      s += "(wall-at posx-{} posy-{})\n".format(x, y)
  return s
Beispiel #3
0
def drawSight(gameState, selfPos, drawFunc):
  width, height = getLayoutSize(gameState)
  visiblePositions = [pos for pos in product(range(width), range(height))
    if abs(pos[0] - selfPos[0]) + abs(pos[1] - selfPos[1]) <= SIGHT_RANGE
  ]
  drawFunc(visiblePositions, [1, 0, 0], clear=True)
  drawFunc([selfPos], [0, 0, 1])
Beispiel #4
0
def makeObjSection(gameState, posRange=None):
  if not posRange:
    width, height = getLayoutSize(gameState)
    posRange = (range(width), range(height))
  xRange, yRange = posRange
  xobjs = ["posx-{}".format(x) for x in xRange]
  yobjs = ["posy-{}".format(y) for y in yRange]
  return "(:objects\n{}\n\n{}\n)".format(
    "\n".join(xobjs), "\n".join(yobjs))
Beispiel #5
0
def getFoodPositions(gameState, isRed):
  width, height = getLayoutSize(gameState)
  if isRed:
    food = gameState.getBlueFood()
  else:
    food = gameState.getRedFood()
  positions = []
  for x in range(width):
    for y in range(height):
      if food[x][y]:
        positions.append((x, y))
  return positions
Beispiel #6
0
def makePacmanInitSection(gameState, isRed, selfIdx, foodPositions, scaredIdx):
  s = "(:init\n"
  s += makeEnvPredicates(gameState, isRed)
  s += "\n"

  # home
  _, height = getLayoutSize(gameState)
  xLo, xHi = getHomeArea(gameState, isRed)
  for x in range(xLo, xHi):
    for y in range(height):
      s += "(is-home posx-{} posy-{})\n".format(x, y)
  s += "\n"
  
  # pacman
  pacx, pacy = gameState.getAgentPosition(selfIdx)
  s += "(pacman-at posx-{} posy-{})\n\n".format(pacx, pacy)

  # ghosts
  if isRed:
    ghostIndices = gameState.getBlueTeamIndices()
  else:
    ghostIndices = gameState.getRedTeamIndices()

  for i in ghostIndices:
    ghostPos = gameState.getAgentPosition(i)
    if ghostPos:
      ghx, ghy = ghostPos
      if i in scaredIdx:
        s += "(scared-ghost-at posx-{} posy-{})\n\n".format(ghx, ghy)
      else:
        s += "(ghost-at posx-{} posy-{})\n\n".format(ghx, ghy)

  # food
  for x, y in foodPositions:
    s += "(food-at posx-{} posy-{})\n".format(x, y)
  s += "\n"
  s += ")"
  return s