private void RandomPatternClick(object sender, EventArgs e) { LifeGrid.SetRandomPatternOnCells(); EnableControlsWhenStartGame(); gameTimer.Start(); CurrentTimerState = TimerState.On; }
private static void DisplayGrid(LifeGrid lifeGrid, char truechar, char falsechar) { foreach (string row in lifeGrid.GetRows(truechar, falsechar)) { Console.WriteLine(row); } }
static async Task Main(string[] args) { bool auto = false; if (args.Length > 0) { auto = args.Contains("-auto"); } int rows = 30; int columns = 79; var grid = new LifeGrid(rows, columns); grid.Randomize(); Console.Clear(); ShowGrid(grid.CurrentState); if (auto) { await RunAutoAdvance(grid); } else { await RunManualAdvance(grid); } }
public void TestInitialize() { grid = new LifeGrid(1000, 1000); var randomGridSeeder = new RandomGridSeeder(); randomGridSeeder.Seed(grid); }
static void Main(string[] args) { var grid = new LifeGrid(75, 25); int iterations = 10000; Console.WriteLine("Number of iterations: {0}", iterations); grid.Randomize(); var stopwatch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { grid.UpdateState(); } Console.WriteLine("Nested for: {0}ms", stopwatch.ElapsedMilliseconds); grid.Randomize(); stopwatch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { grid.UpdateState2(); } Console.WriteLine("Nested Parallel For: {0}ms", stopwatch.ElapsedMilliseconds); grid.Randomize(); stopwatch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { grid.UpdateState3(); } Console.WriteLine("Single Level Parallel For: {0}ms", stopwatch.ElapsedMilliseconds); Console.ReadLine(); }
private void StartSimulation() { lifeGrid = new LifeGrid(NumberOfRows, NumberOfColumns); var randomGridSeeder = new RandomGridSeeder(); randomGridSeeder.Seed(lifeGrid); timer.Start(); }
static void Main(string[] args) { var columns = 0; var rows = 0; var seeds = 0; var truechar = 'X'; var falsechar = '-'; if (args.Length > 0) { columns = Parse(args[0]); rows = Parse(args[1]); seeds = Parse(args[2]); } if (args.Length > 3) { truechar = args[3].ToCharArray()[0]; falsechar = args[4].ToCharArray()[0]; } if (columns <= 0 || rows <= 0 || seeds <= 0) { return; } var lifeGrid = new LifeGrid(columns, rows); //seed for (var i = seeds; i > 0; i--) { Random rnd = new Random(DateTime.Now.Millisecond); var x = rnd.Next(columns); var y = rnd.Next(rows); while (lifeGrid.GetCell(x, y)) { x = rnd.Next(columns); y = rnd.Next(rows); } lifeGrid.Seed(x, y); } DisplayGrid(lifeGrid, truechar, falsechar); while (true) { Console.WriteLine("Press Enter to generate next generation"); var line = Console.ReadLine(); if (line == "exit") { break; } lifeGrid.Generate(); DisplayGrid(lifeGrid, truechar, falsechar); } }
public static LifeGrid RunSimulationUntilGeneration(LifeGrid simulation, long generation) { while (simulation.Generation < generation) { simulation = simulation.NextLifeGridState(); Print(simulation); } return(simulation); }
public static async Task RunManualAdvance(LifeGrid grid) { while (Console.ReadKey().Key != ConsoleKey.Q) { await grid.ParallelForUpdateState(); ShowGrid(grid.CurrentState); } }
/// <summary> /// Initializes the game's state /// </summary> static void InitGame() { // Generate the grid MyGrid = new LifeGrid(20, 20); // Set the Alive cells (Cells are Dead by default) MyGrid.CurrentState[1, 2] = CellState.Alive; MyGrid.CurrentState[2, 2] = CellState.Alive; MyGrid.CurrentState[3, 2] = CellState.Alive; }
private void NextStepBtnClick(object sender, EventArgs e) { if (LifeGrid.IsGridRepeated()) { ClearGameWhenGameOver(); return; } LifeGrid.UpdateGridNextStep(); UpdateGameStep(); }
public static async Task RunAutoAdvance(LifeGrid grid) { while (true) { await Task.Delay(100); await grid.UpdateState(); ShowGrid(grid.CurrentState); } }
static async Task Main(string[] args) { var grid = new LifeGrid(75, 25); int iterations = 10000; Console.Clear(); Console.WriteLine($"Number of iterations: {iterations}"); grid.Randomize(); var stopWatch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { await grid.UpdateState(); } Console.WriteLine($"Nested for: {stopWatch.ElapsedMilliseconds}ms"); grid.Randomize(); stopWatch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { await grid.ParallelTaskUpdateState(); } Console.WriteLine($"Parallel Tasks: {stopWatch.ElapsedMilliseconds}ms"); grid.Randomize(); stopWatch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { await grid.ParallelForUpdateState(); } Console.WriteLine($"Parallel.For: {stopWatch.ElapsedMilliseconds}ms"); grid.Randomize(); stopWatch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { await grid.OveryParallelTaskUpdateState(); } Console.WriteLine($"Overly Parallel Tasks: {stopWatch.ElapsedMilliseconds}ms"); grid.Randomize(); stopWatch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { await grid.OverlyParallelForUpdateState(); } Console.WriteLine($"Overly Parallel.For: {stopWatch.ElapsedMilliseconds}ms"); Console.WriteLine("Complete"); }
static void Main(string[] args) { var grid = new LifeGrid(25, 70); grid.Randomize(); ShowGrid(grid.CurrentState); while (Console.ReadLine() != "q") { grid.UpdateState3(); ShowGrid(grid.CurrentState); } }
static void Main(string[] args) { var grid = new LifeGrid(25, 65); grid.Randomize(); ShowGrid(grid.CurrentState); while (true) { grid.UpdateState(); ShowGrid(grid.CurrentState); Thread.Sleep(500); } }
static void Main(string[] args) { var grid = new LifeGrid(NumberOfRows, NumberOfColumns); var randomGridSeeder = new RandomGridSeeder(); randomGridSeeder.Seed(grid); while (true) { grid.Tick(); var cells = grid.GetLivingCells(); DisplayCells(cells); Thread.Sleep(500); } }
static void Main(string[] args) { Console.WriteLine("Welcome to Game Of Life"); Console.WriteLine("Choose size of Field :"); int widthOfField = int.Parse(Console.ReadLine()); int heightOfField = int.Parse(Console.ReadLine()); var grid = new LifeGrid(widthOfField, heightOfField); grid.RandomizationOfField(); int iterations = 0; ShowGrid(grid.CurrentState); //Task task1 = new Task(() => // { // var iterateEachSecond = Console.ReadLine(); while (true /*Console.ReadLine() != "q"*/) { grid.UpdateState(); ShowGrid(grid.CurrentState); iterations++; //aliveCells.Sum(); //Console.WriteLine($"Alive cells {grid.SumOfAliveCells(widthOfField,heightOfField)}"); Console.WriteLine($"Iterations : {iterations}"); Thread.Sleep(2000); } //Console.WriteLine(" ================================="); //Console.WriteLine("Press press space to exit from game "); //Console.ReadKey(); // }); //task1.Start(); }
static void Main(string[] args) { var grid = new LifeGrid(25, 100); //grid.currentState[1, 2] = CellState.Alive; //grid.currentState[2, 2] = CellState.Alive; //grid.currentState[3, 2] = CellState.Alive; grid.Randomize(); ShowGrid(grid.currentState); while (Console.ReadLine() != "q") { grid.UpdateState(); ShowGrid(grid.currentState); } }
private void TimerTick(object sender, EventArgs e) { if (LifeGrid.IsGridRepeated()) { ClearGameWhenGameOver(); gameTimer.Stop(); return; } LifeGrid.UpdateGridNextStep(); UpdateGameStep(); if (CellsCount == 0) { ClearGameWhenGameOver(); gameTimer.Stop(); } }
private void GridClick(object sender, MouseEventArgs e) { if (LifeGrid != null) { switch (patternBox.SelectedIndex) { case 0: CurrentPattern = Patterns.OneCell; break; case 1: CurrentPattern = Patterns.Blinker; break; case 2: CurrentPattern = Patterns.Block; break; case 3: CurrentPattern = Patterns.Glider; break; case 4: CurrentPattern = Patterns.Python; break; case 5: CurrentPattern = Patterns.ZdrShip; break; default: CurrentPattern = Patterns.OneCell; break; } LifeGrid.DrawPatternOnCells(e.X, e.Y, CurrentPattern); ClearGrid(); DrawGrid(); } popLabel.Text = CellsCount.ToString(); }
public static void StartSimulationWithOptions(CliOptions options) { var rows = options.Rows ?? 30; var columns = options.Columns ?? 60; Console.SetWindowSize(Math.Max(Console.WindowWidth, columns + 2), Math.Max(Console.WindowHeight, rows + 4)); Console.CursorVisible = false; Console.Clear(); LifeGrid grid = LifeGrid.SeedRandomGrid(columns, rows); Print(grid); if (options.RunUtilGeneration.HasValue) { grid = RunSimulationUntilGeneration(grid, options.RunUtilGeneration.Value); } do { Print(grid, "Press 'q' to quit.\r\nPress 'g' to specify the next generation to stop at.\r\nPress 'r' to start a new simulation.\r\nAll other keys advance the simulation one generation."); var key = Console.ReadKey(); if (key.KeyChar == 'q') { return; } if (key.KeyChar == 'g') { Console.Write("\r\nHow many more generations to run for: "); var generationsInput = Console.ReadLine(); var generation = long.Parse(generationsInput); Console.Clear(); grid = RunSimulationUntilGeneration(grid, grid.Generation + generation); } if (key.KeyChar == 'r') { grid = LifeGrid.SeedRandomGrid(columns, rows); } }while(true); }
protected void backgroundWorker_DoWork(Object sender, DoWorkEventArgs e) { var grid = new LifeGrid(NumberOfRows, NumberOfColumns); var randomGridSeeder = new RandomGridSeeder(); randomGridSeeder.Seed(grid); while (true) { if (backgroundWorker.CancellationPending) { e.Cancel = true; break; } grid.Tick(); var cells = grid.GetLivingCells(); backgroundWorker.ReportProgress(0, cells); Thread.Sleep(TickDelay); } }
public static void Print(LifeGrid lifeGrid, string message = "") { for (int row = 0; row < lifeGrid.Rows; row++) { for (int column = 0; column < lifeGrid.Columns; column++) { char write = '.'; Console.SetCursorPosition(column, row); var cellState = lifeGrid.Grid[column, row]; if (cellState == CellState.Alive) { write = 'O'; } Console.Write(write); } } Console.SetCursorPosition(0, lifeGrid.Rows + 2); Console.WriteLine("Generation: " + lifeGrid.Generation.ToString()); Console.WriteLine(message); }
public void HealthyCellLives() { Assert.True(LifeGrid.NextCellState(CellState.Alive, 2) == CellState.Alive); Assert.True(LifeGrid.NextCellState(CellState.Alive, 3) == CellState.Alive); }
private void PreviousStepBtnClick(object sender, EventArgs e) { LifeGrid.UpdateGridPreviousStep(); UpdateGameStep(); }
public MainWindow() { InitializeComponent(); int width = 32; int height = 32; for (int x = 0; x < width; x++) { mainGrid.ColumnDefinitions.Add(new ColumnDefinition()); } for (int y = 0; y < height; y++) { mainGrid.RowDefinitions.Add(new RowDefinition()); } lifeGrid = new LifeGrid(width, height); cells = new Border[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Border border = new Border(); border.Margin = new Thickness(1); border.Background = new SolidColorBrush(Colors.LightGray); border.Tag = new int[] { x, y }; cells[x, y] = border; border.MouseLeftButtonDown += (s, e) => { Border b = s as Border; int[] tag = b.Tag as int[]; bool currentStatus = lifeGrid.GetCell(tag[0], tag[1]); lifeGrid.SetCell(tag[0], tag[1], !currentStatus); cells[tag[0], tag[1]].Background = !currentStatus ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.LightGray); }; Grid.SetRow(border, x); Grid.SetColumn(border, y); mainGrid.Children.Add(border); } } lifeGrid.OnUpdate += (newGrid) => { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Dispatcher.Invoke(() => { cells[x, y].Background = newGrid.GetCell(x, y) ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.LightGray); }); } } }; autoTickThread = new Thread(() => { try { while (true) { if (autoTick) { lifeGrid.Update(); } int i = 250; Dispatcher.Invoke(() => { i = (int)sliderAutoTickRate.Value; }); Thread.Sleep(i); } } catch { } }); autoTickThread.Start(); }
public void ReproducingCellLives() { Assert.True(LifeGrid.NextCellState(CellState.Dead, 3) == CellState.Alive); }
public void DeadCellStaysDead() { Assert.True(LifeGrid.NextCellState(CellState.Dead, 0) == CellState.Dead); Assert.True(LifeGrid.NextCellState(CellState.Dead, 2) == CellState.Dead); Assert.True(LifeGrid.NextCellState(CellState.Dead, 4) == CellState.Dead); }
public void LonelyCellDies() { Assert.True(LifeGrid.NextCellState(CellState.Alive, 0) == CellState.Dead); }
public Conway( int gridWidth, int gridHeight ) { Grid = new LifeGrid ( gridWidth, gridHeight ); }
public void CrowdedCellDies() { Assert.True(LifeGrid.NextCellState(CellState.Alive, 4) == CellState.Dead); }