Ejemplo n.º 1
0
  public LinkedList<Integer> getAdjList(int i) {
    if (adjacencies.containsKey(i)) {
      return adjacencies.get(i);
    }

    LinkedList<Integer> neighbors = new LinkedList<Integer>();
    int column = i % numColumns;
    int row = i / numColumns;
    BoardCell adj;
    BoardCell cell = getCellAt(i);

    adjacencies.put(i, neighbors);

    if (cell.isRoom()) {
      // only midget doorways have adjacencies, and they only have one arm, so short-circuit

      // if a door was placed facing an edge, this would be invalid, but the board should not be set
      // up that way
      if (cell.isDoorway()) {
        switch (((RoomCell) cell).getDoorDirection()) {
          case RIGHT:
            neighbors.add(i + 1);
            break;
          case LEFT:
            neighbors.add(i - 1);
            break;
          case UP:
            neighbors.add(i - numColumns);
            break;
          case DOWN:
            neighbors.add(i + numColumns);
            break;
        }
      }
      return neighbors;
    }

    if (column > 0) {
      adj = getCellAt(i - 1); // the cell to the left

      if (adj.isWalkway()
          || (adj.isDoorway() && ((RoomCell) adj).getDoorDirection() == DoorDirection.RIGHT)) {
        neighbors.add(i - 1);
      }
    }

    if (column < numColumns - 1) {
      adj = getCellAt(i + 1); // the cell to the right

      if (adj.isWalkway()
          || (adj.isDoorway() && ((RoomCell) adj).getDoorDirection() == DoorDirection.LEFT)) {
        neighbors.add(i + 1);
      }
    }

    if (row > 0) {
      adj = getCellAt(i - numColumns); // the cell above

      if (adj.isWalkway()
          || (adj.isDoorway() && ((RoomCell) adj).getDoorDirection() == DoorDirection.DOWN)) {
        neighbors.add(i - numColumns);
      }
    }

    if (row < numRows - 1) {
      adj = getCellAt(i + numColumns); // the cell below

      if (adj.isWalkway()
          || (adj.isDoorway() && ((RoomCell) adj).getDoorDirection() == DoorDirection.UP)) {
        neighbors.add(i + numColumns);
      }
    }

    return neighbors;
  }