Skip to content

Instantly share code, notes, and snippets.

@donbing
Last active August 29, 2015 14:25
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 donbing/6720e90945084f7416a6 to your computer and use it in GitHub Desktop.
Save donbing/6720e90945084f7416a6 to your computer and use it in GitHub Desktop.
our 3rd go (has a nice menu and input cleaning, but not the quick exit stuff)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static List<List<int>> generateLiveCells()
{
var allCoordinates = new List<List<int>>();
var selectedOption = getSelectionFromMenu();
if (selectedOption.Key == ConsoleKey.D1)
{
randomStartingCellPositionsCreator(allCoordinates);
}
else if (selectedOption.Key == ConsoleKey.D2)
{
Console.Clear();
Console.WriteLine("Enter co ordinates of cells with X and Y values seperated by a comma, press s to finish!");
Console.WriteLine("Press o to return to Options");
var sNotPressed = true;
while (sNotPressed)
{
var inputKey = Console.ReadKey().KeyChar;
if (inputKey == 'o' || inputKey == 'O')
{
return generateLiveCells();
}
else if (inputKey == 's' || inputKey == 'S')
{
sNotPressed = false;
}
else
{
var inputValue = Console.ReadLine();
inputsParsedToPairs(allCoordinates, inputKey + inputValue);
}
}
}
Console.Clear();
return allCoordinates;
}
private static void inputsParsedToPairs(List<List<int>> allCoordinates, string inputValue)
{
var cleansedInput = new string(inputValue.Where(character => char.IsNumber(character) || character == ',').ToArray());
var commaSeperatedInput = cleansedInput.Split(',');
var xCoordinate = int.Parse(commaSeperatedInput[0]);
var yCoordinate = int.Parse(commaSeperatedInput[1]);
var coordinatePair = new List<int> { xCoordinate, yCoordinate };
allCoordinates.Add(coordinatePair);
}
private static void randomStartingCellPositionsCreator(List<List<int>> allCoordinates)
{
var random = new Random();
var numberOfLiveCells = random.Next(40, 50);
for (var count = 0; count <= numberOfLiveCells; count++)
{
var yCoordinate = random.Next(25);
var xCoordinate = random.Next(80);
var coordinatePair = new List<int> { xCoordinate, yCoordinate };
allCoordinates.Add(coordinatePair);
}
}
private static ConsoleKeyInfo getSelectionFromMenu()
{
Console.Clear();
var builder = new StringBuilder();
builder.AppendLine("Welcome to the Game of Life!")
.AppendLine("Options")
.AppendLine("Press 1 for a randomly generated game")
.AppendLine("Press 2 to enter live cells");
Console.WriteLine(builder);
var selectedOption = Console.ReadKey();
return selectedOption;
}
static void Main(string[] args)
{
Console.Clear();
foreach (var item in generateLiveCells())
{
Console.SetCursorPosition(item[0], item[1]);
Console.Write("X");
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment