Esempio n. 1
0
  public void createNextGeneration() {

    StringBuffer nextGenerationContent = new StringBuffer();

    int maxRow = currentGridContent.getWidth();
    int maxColumn = currentGridContent.getHeight();
    // TODO: simplify this code
    for (int y = 0; y < maxRow; y++) {
      for (int x = 0; x < maxColumn; x++) {
        Cell currentCell = currentGridContent.getCellAt(x, y);
        int neighbourCount = currentGridContent.getLiveNeighboursAt(x, y);
        Cell nextCell = null;
        if (currentCell == Cell.LIVE_CELL) {
          if ((neighbourCount == 2) || (neighbourCount == 3)) {
            nextCell = LIVE_CELL;
          } else {
            nextCell = DEAD_CELL;
          }
        } else {
          if (neighbourCount == 3) {
            nextCell = LIVE_CELL;
          } else {
            nextCell = DEAD_CELL;
          }
        }
        nextGenerationContent.append(nextCell);
      }
      nextGenerationContent.append(NEW_LINE);
    }
    nextGenerationContent.append(NEW_LINE);
    currentGridContent = new Grid(nextGenerationContent.toString());
  }
Esempio n. 2
0
 public Cell getCellAt(final int row, final int column) {
   return currentGridContent.getCellAt(column, row);
 }
Esempio n. 3
0
 public Cell[][] getCells() {
   return currentGridContent.getContents();
 }
Esempio n. 4
0
 public String getGrid() {
   return currentGridContent.toString();
 }