Skip to content

Instantly share code, notes, and snippets.

@suicide
Created February 5, 2015 00:20
Show Gist options
  • Save suicide/dddd9ebab124c0be23f7 to your computer and use it in GitHub Desktop.
Save suicide/dddd9ebab124c0be23f7 to your computer and use it in GitHub Desktop.
Game of Life
/**
*
*/
/**
* @author psy
*
*/
public class Solution {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/**
* row count of the grid
*/
private int maxRow;
/**
* col count of the grid
*/
private int maxCol;
/**
* @return the maxRow
*/
public int getMaxRow() {
return maxRow;
}
/**
* @param maxRow
* the maxRow to set
*/
public void setMaxRow(int maxRow) {
this.maxRow = maxRow;
}
/**
* @return the maxCol
*/
public int getMaxCol() {
return maxCol;
}
/**
* @param maxCol
* the maxCol to set
*/
public void setMaxCol(int maxCol) {
this.maxCol = maxCol;
}
/**
*
* Updates the grid by the rules of the game by 1 iteration
*
* @param oldGrid
* @return updated grid status
*/
protected Cell[][] updateGrid(Cell[][] oldGrid) {
Cell[][] newGrid = new Cell[maxRow][maxCol];
// iterate through grid and update each cell status to the new grid
for (int row = 0; row < maxRow; row++) {
for (int col = 0; col < maxCol; col++) {
newGrid[row][col] = updateCell(row, col, oldGrid);
}
}
return newGrid;
}
/**
* updates a Cell by looking at its neighbors and applying the game rules
*
* @param currentRow
* @param currentCol
* @param grid
* @return a cell
*/
protected Cell updateCell(int currentRow, int currentCol, Cell[][] grid) {
Cell thisCell = grid[currentRow][currentCol];
int livingNeighbors = 0;
// iterate over neighbors
// +2 because max* variables are from .length
for (int row = Math.max(0, currentRow - 1); row < Math.min(currentRow + 2, maxRow); row++) {
for (int col = Math.max(0, currentCol - 1); col < Math.min(currentCol + 2, maxCol); col++) {
// do not check current cell
if (row == currentRow && col == currentCol) {
continue;
}
if (grid[row][col].isAlive) {
livingNeighbors++;
}
}
}
return liveOrDie(thisCell, livingNeighbors);
}
/**
* checks the rules and decides whether the cell lives or dies depending on
* the number of living neighbors
*
* @param cell
* @param livingNeighbors
* @return true if alive, false if dead
*/
protected Cell liveOrDie(Cell cell, int livingNeighbors) {
if (!cell.isAlive) {
// dead cell
if (livingNeighbors == 3) {
return Cell.ALIVE;
} else {
return Cell.DEAD;
}
} else {
// living cell
if (livingNeighbors == 2 || livingNeighbors == 3) {
return Cell.ALIVE;
} else {
return Cell.DEAD;
}
}
}
/**
* a Cell
*
* @author psy
*
*/
protected static enum Cell {
ALIVE("*", true), DEAD(".", false);
/**
* string representation
*/
private final String symbol;
/**
* status of liveliness
*/
private final boolean isAlive;
/**
* constructor
*
* @param symbol
*/
private Cell(String symbol, boolean isAlive) {
this.symbol = symbol;
this.isAlive = isAlive;
}
@Override
public String toString() {
return symbol;
}
/**
* returns a Cell instance by its symbol
*
* @param symbol
* @return a Cell or null of the symbol was not found
*/
public static Cell getBySymbol(String symbol) {
for (Cell cell : Cell.values()) {
if (cell.symbol.equals(symbol)) {
return cell;
}
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment