Skip to content

Commit

Permalink
Added a menu under the grid with WriteCommands(), a singleton random …
Browse files Browse the repository at this point in the history
…cell state generator with the class RandomGenerator and a way to continously play until a button is pressed.
  • Loading branch information
alessdangelo committed Jul 9, 2021
1 parent a0d8f83 commit 23e3924
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 6 deletions.
70 changes: 64 additions & 6 deletions console_game_of_life/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//Author : alessdangelo
//Date : 07/07/2021
//Description : Quick console implementation of conway's game of life with borders
//ToDo: Add a menu under the game panel to tell which boutons can be used and some other informations
//ToDo: Add a method to generate random cell state
//ToDo: Add a button to play the game automaticaly and a button to stop (playing state is off)
//Modification date : 09/09/2021
//Modification desc : Added a menu under the grid with WriteCommands(), a random cell state generator and a way to continously play until a button is pressed.
using System;

namespace console_game_of_life
Expand Down Expand Up @@ -50,6 +49,17 @@ class Program
/// </summary>
private static bool startTurn = false;

/// <summary>
/// Allow to loop continously if backspace is pressed
/// </summary>
private static bool playContinuously = false;

/// <summary>
///To always generate the same sequence and validate the tests if needed
/// </summary>
private const int _SEED = 1;


static void Main(string[] args)
{
Console.SetWindowSize(_gridSize * 2, _gridSize + 5);
Expand All @@ -63,10 +73,14 @@ static void Main(string[] args)
Move();
if (startTurn)
{
SingleTurn();
WriteGrid();
do
{
SingleTurn();
WriteGrid();
} while (playContinuously && Console.KeyAvailable == false);
Console.SetCursorPosition(cursorPosX, cursorPosY);
startTurn = false;
playContinuously = false;
}
}
}
Expand Down Expand Up @@ -134,9 +148,38 @@ private static void Move()
Console.SetCursorPosition(cursorPosX, cursorPosY);
}
break;
//Start a single turn
case ConsoleKey.Enter:
startTurn = true;
break;
//Continously play until another button is pressed
case ConsoleKey.Backspace:
startTurn = true;
playContinuously = true;
break;
//Put random state (dead or alive) in a cell in the grid
case ConsoleKey.R:
for (int y = 0; y < _gridSize; y++)
{
for (int x = 0; x < _gridSize; x++)
{
int number = RandomGenerator.GetInstance(_SEED).Next(2);
_cellGrid[x, y] = number;
}
}
WriteGrid();
break;
//Reset the grid
case ConsoleKey.X:
for (int y = 0; y < _gridSize; y++)
{
for (int x = 0; x < _gridSize; x++)
{
_cellGrid[x, y] = 0;
}
}
WriteGrid();
break;
}
}

Expand All @@ -161,7 +204,8 @@ private static void GenerateGrid()
/// </summary>
private static void WriteGrid()
{
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.CursorVisible = false;
for (int y = 0; y < _gridSize; y++)
{
for (int x = 0; x < _gridSize; x++)
Expand All @@ -179,6 +223,20 @@ private static void WriteGrid()
}
Console.WriteLine();
}
WriteCommands();
Console.CursorVisible = true;
}


private static void WriteCommands()
{
Console.Write("[WASD] or [Arrow keys] to move \t");
Console.WriteLine("[Spacebar] Toggle Cell state");
Console.Write("[Enter] Start turn \t");
Console.WriteLine("[Backspace] Start until stopped");
Console.Write("[Any button] Stop\t");
Console.WriteLine("[R] Generate random cell state");
Console.Write("[X] Reset the grid \t");
}

/// <summary>
Expand Down
45 changes: 45 additions & 0 deletions console_game_of_life/RandomGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//Author : alessdangelo
//Date : 09/07/2021
//Description: Singleton class to generate a random number allowing to have only one instance of this class
using System;

namespace console_game_of_life
{
/// <summary>
/// Class generating a random number, using the singleton pattern
/// </summary>
public class RandomGenerator : Random
{
/// <summary>
/// Random object
/// </summary>
private Random _random;

/// <summary>
/// Current and only instance of RandomGenerator
/// </summary>
private static RandomGenerator _instance;

/// <summary>
/// Singleton, instanciate the random object
/// </summary>
private RandomGenerator(int seed)
{
_random = new Random(seed);
}

/// <summary>
/// Get current instance if exist, else instanciate it
/// </summary>
/// <returns>the singleton instance</returns>
public static RandomGenerator GetInstance(int seed)
{
if (_instance == null)
{
_instance = new RandomGenerator(seed);
return _instance;
}
return _instance;
}
}
}

0 comments on commit 23e3924

Please sign in to comment.