Advertisement
Guest User

Untitled

a guest
May 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.46 KB | None | 0 0
  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;
  9. using System.Windows.Forms;
  10.  
  11. namespace GameOfLife
  12. {
  13. public partial class Form1 : Form
  14. {
  15.  
  16. Graphics graphics;
  17. Pen pen = new Pen(Color.Black);
  18. static int cells_cnt = 3;
  19. int width = 600;
  20. int height = 600;
  21. int points_cnt = 10;
  22. int[,] states_of_cells = new int[cells_cnt, cells_cnt];
  23. int[,] states_of_cells_step_2 = new int[cells_cnt, cells_cnt];
  24. Brush brush = new SolidBrush(Color.Green);
  25. Brush brush_2 = new SolidBrush(Color.Red);
  26.  
  27.  
  28.  
  29. public Form1()
  30. {
  31. InitializeComponent();
  32. graphics = panel1.CreateGraphics();
  33. drawGrid();
  34. }
  35.  
  36. private void drawGrid()
  37. {
  38. panel1.Size = new Size(width, height);
  39. int w, h;
  40. if (width % cells_cnt != 0)
  41. {
  42. w = width - width % cells_cnt;
  43. }
  44. else
  45. {
  46. w = width;
  47. }
  48. if (height % cells_cnt != 0)
  49. {
  50. h = height - height % cells_cnt;
  51. }
  52. else
  53. {
  54. h = height;
  55. }
  56.  
  57. int x = w / cells_cnt;
  58. int y = h / cells_cnt;
  59. for (int i = 0; i < cells_cnt; i++)
  60. {
  61. for (int j = 0; j < cells_cnt; j++)
  62. {
  63.  
  64. Rectangle rectangle = new Rectangle(x * i, y * j, x, y);
  65. graphics.DrawRectangle(pen, rectangle);
  66.  
  67. }
  68. }
  69. panel1.Update();
  70.  
  71.  
  72. int a, b;
  73. a = 1;
  74. b = 1;
  75.  
  76. states_of_cells[a, b] = 1;
  77. states_of_cells[a - 1, b] = 1;
  78. states_of_cells[a + 1, b] = 1;
  79.  
  80.  
  81.  
  82.  
  83. for (int i = 0; i < cells_cnt; i++)
  84. {
  85. for (int j = 0; j < cells_cnt; j++)
  86. {
  87.  
  88. if (states_of_cells[i, j] == 1)
  89. {
  90. Rectangle rectangle = new Rectangle(x * i + 1, y * j + 1, x - 1, y - 1);
  91. graphics.FillRectangle(brush, rectangle);
  92. panel1.Update();
  93. }
  94. else if (states_of_cells[i, j] == 0)
  95. {
  96. Rectangle rectangle = new Rectangle(x * i + 1, y * j + 1, x - 1, y - 1);
  97. graphics.FillRectangle(brush_2, rectangle);
  98. panel1.Update();
  99. }
  100. }
  101. }
  102.  
  103.  
  104.  
  105. int x1 = w / cells_cnt;
  106. int y1 = h / cells_cnt;
  107. int cells_around = 0;
  108. states_of_cells_step_2 = states_of_cells;
  109.  
  110. for (int i = 0; i < cells_cnt; i++)
  111. {
  112.  
  113.  
  114. for (int j = 0; j < cells_cnt; j++)
  115. {
  116. cells_around = 0;
  117. if (i - 1 >= 0 && j - 1 >= 0)
  118. {
  119. // x = i - 1;
  120. // y = j - 1;
  121. if (states_of_cells[i - 1, j - 1] == 1)
  122. cells_around++;
  123. }
  124. if (j - 1 >= 0)
  125. {
  126. // x = i;
  127. // y = j - 1;
  128. if (states_of_cells[i, j - 1] == 1)
  129. cells_around++;
  130. }
  131. if (j - 1 >= 0 && i + 1 < cells_cnt)
  132. {
  133. // x = i + 1;
  134. // y = j - 1;
  135. if (states_of_cells[j - 1, i + 1] == 1)
  136. cells_around++;
  137. }
  138. if (i - 1 >= 0)
  139. {
  140. // x = i - 1;
  141. // y = j;
  142. if (states_of_cells[i - 1, j] == 1)
  143. cells_around++;
  144. }
  145. if (i >= 0 && j >= 0)
  146. {
  147. x = i;
  148. y = j;
  149. if (states_of_cells[i, j] == 1)
  150. cells_around++;
  151. }
  152. if (i + 1 < cells_cnt)
  153. {
  154. x = i + 1;
  155. y = j;
  156. if (states_of_cells[i + 1, j] == 1)
  157. cells_around++;
  158. }
  159. if (i - 1 >= 0 && j + 1 < cells_cnt)
  160. {
  161. x = i - 1;
  162. y = j + 1;
  163. if (states_of_cells[i - 1, j + 1] == 1)
  164. cells_around++;
  165. }
  166. if (j + 1 < cells_cnt)
  167. {
  168. x = i;
  169. y = j + 1;
  170. if (states_of_cells[i, j + 1] == 1)
  171. cells_around++;
  172. }
  173. if (j + 1 < cells_cnt && i + 1 < cells_cnt)
  174. {
  175. x = i + 1;
  176. y = j + 1;
  177. if (states_of_cells[i + 1, j + 1] == 1)
  178. cells_around++;
  179. }
  180.  
  181. if (cells_around == 0 || cells_around == 1)
  182. {
  183. states_of_cells_step_2[i, j] = 0;
  184. }
  185. else if (cells_around == 2)
  186. {
  187. break;
  188. }
  189. else if (cells_around == 3 && states_of_cells[i, j] == 0)
  190. {
  191. states_of_cells_step_2[i, j] = 1;
  192. }
  193. else if (cells_around == 3)
  194. {
  195. break;
  196. }
  197. else if (cells_around > 3)
  198. {
  199. states_of_cells_step_2[i, j] = 0;
  200. }
  201.  
  202. if (states_of_cells_step_2[i, j] == 1)
  203. {
  204. Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
  205. graphics.FillRectangle(brush, rectangle);
  206. panel1.Update();
  207. }
  208. else if (states_of_cells_step_2[i, j] == 0)
  209. {
  210. Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
  211. graphics.FillRectangle(brush_2, rectangle);
  212. panel1.Update();
  213.  
  214. }
  215.  
  216.  
  217. }
  218.  
  219. }
  220. states_of_cells = states_of_cells_step_2;
  221. }
  222.  
  223.  
  224.  
  225.  
  226. private int[,] set_seeds(int points_cnt)
  227. {
  228.  
  229. Array.Clear(states_of_cells, 0, states_of_cells.Length);
  230. Brush brush = new SolidBrush(Color.Green);
  231. Brush brush_2 = new SolidBrush(Color.Empty);
  232.  
  233. int w, h;
  234. if (width % cells_cnt != 0)
  235. {
  236. w = width - (width % cells_cnt);
  237.  
  238. }
  239. else
  240. {
  241. w = width;
  242. }
  243. if (height % cells_cnt != 0)
  244. {
  245. h = height - height % cells_cnt;
  246. }
  247. else
  248. {
  249. h = height;
  250. }
  251.  
  252. int x1 = w / cells_cnt;
  253. int y1 = h / cells_cnt;
  254. int x, y;
  255.  
  256.  
  257.  
  258.  
  259.  
  260. for (int i = 0; i < cells_cnt; i++)
  261. {
  262. for (int j = 0; j < cells_cnt; j++)
  263. {
  264.  
  265. if (states_of_cells[i, j] == 1)
  266. {
  267. Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
  268. graphics.FillRectangle(brush, rectangle);
  269. }
  270. else if (states_of_cells[i, j] == 0)
  271. {
  272. Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
  273. graphics.FillRectangle(brush_2, rectangle);
  274. }
  275. }
  276. }
  277.  
  278. return states_of_cells;
  279.  
  280. }
  281.  
  282. private void fun()
  283. {
  284. int w, h;
  285. if (width % cells_cnt != 0)
  286. {
  287. w = width - width % cells_cnt;
  288. }
  289. else
  290. {
  291. w = width;
  292. }
  293. if (height % cells_cnt != 0)
  294. {
  295. h = height - height % cells_cnt;
  296. }
  297. else
  298. {
  299. h = height;
  300. }
  301.  
  302. int x = w / cells_cnt;
  303. int y = h / cells_cnt;
  304. int x1 = w / cells_cnt;
  305. int y1 = h / cells_cnt;
  306. int cells_around = 0;
  307. states_of_cells_step_2 = states_of_cells;
  308.  
  309. for (int i = 0; i < cells_cnt; i++)
  310. {
  311.  
  312.  
  313. for (int j = 0; j < cells_cnt; j++)
  314. {
  315. cells_around = 0;
  316. if (i - 1 >= 0 && j - 1 >= 0)
  317. {
  318. // x = i - 1;
  319. // y = j - 1;
  320. if (states_of_cells[i - 1, j - 1] == 1)
  321. cells_around++;
  322. }
  323. if (j - 1 >= 0)
  324. {
  325. // x = i;
  326. // y = j - 1;
  327. if (states_of_cells[i, j - 1] == 1)
  328. cells_around++;
  329. }
  330. if (j - 1 >= 0 && i + 1 < cells_cnt)
  331. {
  332. // x = i + 1;
  333. // y = j - 1;
  334. if (states_of_cells[j - 1, i + 1] == 1)
  335. cells_around++;
  336. }
  337. if (i - 1 >= 0)
  338. {
  339. // x = i - 1;
  340. // y = j;
  341. if (states_of_cells[i - 1, j] == 1)
  342. cells_around++;
  343. }
  344. if (i >= 0 && j >= 0)
  345. {
  346. x = i;
  347. y = j;
  348. if (states_of_cells[i, j] == 1)
  349. cells_around++;
  350. }
  351. if (i + 1 < cells_cnt)
  352. {
  353. x = i + 1;
  354. y = j;
  355. if (states_of_cells[i + 1, j] == 1)
  356. cells_around++;
  357. }
  358. if (i - 1 >= 0 && j + 1 < cells_cnt)
  359. {
  360. x = i - 1;
  361. y = j + 1;
  362. if (states_of_cells[i - 1, j + 1] == 1)
  363. cells_around++;
  364. }
  365. if (j + 1 < cells_cnt)
  366. {
  367. x = i;
  368. y = j + 1;
  369. if (states_of_cells[i, j + 1] == 1)
  370. cells_around++;
  371. }
  372. if (j + 1 < cells_cnt && i + 1 < cells_cnt)
  373. {
  374. x = i + 1;
  375. y = j + 1;
  376. if (states_of_cells[i + 1, j + 1] == 1)
  377. cells_around++;
  378. }
  379.  
  380. if (cells_around == 0 || cells_around == 1)
  381. {
  382. states_of_cells_step_2[i, j] = 0;
  383. }
  384. else if (cells_around == 2)
  385. {
  386. break;
  387. }
  388. else if (cells_around == 3 && states_of_cells[i, j] == 0)
  389. {
  390. states_of_cells_step_2[i, j] = 1;
  391. }
  392. else if (cells_around == 3)
  393. {
  394. break;
  395. }
  396. else if (cells_around > 3)
  397. {
  398. states_of_cells_step_2[i, j] = 0;
  399. }
  400.  
  401. if (states_of_cells_step_2[i, j] == 1)
  402. {
  403. Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
  404. graphics.FillRectangle(brush, rectangle);
  405. panel1.Update();
  406. }
  407. else if (states_of_cells_step_2[i, j] == 0)
  408. {
  409. Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
  410. graphics.FillRectangle(brush_2, rectangle);
  411. panel1.Update();
  412.  
  413. }
  414.  
  415.  
  416. }
  417.  
  418. }
  419. states_of_cells = states_of_cells_step_2;
  420. }
  421.  
  422.  
  423. private void detect_(int [,] arr)
  424. {
  425.  
  426. Brush brush = new SolidBrush(Color.Green);
  427. Brush brush_2 = new SolidBrush(Color.Empty);
  428. int w, h;
  429. if (width % cells_cnt != 0)
  430. {
  431. w = width - (width % cells_cnt);
  432.  
  433. }
  434. else
  435. {
  436. w = width;
  437. }
  438. if (height % cells_cnt != 0)
  439. {
  440. h = height - height % cells_cnt;
  441. }
  442. else
  443. {
  444. h = height;
  445. }
  446.  
  447. int x1 = w / cells_cnt;
  448. int y1 = h / cells_cnt;
  449. int[,] states_of_cells_step_2 = new int[cells_cnt, cells_cnt];
  450. int x, y;
  451. int cells_around = 0;
  452. states_of_cells_step_2 = arr;
  453. int i = 0;
  454. int j = 0;
  455. bool flag = true;
  456. while (flag)
  457. {
  458. if (i - 1 > 0 && j - 1 > 0)
  459. {
  460. x = i - 1;
  461. y = j - 1;
  462. if (arr[x, y] == 1)
  463. cells_around++;
  464. }
  465. if (j - 1 > 0)
  466. {
  467. x = i;
  468. y = j - 1;
  469. if (arr[x, y] == 1)
  470. cells_around++;
  471. }
  472. if (j - 1 > 0 && i + 1 < cells_cnt)
  473. {
  474. x = i + 1;
  475. y = j - 1;
  476. if (arr[x, y] == 1)
  477. cells_around++;
  478. }
  479. if (i - 1 > 0)
  480. {
  481. x = i - 1;
  482. y = j;
  483. if (arr[x, y] == 1)
  484. cells_around++;
  485. }
  486. if (i > 0 && j > 0)
  487. {
  488. x = i;
  489. y = j;
  490. if (arr[x, y] == 1)
  491. cells_around++;
  492. }
  493. if (i + 1 < cells_cnt)
  494. {
  495. x = i + 1;
  496. y = j;
  497. if (arr[x, y] == 1)
  498. cells_around++;
  499. }
  500. if (i - 1 > 0 && j + 1 < cells_cnt)
  501. {
  502. x = i - 1;
  503. y = j + 1;
  504. if (arr[x, y] == 1)
  505. cells_around++;
  506. }
  507. if (j + 1 < cells_cnt)
  508. {
  509. x = i;
  510. y = j + 1;
  511. if (arr[x, y] == 1)
  512. cells_around++;
  513. }
  514. if (j + 1 < cells_cnt && i + 1 < cells_cnt )
  515. {
  516. x = i + 1;
  517. y = j + 1;
  518. if (arr[x, y] == 1)
  519. cells_around++;
  520. }
  521.  
  522. if (cells_around == 0 || cells_around == 1)
  523. {
  524. states_of_cells_step_2[i, j] = 0;
  525. }
  526. else if (cells_around == 2)
  527. {
  528.  
  529. }
  530. else if (cells_around == 3 && arr[i, j] == 0)
  531. {
  532. states_of_cells_step_2[i, j] = 1;
  533. }
  534. else if (cells_around == 3)
  535. {
  536.  
  537. }
  538. else if (cells_around > 3)
  539. {
  540. states_of_cells_step_2[i, j] = 0;
  541. }
  542. j++;
  543. cells_around = 0;
  544. if(j==cells_cnt)
  545. {
  546. j = 0;
  547. i++;
  548. arr = states_of_cells_step_2;
  549.  
  550. for (int k = 0; k < cells_cnt; k++)
  551. {
  552. for (int z = 0; z < cells_cnt; z++)
  553. {
  554.  
  555. if (states_of_cells_step_2[k, z] == 1)
  556. {
  557. Rectangle rectangle = new Rectangle(x1 * k + 1, y1 * z + 1, x1 - 1, y1 - 1);
  558. graphics.FillRectangle(brush, rectangle);
  559. }
  560. else if (states_of_cells_step_2[k, z] == 0)
  561. {
  562. Rectangle rectangle = new Rectangle(x1 * k + 1, y1 * z + 1, x1 - 1, y1 - 1);
  563. graphics.FillRectangle(brush_2, rectangle);
  564. }
  565. }
  566. }
  567.  
  568. }
  569. if(i==cells_cnt)
  570. {
  571. flag = false;
  572. }
  573.  
  574. }
  575.  
  576.  
  577.  
  578.  
  579.  
  580. }
  581.  
  582.  
  583.  
  584. private void panel1_Paint(object sender, PaintEventArgs e)
  585. {
  586. graphics = panel1.CreateGraphics();
  587. drawGrid();
  588. //fun();
  589.  
  590.  
  591. }
  592.  
  593. private void textBox1_TextChanged(object sender, EventArgs e)
  594. {
  595.  
  596. }
  597.  
  598. private void button1_Click(object sender, EventArgs e)
  599. {
  600.  
  601.  
  602. }
  603.  
  604.  
  605. }
  606. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement