GameOfLife { public void nextCycle() throws GameOfLifeException { emptyMatrix(auxMatrix, MAX_X, MAX_Y); for(int i = 0; i < MAX_X; i++) { for(int j = 0; j < MAX_Y; j++) { if(matrix[i][j] == 1) { if(i >= MAX_X - 2 || j >= MAX_Y - 2) { throw new GameOfLifeException(CauseMessage.GRID_OVERFLOW, states.size(), "Current state can't be evolved further with a " + MAX_X + "x" + MAX_Y + " grid."); } auxMatrix[i + 1][j + 1] = 1; } } } for(int i = 0; i < MAX_X; i++) { for(int j = 0; j < MAX_Y; j++) { matrix[i][j] = auxMatrix[i][j]; auxMatrix[i][j] = 0; } } for(int i = 0; i < MAX_X; i++) { for(int j = 0; j < MAX_Y; j++) { int nNeighbors = 0; if(i > 0 && matrix[i - 1][j] == 1) { nNeighbors++; } if(i > 0 && j > 0 && matrix[i - 1][j - 1] == 1) { nNeighbors++; } if(i > 0 && j < MAX_Y - 1 && matrix[i - 1][j + 1] == 1) { nNeighbors++; } if(j > 0 && matrix[i][j - 1] == 1) { nNeighbors++; } if(j < MAX_Y - 1 && matrix[i][j + 1] == 1) { nNeighbors++; } if(i < MAX_X - 1 && j > 0 && matrix[i + 1][j - 1] == 1) { nNeighbors++; } if(i < MAX_X - 1 && matrix[i + 1][j] == 1) { nNeighbors++; } if(i < MAX_X - 1 && j < MAX_Y - 1 && matrix[i + 1][j + 1] == 1) { nNeighbors++; } if(matrix[i][j] == 1) { if(nNeighbors < 2 || nNeighbors > 3) { auxMatrix[i][j] = 0; } else { auxMatrix[i][j] = 1; } } else { if(nNeighbors == 3) { auxMatrix[i][j] = 1; } else { auxMatrix[i][j] = 0; } } } } int nAlives = 0; for(int i = 0; i < MAX_X; i++) { for(int j = 0; j < MAX_Y; j++) { matrix[i][j] = auxMatrix[i][j]; if(matrix[i][j] == 1) { nAlives++; } } } if(nAlives == 0) { throw new GameOfLifeException(CauseMessage.NO_ALIVE_CELLS, states.size(), "No alive cells."); } emptyMatrix(auxMatrix, MAX_X, MAX_Y); adjustUpLeft(matrix, auxMatrix, MAX_X, MAX_Y); saveCurrentState(); } GameOfLife(int width, byte[] initialStateInBytes, int maxX, int maxY, int maxIterations); static void emptyMatrix(byte[][] matrix, int maxX, int maxY); static void printMatrix(byte[][] matrix, int maxX, int maxY, PrintStream stream); void nextCycle(); static void main(String[] args);  }
@Test public void testNextCycle() throws GameOfLifeException { byte[] bytes = new byte[] { -52, 51 }; GameOfLife gameOfLife = new GameOfLife(4, bytes, 8, 8, 10); assertEquals(1, gameOfLife.getMatrix()[0][0]); assertEquals(1, gameOfLife.getMatrix()[0][1]); assertEquals(0, gameOfLife.getMatrix()[0][2]); assertEquals(0, gameOfLife.getMatrix()[0][3]); assertEquals(1, gameOfLife.getMatrix()[1][0]); assertEquals(1, gameOfLife.getMatrix()[1][1]); assertEquals(0, gameOfLife.getMatrix()[1][2]); assertEquals(0, gameOfLife.getMatrix()[1][3]); assertEquals(0, gameOfLife.getMatrix()[2][0]); assertEquals(0, gameOfLife.getMatrix()[2][1]); assertEquals(1, gameOfLife.getMatrix()[2][2]); assertEquals(1, gameOfLife.getMatrix()[2][3]); assertEquals(0, gameOfLife.getMatrix()[3][0]); assertEquals(0, gameOfLife.getMatrix()[3][1]); assertEquals(1, gameOfLife.getMatrix()[3][2]); assertEquals(1, gameOfLife.getMatrix()[3][3]); gameOfLife.nextCycle(); assertEquals(1, gameOfLife.getMatrix()[0][0]); assertEquals(1, gameOfLife.getMatrix()[0][1]); assertEquals(0, gameOfLife.getMatrix()[0][2]); assertEquals(0, gameOfLife.getMatrix()[0][3]); assertEquals(1, gameOfLife.getMatrix()[1][0]); assertEquals(0, gameOfLife.getMatrix()[1][1]); assertEquals(0, gameOfLife.getMatrix()[1][2]); assertEquals(0, gameOfLife.getMatrix()[1][3]); assertEquals(0, gameOfLife.getMatrix()[2][0]); assertEquals(0, gameOfLife.getMatrix()[2][1]); assertEquals(0, gameOfLife.getMatrix()[2][2]); assertEquals(1, gameOfLife.getMatrix()[2][3]); assertEquals(0, gameOfLife.getMatrix()[3][0]); assertEquals(0, gameOfLife.getMatrix()[3][1]); assertEquals(1, gameOfLife.getMatrix()[3][2]); assertEquals(1, gameOfLife.getMatrix()[3][3]); try { gameOfLife.nextCycle(); } catch(GameOfLifeException e) { assertTrue(e.getMessage().toLowerCase().contains("convergence reached")); } assertEquals(1, gameOfLife.getMatrix()[0][0]); assertEquals(1, gameOfLife.getMatrix()[0][1]); assertEquals(0, gameOfLife.getMatrix()[0][2]); assertEquals(0, gameOfLife.getMatrix()[0][3]); assertEquals(1, gameOfLife.getMatrix()[1][0]); assertEquals(1, gameOfLife.getMatrix()[1][1]); assertEquals(0, gameOfLife.getMatrix()[1][2]); assertEquals(0, gameOfLife.getMatrix()[1][3]); assertEquals(0, gameOfLife.getMatrix()[2][0]); assertEquals(0, gameOfLife.getMatrix()[2][1]); assertEquals(1, gameOfLife.getMatrix()[2][2]); assertEquals(1, gameOfLife.getMatrix()[2][3]); assertEquals(0, gameOfLife.getMatrix()[3][0]); assertEquals(0, gameOfLife.getMatrix()[3][1]); assertEquals(1, gameOfLife.getMatrix()[3][2]); assertEquals(1, gameOfLife.getMatrix()[3][3]); }