private static GameInfo GetGameInfo(GridSize gridSize) { Console.WriteLine("Enter coordinates X,Y,N"); string userInput = Console.ReadLine(); string[] inputParts = userInput.Split(","); if (inputParts.Length != 3) { Console.WriteLine("Incorrect parameters"); return(null); } GameInfo gameInfo = new GameInfo(); for (int i = 0; i < inputParts.Length; i++) { int intValue; if (!int.TryParse(inputParts[i], out intValue)) { Console.WriteLine("Incorrect parameters " + inputParts[i]); return(null); } if (i == 1) { if (intValue > gridSize.Rows) { Console.WriteLine("Incorrect parameter " + intValue); return(null); } gameInfo.Point.X = intValue; } if (i == 0) { if (intValue > gridSize.Columns) { Console.WriteLine("Incorrect parameter " + intValue); return(null); } gameInfo.Point.Y = intValue; } if (i == 2) { gameInfo.N = intValue; } } return(gameInfo); }
private static GridSize GetGridSize() { Console.WriteLine("Enter rows and columns for the grid X,Y"); string userInput = Console.ReadLine(); string[] inputParts = userInput.Split(","); if (inputParts.Length != 2) { Console.WriteLine("Incorrect parameters"); return(null); } GridSize gameGridSize = new GridSize(); for (int i = 0; i < inputParts.Length; i++) { int intValue; if (!int.TryParse(inputParts[i], out intValue)) { Console.WriteLine("Incorrect parameters " + inputParts[i]); return(null); } if (intValue >= 1000) { Console.WriteLine("Incorrect parameters " + inputParts[i]); return(null); } if (i == 0) { gameGridSize.Rows = intValue; } if (i == 1) { gameGridSize.Columns = intValue; } } if (gameGridSize.Rows > gameGridSize.Columns) { Console.WriteLine("Rows cannot be more than columns"); return(null); } return(gameGridSize); }
private static Generation GetGenerationZero(GridSize gridSize) { Console.WriteLine("Enter Generation Zero"); Generation generationZero = new Generation(); for (int x = 0; x < gridSize.Rows; x++) { string userInput = Console.ReadLine(); if (userInput.Length != gridSize.Columns) { Console.WriteLine("Incorrect columns parts"); return(null); } for (int y = 0; y < userInput.Length; y++) { int intValue; if (!int.TryParse(userInput[y].ToString(), out intValue)) { Console.WriteLine("Incorrect column value " + userInput[y]); return(null); } if (intValue < 0 && intValue > 1) { Console.WriteLine("Incorrect column value " + userInput[y]); return(null); } GridPoint point = new GridPoint(); point.X = x; point.Y = y; point.Value = intValue; generationZero.Points.Add(point); } } return(generationZero); }
private static void PlayGame(GridSize gridSize, Generation generationZero, GameInfo gameInfo) { List <Generation> generations = new List <Generation>(); generations.Add(generationZero); Generation currentGeneration = generationZero; for (int i = 0; i < gameInfo.N; i++) { currentGeneration = GetNextGeneration(currentGeneration); generations.Add(currentGeneration); } int coutGreenPointsBetweenGenerations = generations .SelectMany(g => g.Points) .Where(p => p.X == gameInfo.Point.X && p.Y == gameInfo.Point.Y) .Count(p => p.Value == 1); Console.WriteLine("Result: " + coutGreenPointsBetweenGenerations); }