Beispiel #1
0
 /**
  * Generates the Knight's moveset by looping clockwise. First take two steps in a certain
  * direction to create a baseLocation, and then go to the right and left of the baseLocation to
  * check for possible moves.
  */
 public ArrayList<Location> getValidMoveLocations() {
   Grid<Actor> gr = getGrid();
   if (gr == null) {
     return null;
   }
   ArrayList<Location> result = new ArrayList<Location>();
   Location possibleLocation1, possibleLocation2;
   for (int i = 0; i < Location.FULL_CIRCLE; i += Location.RIGHT) {
     Location baseLocation = getLocation();
     for (int counter = 0; counter < 2; counter++) {
       baseLocation = baseLocation.getAdjacentLocation(i);
     }
     possibleLocation1 = baseLocation.getAdjacentLocation(i + Location.LEFT);
     if (gr.isValid(possibleLocation1)) {
       result.add(possibleLocation1);
       if (gr.get(possibleLocation1) instanceof Piece
           && gr.get(possibleLocation1).getColor().equals(getColor())) {
         result.remove(possibleLocation1);
       }
     }
     possibleLocation2 = baseLocation.getAdjacentLocation(i + Location.RIGHT);
     if (gr.isValid(possibleLocation2)) {
       result.add(possibleLocation2);
       if (gr.get(possibleLocation2) instanceof Piece
           && gr.get(possibleLocation2).getColor().equals(getColor())) {
         result.remove(possibleLocation2);
       }
     }
   }
   return result;
 }
Beispiel #2
0
  @Test
  public void zoneTest() {
    // 1 1 2 2
    // 1 1 3 3
    // 2 2 2 2
    Grid<Integer> grid = new Grid<Integer>(4, 3, null);
    grid.set(0, 0, 1);
    grid.set(1, 0, 1);
    grid.set(2, 0, 2);
    grid.set(3, 0, 2);
    grid.set(0, 1, 1);
    grid.set(1, 1, 1);
    grid.set(2, 1, 3);
    grid.set(3, 1, 3);
    grid.set(0, 2, 2);
    grid.set(1, 2, 2);
    grid.set(2, 2, 2);
    grid.set(3, 2, 2);

    Grid<Boolean> zone = grid.zone(2, 0);
    assertTrue(zone.get(2, 0));
    assertTrue(zone.get(3, 0));
    assertEquals(2, zone.count(true));

    zone = grid.zone(0, 2);
    assertTrue(zone.get(0, 2));
    assertTrue(zone.get(1, 2));
    assertTrue(zone.get(2, 2));
    assertTrue(zone.get(3, 2));
    assertEquals(4, zone.count(true));
  }
Beispiel #3
0
  @Test
  public void gridTest() {
    Grid grid = new Grid(5, 4);

    assertEquals(Color.NONE, grid.get(4, 3).get());

    grid.set(4, 3, Color.BAD);
    assertEquals(Color.BAD, grid.get(4, 3).get());

    grid.set(4, 3, Color.GOOD);
    assertEquals(Color.GOOD, grid.get(4, 3).get());
  }
Beispiel #4
0
  @Test
  public void setTest() {
    Grid<Integer> grid = new Grid<Integer>(4, 2, null);

    grid.set(0, 0, 1);
    grid.set(1, 0, 4);
    grid.set(3, 0, 3);

    System.out.println(grid.toString());

    assertEquals(3, (int) grid.get(3, 0));
    assertNull(grid.get(2, 0));
  }
