Skip to content

Instantly share code, notes, and snippets.

@dpedro
Created January 18, 2018 12:32
Show Gist options
  • Save dpedro/02162ba68ba611a3dcf20e9ea6826f5e to your computer and use it in GitHub Desktop.
Save dpedro/02162ba68ba611a3dcf20e9ea6826f5e to your computer and use it in GitHub Desktop.
private int[,] grid;
public static int DEAD_CELL = 0;
public static int LIVING_CELL = 1;
private int rowCount;
private int columnCount;
public GameOfLife(int rowCount, int columnCount)
{
grid = new int[rowCount, columnCount];
this.rowCount = grid.GetLength(0);
this.columnCount = grid.GetLength(1);
InitializeGridWithDeadCells();
}
public void InitializeGridWithDeadCells()
{
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int j = 0; j < grid.GetLength(1); j++)
{
grid[i, j] = DEAD_CELL;
System.Console.WriteLine(string.Format("{0}\t", grid[i, j]));
}
System.Console.WriteLine("<br/>");
}
}
public void SetLivingCell(int row, int column)
{
grid[row, column] = LIVING_CELL;
}
public int countlivingNeighbours(int row, int column)
{
int[,] cellsToCheck = {
{row - 1, column - 1},
{row - 1, column},
{row - 1, column + 1},
{row, column + 1},
{row + 1, column + 1},
{row + 1, column},
{row + 1, column - 1},
{row, column - 1},
};
int livingNeighbours = 0;
for (int i = 0; i < cellsToCheck.GetLength(0); i++)
{
int rowToCheck = cellsToCheck[i,0];
int colTocheck = cellsToCheck[i,1];
if (isInTheGrid(rowToCheck, colTocheck) && isAlive(rowToCheck, colTocheck))
{
livingNeighbours++;
}
}
return livingNeighbours;
}
private Boolean isInTheGrid(int row, int col)
{
return row >= 0 && col >= 0 && row < rowCount && col < columnCount;
}
public Boolean isAlive(int row, int column)
{
return grid[row, column] == LIVING_CELL;
}
public Boolean isDead(int row, int column)
{
return grid[row, column] == DEAD_CELL;
}
public void computeNextGeneration()
{
int[,] nextGenerationGrid = new int[rowCount, columnCount];
for (int y = 0; y < rowCount; y++)
{
for (int x = 0; x < columnCount; x++)
{
if (thisCellIsAliveAndHasLessThanTwoLivingNeighbours(y, x))
{
nextGenerationGrid[y, x] = DEAD_CELL;
}
else if (thisCellIsAliveAndHasTwoOrThreeLivingNeighbours(y, x))
{
nextGenerationGrid[y, x] = LIVING_CELL;
}
else if (thisCellIsAliveAndHasMoreThanThreeLivingNeighbours(y, x))
{
nextGenerationGrid[y, x] = DEAD_CELL;
}
else if (thisCellIsDeadAndHasThreeLivingNeighbours(y, x))
{
nextGenerationGrid[y, x] = LIVING_CELL;
}
else
{
nextGenerationGrid[y, x] = grid[y, x];
}
}
}
grid = nextGenerationGrid.Clone();
}
private Boolean thisCellIsDeadAndHasThreeLivingNeighbours(int row, int column)
{
int livingNeighbours = countlivingNeighbours(row, column);
return isDead(row, column) && livingNeighbours == 3;
}
private Boolean thisCellIsAliveAndHasMoreThanThreeLivingNeighbours(int row, int column)
{
int livingNeighbours = countlivingNeighbours(row, column);
return isAlive(row, column) && livingNeighbours > 3;
}
private Boolean thisCellIsAliveAndHasTwoOrThreeLivingNeighbours(int row, int column)
{
int livingNeighbours = countlivingNeighbours(row, column);
return isAlive(row, column) && (livingNeighbours == 2 || livingNeighbours == 3);
}
private Boolean thisCellIsAliveAndHasLessThanTwoLivingNeighbours(int row, int column)
{
int neighboursCount = countlivingNeighbours(row, column);
return isAlive(row, column) && neighboursCount < 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment