fork download
  1. //ライフゲーム
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. using System.Threading.Tasks;
  12. using System.Threading;
  13.  
  14. namespace LifeGame
  15. {
  16. public partial class Form1 : Form
  17. {
  18.  
  19. Random rnd = new Random();
  20. public Bitmap bmp { get; set; }
  21. public Graphics g { get; set; }
  22. public int[,] lifes { get; set; }
  23. public int[,] next { get; set; }
  24. const int MX = 300;
  25. const int SZ = 3;
  26. Task taskMain;
  27. bool isUpdate = false;
  28.  
  29. IEnumerable<Point> xy = from x in Enumerable.Range(0, MX)
  30. from y in Enumerable.Range(0, MX)
  31. select new Point(x, y);
  32.  
  33. public Form1()
  34. {
  35. InitializeComponent();
  36.  
  37. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  38. SetStyle(ControlStyles.UserPaint, true);
  39. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  40.  
  41.  
  42. bmp = new Bitmap(MX * SZ, MX * SZ);
  43. this.ClientSize = bmp.Size;
  44. g = Graphics.FromImage(bmp);
  45. lifes = new int[MX, MX];
  46. next = new int[MX, MX];
  47. for (int i = 0; i < 20000; i++)
  48. {
  49. lifes[rnd.Next(MX), rnd.Next(MX)] = 1;
  50. }
  51. isUpdate = true;
  52. taskMain = Task.Factory.StartNew(GameMain);
  53.  
  54. }
  55.  
  56. private void GameMain()
  57. {
  58. MessageBox.Show("Start");
  59.  
  60. bool isContinue = true;
  61. while (isContinue)
  62. {
  63. Thread.Sleep(1000 / 60);
  64.  
  65. if (!isUpdate) continue;
  66. //次世代ライフをクリア
  67. next = new int[MX, MX];
  68.  
  69. //自分以外の周囲8のlifeの数を知る
  70. isContinue = false;
  71. for(int x = 0 ; x <MX ; x++)
  72. {
  73. for (int y = 0; y < MX; y++)
  74. {
  75. int roundLifes = GetRoundLifes(x,y);
  76. if ((lifes[x, y] == 0 && roundLifes == 3)
  77. ||
  78. (lifes[x, y] == 1 && (2 <= roundLifes && roundLifes <= 3)))
  79. {
  80. isContinue = true;
  81. next[x, y] = 1;
  82. }
  83. else
  84. {
  85. next[x, y] = 0;
  86. }
  87. }
  88. }
  89.  
  90. for (int x = 0; x < MX; x++)
  91. {
  92. for (int y = 0; y < MX; y++)
  93. {
  94. lifes[x, y] = next[x, y];
  95. }
  96. }
  97.  
  98. Invalidate();
  99. }
  100. MessageBox.Show("End");
  101.  
  102. }
  103.  
  104. private int GetRoundLifes(int x,int y)
  105. {
  106. int result = 0;
  107. Func<int, int, int> getLife = (x1, y1) =>
  108. {
  109. if ((x1 < 0) || (y1 < 0) || (x1 >= MX) || (y1 >= MX))
  110. {
  111. return 0;
  112. }
  113. return lifes[x1, y1];
  114. };
  115. result += getLife(x - 1, y - 1);
  116. result += getLife(x - 0, y - 1);
  117. result += getLife(x + 1, y - 1);
  118. result += getLife(x - 1, y - 0);
  119. result += getLife(x + 1, y - 0);
  120. result += getLife(x - 1, y + 1);
  121. result += getLife(x - 0, y + 1);
  122. result += getLife(x + 1, y + 1);
  123. return result;
  124.  
  125. }
  126.  
  127. protected override void OnPaint(PaintEventArgs e)
  128. {
  129. if (!isUpdate) return;
  130.  
  131. g.Clear(Color.Black);
  132.  
  133. for (int x = 0; x < MX; x++)
  134. {
  135. for (int y = 0; y < MX; y++)
  136. {
  137. if (lifes[x, y] == 1) g.DrawRectangle(Pens.Aqua, x * SZ, y *SZ, SZ, SZ);
  138. }
  139. }
  140. e.Graphics.DrawImage(bmp,Point.Empty);
  141. }
  142.  
  143. /// <summary>
  144. /// 押された場所に5x5のライフを作る
  145. /// </summary>
  146. /// <param name="sender"></param>
  147. /// <param name="e"></param>
  148. private void Form1_MouseDown(object sender, MouseEventArgs e)
  149. {
  150. int x = e.X / SZ;
  151. int y = e.Y / SZ;
  152.  
  153. isUpdate = false;
  154.  
  155. for (int xx = 0; xx < 5; xx++)
  156. {
  157. for (int yy = 0; yy < 5; yy++)
  158. {
  159. int nx = xx + x;
  160. int ny = yy + y;
  161. if (nx < 0 || nx >= MX || ny < 0 || ny >= MX) continue;
  162.  
  163. lifes[nx,ny] = 1;
  164. }
  165. }
  166. isUpdate = true;
  167.  
  168.  
  169. }
  170.  
  171. }
  172. }
  173.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(6,14): error CS0234: The type or namespace name `Data' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(7,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(10,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(11,24): error CS0234: The type or namespace name `Tasks' does not exist in the namespace `System.Threading'. Are you missing an assembly reference?
Compilation failed: 4 error(s), 0 warnings
stdout
Standard output is empty