Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using static System.Console;
- using System.Threading;
- //using Nlog;
- namespace GameOfLife
- {
- class Program
- {
- const int Dead = 0; // Using a grid of 0's and 1's will help us count
- const int Alive = 1; // count neighbors efficiently in the Life program.
- static int GridSizeX = 25;
- static int GridSizeY = 25;
- static void Main(string[] args)
- {
- WriteLine("Conway's Game of Life:");
- WriteLine("=====================================================");
- int gridCount = 0;
- int[,] grid = new int[GridSizeX, GridSizeY];
- bool isSilentMode = false;
- bool isRMode = false;
- if (args.Length > 0)
- {
- if (args[0].ToLower() == "silent")
- {
- isSilentMode = true;
- WriteLine("Silent Mode");
- }
- }
- if (args.Length > 1)
- {
- if (args[1].ToLower() == "r")
- {
- isRMode = true;
- rGen(grid);
- WriteLine("Starting with R-Pentonimo");
- }
- }
- bool done = false;
- //Show Default Grid if not r mode enable
- if (isRMode == false)
- {
- defaultGen(grid);
- }
- while (!done)
- {
- // Clear Previous Console
- Console.Clear();
- // Display the grid (and its statistics)
- WriteLine($"Grid #{gridCount}");
- PrintGrid(grid);
- WriteLine($"Grid #{gridCount} aliveCount: {CountLiveCells(grid)}");
- Thread.Sleep(500);
- // Check to see if the user pressed a key
- if (Console.KeyAvailable)
- {
- ConsoleKey key = Console.ReadKey(true).Key;
- if (key == ConsoleKey.Q)
- done = true;
- }
- // Randomly Fill the Grid
- CalcNextGen(grid);
- gridCount++; // Increment at bottom of loop so that first grid displayed is Grid #0
- }
- }
- static void FillGridRandomly(int[,] grid, int fillPercentage)
- {
- for (int x=0; x < GridSizeX; x++)
- {
- for (int y=0; y < GridSizeY; y++)
- {
- if (RandomBool(fillPercentage) == true)
- grid[x,y] = Alive;
- else
- grid[x,y] = Dead;
- }
- }
- }
- static bool RandomBool(int percent)
- {
- Random rng = new Random();
- return (rng.Next() % 100 < percent);
- }
- static void PrintGrid(int[,] grid)
- {
- WriteLine($"+{Dashes(GridSizeX*3)}+");
- for (int y=0; y < GridSizeY; y++)
- {
- string s = "|";
- for (int x=0; x < GridSizeX; x++)
- {
- string cell = (grid[x,y] == Alive) ? " * " : " ";
- s += cell;
- }
- s += "|";
- WriteLine(s);
- }
- WriteLine($"+{Dashes(GridSizeX*3)}+");
- }
- static string Dashes(int number)
- {
- return new string('-', number);
- }
- static int CountLiveCells(int[,] grid)
- {
- int count = 0;
- for (int x=0; x < GridSizeX; x++)
- for (int y=0; y < GridSizeY; y++)
- if (grid[x,y] == Alive)
- count++;
- return count;
- }
- static void defaultGen(int[,] currentGrid)
- {
- currentGrid[13,13] = Alive;
- currentGrid[13+1,13+1] = Alive;
- currentGrid[13+1,13-1] = Alive;
- currentGrid[13-1,13-1] = Alive;
- currentGrid[13-1,13+1] = Alive;
- }
- static void rGen(int[,] currentGrid)
- {
- currentGrid[14, 12] = Alive;
- currentGrid[13, 12] = Alive;
- currentGrid[13, 13] = Alive;
- currentGrid[12, 13] = Alive;
- currentGrid[13, 14] = Alive;
- }
- static void CalcNextGen(int[,] currentGrid)
- {
- int[,] newGrid = new int[GridSizeX, GridSizeY];
- for (int y = 1; y < GridSizeY - 1; y++)
- {
- for (int x = 1; x < GridSizeX - 1; x++)
- {
- int numNeighbor =
- currentGrid[x - 1, y - 1] + currentGrid[x, y - 1] + currentGrid[x + 1, y - 1] +
- currentGrid[x - 1, y] + currentGrid[x + 1, y] +
- currentGrid[x - 1, y + 1] + currentGrid[x, y + 1] + currentGrid[x + 1, y + 1];
- if (currentGrid[x, y] == Alive)
- {
- if (numNeighbor == 2 || numNeighbor == 3)
- {
- newGrid[x, y] = Alive;
- }
- else
- {
- newGrid[x, y] = Dead;
- }
- }
- else
- {
- if (numNeighbor == 3)
- {
- newGrid[x, y] = Alive;
- }
- else
- {
- newGrid[x, y] = Dead;
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement