Skip to content

Commit

Permalink
Diagonal Cells
Browse files Browse the repository at this point in the history
  • Loading branch information
rr1952 committed Jul 28, 2017
1 parent 37d1d2a commit bfca276
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 39 deletions.
74 changes: 37 additions & 37 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/main/java/com/gc/demo/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public static boolean liveOrDie(boolean isLive, int liveNeighbors) {
return isLive;
}

// Cell neighbors are only considered to be directly north/east/south/west of a cell. Diagonals don't count.

public static boolean[][] advanceGeneration(boolean[][] grid) {
// a new clean boolean grid to keep track of our next generation
boolean[][] ice = new boolean[grid.length][grid[0].length];
Expand All @@ -16,12 +18,19 @@ public static boolean[][] advanceGeneration(boolean[][] grid) {
for (int i = 0; i < ice.length; i++) {
for(int j = 0; j < ice[i].length; j++) {

//four checks around the grid. First checks if on border, and if not, proceeds in that direction and fishes out boolean of adjacent cell.
// four checks around the grid. First checks if on border, and if not, proceeds in that direction and fishes out boolean of adjacent cell.
if(i != 0 && grid[i-1][j]) neighbors++;
if((i != grid.length -1) && grid[i+1][j]) neighbors++;
if(j != 0 && grid[i][j-1]) neighbors++;
if((j != grid[i].length - 1) && grid[i][j+1]) neighbors++;

// Although if you wanted to check diagonals as well, you could just do the other dimension checks as well.
///*
if(i != 0 && j != 0 && grid[i-1][j-1]) neighbors++;
if(i != 0 && (j != grid[i].length -1) && grid[i-1][j+1]) neighbors++;
if((i != grid.length -1) && j != 0 && grid[i+1][j-1]) neighbors++;
if((i != grid.length -1) && (j != grid[i].length -1) && grid[i+1][j+1]) neighbors++;
//*/
ice[i][j] = liveOrDie(grid[i][j], neighbors);
neighbors = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/gc/demo/CellTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void test5() {
// Tries out a 3x3 cell array. Hopefully nothing crashes.
@Test
public void test6() {
boolean [][] allAlive = {{true,true,false}, {true,false,true}, {false, true,true}};
boolean [][] allAlive = {{true,true,true, true}, {true,true,true,true}, {true, true,true,true}, {true,true,true,true}};
allAlive = advanceGeneration(allAlive);

assertFalse(Arrays.deepToString(allAlive), true);
Expand Down
Binary file modified target/classes/com/gc/demo/Cell.class
Binary file not shown.
Binary file modified target/test-classes/com/gc/demo/CellTest.class
Binary file not shown.

0 comments on commit bfca276

Please sign in to comment.