/// <summary> /// Resets a GameList Progress generation according to neighbours /// </summary> /// <param name="game"> Particular game in the list </param> public void NewGeneration(GameProgress game) { bool[,] newGeneration = new bool[game.Rows, game.Columns]; game.LiveCells = 0; // Sifts through each cell, checking it's neighbours. for (int rowIndex = 0; rowIndex < game.Rows; rowIndex++) { for (int colIndex = 0; colIndex < game.Columns; colIndex++) { // Checks for neighbours of the cell. NeigbourCounter(rowIndex, colIndex, game); // Checks if cell is alive. if (Neighbours == 3 || (Neighbours == 2 && game.Generation[rowIndex, colIndex])) { newGeneration[rowIndex, colIndex] = true; game.LiveCells++; gameList.CellsAlive++; } else { newGeneration[rowIndex, colIndex] = false; } } } var x = JsonConvert.SerializeObject(newGeneration); var y = JsonConvert.SerializeObject(game.Generation); if (x == y) { game.IsGameAlive = false; } game.Iteration++; game.Generation = newGeneration; }
/// <summary> /// Algorythm to check the neighbours of a particular cell. /// </summary> /// <param name="game"> Which particular game in the List GameList Progress to check. </param> public void NeigbourCounter(int rowIndex, int colIndex, GameProgress game) { Neighbours = 0; // Loops through a 3x3 square with an array cell set in center. for (int Row = -1; Row < 2; Row++) { for (int Col = -1; Col < 2; Col++) { // Checks if the neighbour is within array bounds. if ((rowIndex + Row > -1) && (colIndex + Col > -1) && (Row + rowIndex < game.Rows) && (colIndex + Col < game.Columns)) { if ((Row == 0) && (Col == 0)) { // Do nothing. } else if (game.Generation[rowIndex + Row, colIndex + Col]) { Neighbours++; } } } } }