// Change how long each tree can burn for before burning out public static void FireBurnTime() { string temp = string.Empty; // Return value is the current value if no change is made. int newBurnTime = Cell.GetBurnTime(); Console.Clear(); Console.WriteLine("Currently, the fires takes " + Cell.GetBurnTime() + " ticks to burn out"); Console.WriteLine("Input the new burn time >>"); bool inputIsValid = false; while (inputIsValid == false) { temp = Console.ReadLine(); try { newBurnTime = int.Parse(temp); inputIsValid = true; if (newBurnTime < 1) { Console.WriteLine("Error: Please enter a positive integer >>"); inputIsValid = false; } } catch (FormatException) { Console.WriteLine("Error: Please enter a positive integer >>"); } } Cell.SetBurnTime(newBurnTime); }
// This method handles the logic for the simulation from start to finish // Detailed in-line comments outline the sections of this method public void Burn() { // Instantiate cells cells = new Cell[numColumns, numRows]; for (int i = 0; i < numColumns; i++) { for (int j = 0; j < numRows; j++) { cells[i, j] = new Cell(i, j, this); } } // Reset grid and burn statistics and print the starting grid fireDuration = 0; biggestFire = 0; this.Reset(); CentreCell().Ignite(); Print(); // Logic for burning the grid // As long as there are cells still burning... while (burningCells.Any()) { // Update the burn statistics fireDuration++; if (biggestFire < burningCells.Count) { biggestFire = burningCells.Count; } // Spread the fire to the neighbouring trees of each burning cell foreach (Cell cell in burningCells) { // cell.timeburned++; cell.SetTimeBurned(cell.GetTimeBurned() + 1); SpreadFire(cell); // If the cell has burned for too long, it will burn out later in this tick if (cell.GetTimeBurned() >= Cell.GetBurnTime()) { cellsToDie.Add(cell); } } // Any burning cells that have burned for too long burn out foreach (Cell cell in cellsToDie) { cell.Die(); } cellsToDie.Clear(); // Each tree next to a burning cell has a chance to start burning foreach (Cell cell in cellsToBurn) { cell.AttemptIgnition(rng); } cellsToBurn.Clear(); // Wait for user input. Placing this check after the logic is calculated // should marginally increase responsiveness Console.ReadKey(); Print(); } }