|
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace GameOfLife
- {
- public partial class MainForm : Form
- {
- private Point mouseLocation;
-
- private bool[,] newCells;
- private bool[,] toggleCells;
- private bool[,] oldCells;
-
- private Random random;
-
- private int gridSize = 4;
-
- private int magicCells = 0;
-
- private double density = 0.45;
-
- public MainForm(Rectangle bounds)
- {
- InitializeComponent();
- random = new Random();
- Bounds = bounds;
- newCells = new bool[bounds.Width / gridSize, bounds.Height / gridSize];
- toggleCells = new bool[bounds.Width / gridSize, bounds.Height / gridSize];
- oldCells = new bool[bounds.Width / gridSize, bounds.Height / gridSize];
-
- for (var x = 0; x < newCells.GetLength(0); x++) {
- for (var y = 0; y < newCells.GetLength(1); y++) {
- newCells[x, y] = random.NextDouble() <= density;
- oldCells[x, y] = false;
- toggleCells[x, y] = true;
- }
- }
- }
-
- private void MainForm_Load(object sender, EventArgs e)
- {
- Cursor.Hide();
- TopMost = true;
-
- timer.Interval = 50;
- timer.Tick += new EventHandler(timer_Tick);
- timer.Start();
- }
-
- private void MainForm_MouseMove(object sender, MouseEventArgs e)
- {
- if (!mouseLocation.IsEmpty)
- {
- // Terminate if mouse is moved a significant distance
- if (Math.Abs(mouseLocation.X - e.X) > 5 ||
- Math.Abs(mouseLocation.Y - e.Y) > 5)
- Application.Exit();
- }
-
- // Update current mouse location
- mouseLocation = e.Location;
-
- }
-
- private void MainForm_MouseClick(object sender, MouseEventArgs e)
- {
- Application.Exit();
- }
-
- private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
- {
- Application.Exit();
- }
-
- private int GetAliveNeighbors(bool[,] cells, int x, int y) {
- int prevX = x == cells.GetLowerBound(0) ? cells.GetUpperBound(0) : x - 1;
- int nextX = x == cells.GetUpperBound(0) ? cells.GetLowerBound(0) : x + 1;
- int prevY = y == cells.GetLowerBound(1) ? cells.GetUpperBound(1) : y - 1;
- int nextY = y == cells.GetUpperBound(1) ? cells.GetLowerBound(1) : y + 1;
-
- int aliveNeighbors = 0;
- if (cells[prevX, prevY]) {
- aliveNeighbors += 1;
- }
- if (cells[x, prevY])
- {
- aliveNeighbors += 1;
- }
- if (cells[nextX, prevY])
- {
- aliveNeighbors += 1;
- }
- if (cells[prevX, y])
- {
- aliveNeighbors += 1;
- }
- if (cells[nextX, y])
- {
- aliveNeighbors += 1;
- }
- if (cells[prevX, nextY])
- {
- aliveNeighbors += 1;
- }
- if (cells[x, nextY])
- {
- aliveNeighbors += 1;
- }
- if (cells[nextX, nextY])
- {
- aliveNeighbors += 1;
- }
- return aliveNeighbors;
- }
-
- private void timer_Tick(object sender, EventArgs e) {
- for (var x = 0; x < newCells.GetLength(0); x++)
- {
- for (var y = 0; y < newCells.GetLength(1); y++)
- {
- oldCells[x, y] = newCells[x, y];
- }
- }
-
- for (var x = 0; x < newCells.GetLength(0); x++)
- {
- for (var y = 0; y < newCells.GetLength(1); y++)
- {
- int neighbors = GetAliveNeighbors(oldCells, x, y);
- bool shouldLive = false;
- if (oldCells[x, y] && neighbors == 2)
- {
- shouldLive = true;
- } else if (!oldCells[x, y] && neighbors == 3) {
- shouldLive = true;
- }
-
- newCells[x, y] = shouldLive;
- toggleCells[x, y] = oldCells[x, y] == newCells[x, y];
- }
- }
-
- for (var i = 0; i < magicCells; i++) {
- int magicX = random.Next(0, newCells.GetUpperBound(0));
- int magicY = random.Next(0, newCells.GetUpperBound(1));
-
- newCells[magicX, magicY] = true;
- toggleCells[magicX, magicY] = oldCells[magicX, magicY] == newCells[magicX, magicY];
- }
-
- Redraw(CreateGraphics());
- }
-
- private void Redraw(Graphics graphics) {
- graphics.FillRectangle(Brushes.Black, Bounds);
- for (var x = 0; x < newCells.GetLength(0); x++)
- {
- for (var y = 0; y < newCells.GetLength(1); y++)
- {
- if (newCells[x, y])
- {
- graphics.FillRectangle(Brushes.White, x * gridSize, y * gridSize, gridSize, gridSize);
- }
- //else if (!newCells[x, y]) {
- // graphics.FillRectangle(Brushes.Black, x * gridSize, y * gridSize, gridSize, gridSize);
- //}
- }
- }
- }
-
- private void MainForm_Paint(object sender, PaintEventArgs e)
- {
- Redraw(e.Graphics);
- }
- }
- }
|