Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Last active April 1, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzdgn/39b5159cde0aeba04deb1a11280019f2 to your computer and use it in GitHub Desktop.
Save bzdgn/39b5159cde0aeba04deb1a11280019f2 to your computer and use it in GitHub Desktop.
A Simple Conway's Game Of Life Demo in Java
public class GameOfLifeDemo {
private static final int HEIGHT = 10;
private static final long PERIOD = 120*1;
public static void main(String[] args) throws InterruptedException {
boolean [][] matrix = new boolean[HEIGHT][HEIGHT];
// generateRandom(matrix); // random values matrix
// testGlider(matrix); // test for Glider
testTumbler(matrix); // test for Tumbler
while(true) {
Thread.sleep(PERIOD);
printMatrix(matrix);
processLife(matrix);
System.out.println("-----------------------------------------------------");
}
}
private static void processLife(boolean[][] matrix) {
boolean[][] tempMatrix = new boolean[matrix.length][matrix[0].length];
copyMatrix(matrix, tempMatrix);
// sweep the matrix
for(int i = 0; i < HEIGHT; i++) {
for(int j = 0; j < HEIGHT; j++) {
// count alive neighboors
int countAlive = 0;
for(int k = i-1; k <= i+1; k++) {
for(int t = j-1; t <= j+1; t++) {
if((k == i && t == j) || (t < 0 || t >= HEIGHT) || (k < 0 || k >= HEIGHT) )
continue;
else {
if(matrix[k][t]) {
countAlive++;
}
}
}
}
handleRules(tempMatrix, i, j, countAlive);
}
}
copyMatrix(tempMatrix, matrix);
}
// rules
// if cell have neighboors smaller than 1, die of loneliness
// if cell have neighboors greater than 4, die of overpopulation
// if only cell have 3 or 4 neighboors, live
private static void handleRules(boolean[][] matrix, int i, int j, int countAlive) {
if(countAlive <= 1 || countAlive >= 4)
matrix[i][j] = false;
else if(countAlive == 3 || countAlive == 4)
matrix[i][j] = true;
}
private static void copyMatrix(boolean[][] src, boolean[][] dst) {
for(int i = 0; i < HEIGHT; i++)
System.arraycopy(src[i], 0, dst[i], 0, HEIGHT);
}
private static void printMatrix(boolean[][] matrix) {
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length; j++) {
if(matrix[i][j] == true) {
System.out.print('X');
} else {
System.out.print(' ');
}
}
System.out.println();
}
}
private static void generateRandom(boolean[][] matrix) {
for(int i = 0; i < HEIGHT; i++)
for(int j = 0; j < HEIGHT; j++)
matrix[i][j] = Math.random() < 0.5;
}
/*
* Test Method for Generating a Glider
*
* X
* X
* XXX
*
*/
private static void testGlider(boolean[][] matrix) {
matrix[0][1] = true;
matrix[1][2] = true;
matrix[2][0] = true;
matrix[2][1] = true;
matrix[2][2] = true;
}
/*
* Test Method for Generating a Tumbler
*
* XX XX
* XX XX
* X X
* X X X X
* X X X X
* XX XX
*
*/
private static void testTumbler(boolean[][] matrix) {
matrix[0][2] = true;
matrix[0][3] = true;
matrix[0][5] = true;
matrix[0][6] = true;
matrix[1][2] = true;
matrix[1][3] = true;
matrix[1][5] = true;
matrix[1][6] = true;
matrix[2][3] = true;
matrix[2][5] = true;
matrix[3][1] = true;
matrix[3][3] = true;
matrix[3][5] = true;
matrix[3][7] = true;
matrix[4][1] = true;
matrix[4][3] = true;
matrix[4][5] = true;
matrix[4][7] = true;
matrix[5][1] = true;
matrix[5][2] = true;
matrix[5][6] = true;
matrix[5][7] = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment