Bunch of screensavers in Windows
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace GameOfLife
  11. {
  12. public partial class MainForm : Form
  13. {
  14. private Point mouseLocation;
  15. private bool[,] newCells;
  16. private bool[,] toggleCells;
  17. private bool[,] oldCells;
  18. private Random random;
  19. private int gridSize = 4;
  20. private int magicCells = 0;
  21. private double density = 0.45;
  22. public MainForm(Rectangle bounds)
  23. {
  24. InitializeComponent();
  25. random = new Random();
  26. Bounds = bounds;
  27. newCells = new bool[bounds.Width / gridSize, bounds.Height / gridSize];
  28. toggleCells = new bool[bounds.Width / gridSize, bounds.Height / gridSize];
  29. oldCells = new bool[bounds.Width / gridSize, bounds.Height / gridSize];
  30. for (var x = 0; x < newCells.GetLength(0); x++) {
  31. for (var y = 0; y < newCells.GetLength(1); y++) {
  32. newCells[x, y] = random.NextDouble() <= density;
  33. oldCells[x, y] = false;
  34. toggleCells[x, y] = true;
  35. }
  36. }
  37. }
  38. private void MainForm_Load(object sender, EventArgs e)
  39. {
  40. Cursor.Hide();
  41. TopMost = true;
  42. timer.Interval = 50;
  43. timer.Tick += new EventHandler(timer_Tick);
  44. timer.Start();
  45. }
  46. private void MainForm_MouseMove(object sender, MouseEventArgs e)
  47. {
  48. if (!mouseLocation.IsEmpty)
  49. {
  50. // Terminate if mouse is moved a significant distance
  51. if (Math.Abs(mouseLocation.X - e.X) > 5 ||
  52. Math.Abs(mouseLocation.Y - e.Y) > 5)
  53. Application.Exit();
  54. }
  55. // Update current mouse location
  56. mouseLocation = e.Location;
  57. }
  58. private void MainForm_MouseClick(object sender, MouseEventArgs e)
  59. {
  60. Application.Exit();
  61. }
  62. private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
  63. {
  64. Application.Exit();
  65. }
  66. private int GetAliveNeighbors(bool[,] cells, int x, int y) {
  67. int prevX = x == cells.GetLowerBound(0) ? cells.GetUpperBound(0) : x - 1;
  68. int nextX = x == cells.GetUpperBound(0) ? cells.GetLowerBound(0) : x + 1;
  69. int prevY = y == cells.GetLowerBound(1) ? cells.GetUpperBound(1) : y - 1;
  70. int nextY = y == cells.GetUpperBound(1) ? cells.GetLowerBound(1) : y + 1;
  71. int aliveNeighbors = 0;
  72. if (cells[prevX, prevY]) {
  73. aliveNeighbors += 1;
  74. }
  75. if (cells[x, prevY])
  76. {
  77. aliveNeighbors += 1;
  78. }
  79. if (cells[nextX, prevY])
  80. {
  81. aliveNeighbors += 1;
  82. }
  83. if (cells[prevX, y])
  84. {
  85. aliveNeighbors += 1;
  86. }
  87. if (cells[nextX, y])
  88. {
  89. aliveNeighbors += 1;
  90. }
  91. if (cells[prevX, nextY])
  92. {
  93. aliveNeighbors += 1;
  94. }
  95. if (cells[x, nextY])
  96. {
  97. aliveNeighbors += 1;
  98. }
  99. if (cells[nextX, nextY])
  100. {
  101. aliveNeighbors += 1;
  102. }
  103. return aliveNeighbors;
  104. }
  105. private void timer_Tick(object sender, EventArgs e) {
  106. for (var x = 0; x < newCells.GetLength(0); x++)
  107. {
  108. for (var y = 0; y < newCells.GetLength(1); y++)
  109. {
  110. oldCells[x, y] = newCells[x, y];
  111. }
  112. }
  113. for (var x = 0; x < newCells.GetLength(0); x++)
  114. {
  115. for (var y = 0; y < newCells.GetLength(1); y++)
  116. {
  117. int neighbors = GetAliveNeighbors(oldCells, x, y);
  118. bool shouldLive = false;
  119. if (oldCells[x, y] && neighbors == 2)
  120. {
  121. shouldLive = true;
  122. } else if (!oldCells[x, y] && neighbors == 3) {
  123. shouldLive = true;
  124. }
  125. newCells[x, y] = shouldLive;
  126. toggleCells[x, y] = oldCells[x, y] == newCells[x, y];
  127. }
  128. }
  129. for (var i = 0; i < magicCells; i++) {
  130. int magicX = random.Next(0, newCells.GetUpperBound(0));
  131. int magicY = random.Next(0, newCells.GetUpperBound(1));
  132. newCells[magicX, magicY] = true;
  133. toggleCells[magicX, magicY] = oldCells[magicX, magicY] == newCells[magicX, magicY];
  134. }
  135. Redraw(CreateGraphics());
  136. }
  137. private void Redraw(Graphics graphics) {
  138. graphics.FillRectangle(Brushes.Black, Bounds);
  139. for (var x = 0; x < newCells.GetLength(0); x++)
  140. {
  141. for (var y = 0; y < newCells.GetLength(1); y++)
  142. {
  143. if (newCells[x, y])
  144. {
  145. graphics.FillRectangle(Brushes.White, x * gridSize, y * gridSize, gridSize, gridSize);
  146. }
  147. //else if (!newCells[x, y]) {
  148. // graphics.FillRectangle(Brushes.Black, x * gridSize, y * gridSize, gridSize, gridSize);
  149. //}
  150. }
  151. }
  152. }
  153. private void MainForm_Paint(object sender, PaintEventArgs e)
  154. {
  155. Redraw(e.Graphics);
  156. }
  157. }
  158. }