Beispiel #5
0
 public boolean canMove() {
   Grid<Actor> gr = getGrid();
   if (gr == null) return false;
   Location loc = getLocation();
   Location next = loc.getAdjacentLocation(getDirection()).getAdjacentLocation(getDirection());
   if (!gr.isValid(next)) return false;
   Actor neighbor = gr.get(next);
   return (neighbor == null) || (neighbor instanceof Flower);
 }
  public static QueryResultsRemoteClient getQueryResultRemoteClient(
      Grid grid, String nodeId, String sessionId, String queryName, String remoteResultsId) {

    GridServiceDescription<GridNode> gsd = getGridServiceDescriptor(grid, nodeId);
    GridNode node = getGridNode(nodeId, grid, false);
    String reverseId = node.get(sessionId, String.class);
    return new QueryResultsRemoteClient(
        queryName, reverseId, remoteResultsId, gsd, grid.get(ConversationManager.class));
  }
  public void act() {
    Grid<Actor> gr = getGrid();
    Location location = new Location(6, 0);
    Location rightLoc = new Location(6, 19);

    if (oldActor instanceof Cave33) {
      if (getLocation().equals(new Location(3, 15))) {
        removeSelfFromGrid();
        oldActor.putSelfInGrid(gr, new Location(3, 15));
        oldActor = gr.get(new Location(11, 10));
        putSelfInGrid(gr, new Location(11, 10));
        directionSuffix = "down";
      } else {
        if (getLocation().equals(new Location(10, 10))) {
          removeSelfFromGrid();
          oldActor.putSelfInGrid(gr, new Location(10, 10));
          oldActor = gr.get(new Location(4, 15));
          putSelfInGrid(gr, new Location(4, 15));
          directionSuffix = "down";
        }
      }
    }

    if (getLocation().equals(location)) {
      if ((world2 == true) && (switched == false)) {
        ((PokemonGrid) gr).fillWorld1();
        oldActor = gr.get(new Location(6, 19));
        putSelfInGrid(gr, new Location(6, 19));
        world2 = false;
        switched = true;
      }
    } else {
      if (getLocation().equals(rightLoc)) {
        if ((world2 == false) && (switched == false)) {
          ((PokemonGrid) gr).fillWorld2();
          oldActor = gr.get(new Location(6, 0));
          putSelfInGrid(gr, new Location(6, 0));
          world2 = true;
          switched = true;
        }
      } else switched = false;
    }
  }
 /**
  * Returns the maze distance between any two points, using the search functions you have already
  * built. The gameState can be any game state -- Pacman's position in that state is ignored.
  *
  * <p>Example usage: mazeDistance( (2,4), (5,6), gameState)
  *
  * <p>This might be a useful helper function for your ApproximateSearchAgent.
  */
 public int mazeDistance(
     final int x1, final int y1, final int x2, final int y2, final GameState1 gameState) {
   final Grid walls = gameState.getWalls();
   if (walls.get(x1, y1)) {
     throw new RuntimeException("point1 is a wall: " + Position.newInstance(x1, y1));
   }
   if (walls.get(x2, y2)) {
     throw new RuntimeException("point2 is a wall: " + Position.newInstance(x2, y2));
   }
   final PositionSearchProblem prob =
       new PositionSearchProblem(
           gameState,
           null,
           x2,
           y2,
           new GameStatePositionSearchProblem(gameState.getPacmanPosition()),
           false,
           false);
   return new BreadthFirstSearch().getActions(prob).size();
 }
 public void move() {
   Grid<Actor> gr = getGrid();
   if (gr == null) return;
   Location loc = getLocation();
   Location next = loc.getAdjacentLocation(getDirection());
   if (gr.isValid(next)) {
     nextActor = gr.get(next);
     moveTo(next);
     if (oldActor != null) oldActor.putSelfInGrid(gr, loc);
     oldActor = nextActor;
   } else removeSelfFromGrid();
 }
 public static KnowledgeAgent getKnowledgeAgentRemoteClient(
     Grid grid, String nodeId, String sessionId) {
   GridServiceDescription<GridNode> gsd = getGridServiceDescriptor(grid, nodeId);
   GridNode node = getGridNode(nodeId, grid, false);
   String reverseId = node.get(sessionId, String.class);
   if (logger.isDebugEnabled()) {
     logger.debug(
         " ### Grid Helper: Creating KnowledgeAgent Client for: reverseId: "
             + reverseId
             + " - session-id: "
             + sessionId);
   }
   return new KnowledgeAgentRemoteClient(reverseId, gsd, grid.get(ConversationManager.class));
 }
 public boolean canMove() {
   Grid<Actor> gr = getGrid();
   if (gr == null) return false;
   Location loc = getLocation();
   Location next = loc.getAdjacentLocation(getDirection());
   if (!gr.isValid(next)) return false;
   Actor neighbor = gr.get(next);
   return (neighbor == null)
       || (neighbor instanceof TallGrass)
       || (neighbor instanceof Green)
       || (neighbor instanceof Path)
       || (neighbor instanceof Cave43)
       || (neighbor instanceof Cave33);
   // not ok to move onto water or rocks
 }
  private static GridServiceDescription<GridNode> getGridServiceDescriptor(Grid grid, String name) {
    if (logger.isDebugEnabled()) {
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper trying to locate GridNode: "
              + name);
    }

    if (logger.isDebugEnabled()) {
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper Looking up: "
              + name);
    }
    GridServiceDescription<GridNode> nGsd = grid.get(WhitePages.class).lookup(name);
    return nGsd;
  }
 public void getOldActor(Grid gr, Location loc) {
   oldActor = (Actor) (gr.get(new Location(6, 10)));
 }
  public static synchronized GridNode getGridNode(String name, Grid grid, boolean forceRemote) {

    if (logger.isDebugEnabled()) {
      logger.debug(" ### Grid Helper trying to locate GridNode: " + name);
    }

    if (nodeCache.containsKey(name)) {
      logger.debug(" ### Grid Helper found node " + name + " in cache");
      return nodeCache.get(name);
    }

    GridServiceDescription<GridNode> nGsd = grid.get(WhitePages.class).lookup(name);

    if (nGsd == null) {
      if (logger.isDebugEnabled()) {
        logger.error(
            "("
                + Thread.currentThread().getId()
                + ")"
                + Thread.currentThread().getName()
                + " ### Grid Helper DOES NOT Found a Node Descriptor for: "
                + name);
      }
      return null;
    }
    if (logger.isDebugEnabled()) {

      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper Found Node Descriptor: "
              + nGsd);
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t id: "
              + nGsd.getId());
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t Address size: "
              + nGsd.getAddresses().size());
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t Addresses: "
              + nGsd.getAddresses());
      for (String key : nGsd.getAddresses().keySet()) {
        logger.debug(
            "("
                + Thread.currentThread().getId()
                + ")"
                + Thread.currentThread().getName()
                + " \t ### Address: "
                + nGsd.getAddresses().get(key));
      }

      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t Interface: "
              + nGsd.getServiceInterface());
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t DATA: "
              + nGsd.getData());
    }

    ConnectionFactoryService csf = grid.get(ConnectionFactoryService.class);
    boolean allowsLocal = csf.isLocalAllowed();
    csf.setLocalAllowed(!forceRemote);
    GridConnection<GridNode> conn = csf.createConnection(nGsd);
    csf.setLocalAllowed(allowsLocal);

    if (logger.isDebugEnabled()) {
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper Create a Conection: "
              + name);
    }
    GridNode node = conn.connect();

    if (logger.isDebugEnabled()) {
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper found GridNode: ("
              + name
              + ") -> "
              + node);
    }

    nodeCache.put(name, node);

    return node;
  }