Beispiel #1
0
  private static List<Cell> retrieveNeighbouringCells(
      Cell[][] cells, Cell cell, Creature creature) {
    List<Cell> neighbours = new ArrayList<Cell>();

    int x = cell.getXCoor();
    int y = cell.getYCoor();

    // left
    if (x > 1) {
      // up
      if (y > 1) {
        neighbours.add(cells[x - 1][y - 1]);
        neighbours.add(cells[x][y - 1]);
      }

      // middle
      neighbours.add(cells[x - 1][y]);

      // down
      if (y + 1 < cells[0].length) {
        neighbours.add(cells[x - 1][y + 1]);
        neighbours.add(cells[x][y + 1]);
      }
    }

    // right
    if (x + 1 < cells.length) {
      // up
      if (y > 1) {
        neighbours.add(cells[x + 1][y - 1]);
      }

      // middle
      neighbours.add(cells[x + 1][y]);

      // down
      if (y + 1 < cells[0].length) {
        neighbours.add(cells[x + 1][y + 1]);
      }
    }

    List<Cell> finalNeighbours = new ArrayList<Cell>();

    for (Cell neighbour : neighbours) {
      if (neighbour.creatureAllowed(creature) && neighbour.getBase() == null) {
        finalNeighbours.add(neighbour);
      }
    }

    return finalNeighbours;
  }