Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 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.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace CellAutomat
  12. {
  13.  
  14. public partial class Form1 : Form
  15. {
  16. Point[,] table = new Point[200,200];
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. pictureBox1.Image = Image.FromFile("C:\\Users\\Filipp\\Desktop\\123.png");
  21.  
  22. table = GetTable();
  23. draw(table);
  24.  
  25.  
  26. }
  27. public Point[,] update(Point[,] pointss, Point[,] newp)
  28. {
  29.  
  30. var points = new Point[200, 200];
  31.  
  32. for (int i = 0; i < 200; i++) for (int j = 0; j < 200; j++) points[i, j] = new Point(pointss[i, j]);
  33. for (int i = 0; i < 200; i++)
  34. {
  35. for (int j = 0; j < 200; j++)
  36. {
  37. List<Point> p = new List<Point>();
  38. if (i > 0) p.Add(newp[i - 1,j]);
  39. if (j > 0) p.Add(newp[i,j - 1]);
  40.  
  41. if (i < 199) p.Add(newp[i + 1,j]);
  42. if (j < 199) p.Add(newp[i,j + 1]);
  43.  
  44. points[i,j].changeState(p);
  45.  
  46. }
  47.  
  48.  
  49. }
  50.  
  51. return points;
  52. }
  53.  
  54. public Point[,] GetTable()
  55. {
  56. Point[,] t = new Point[200,200];
  57. for (int i = 0; i < 200; i++)
  58. {
  59.  
  60. for (int j = 0; j < 200; j++)
  61. {
  62. if (i == 100 && j == 100) t[i,j]=new Point(i, j, 1);
  63. else t[i,j]=new Point(i, j, 0);
  64. }
  65.  
  66. }
  67.  
  68. return t;
  69. }
  70.  
  71. public void draw(Point[,] pointss)
  72. {
  73. Bitmap bm = new Bitmap(pictureBox1.Image);
  74.  
  75. for (int i = 0; i < 200; i++)
  76. {
  77. for (int j = 0; j < 200; j++)
  78. {
  79. if (pointss[i,j].state == 0)
  80. {
  81.  
  82. bm.SetPixel(pointss[i,j].x, pointss[i,j].y, Color.Black);
  83.  
  84. }
  85.  
  86.  
  87. if (pointss[i,j].state == 1)
  88. {
  89.  
  90. bm.SetPixel(pointss[i, j].x, pointss[i, j].y, Color.White);
  91.  
  92. }
  93. }
  94. }
  95. pictureBox1.Image = bm;
  96. }
  97.  
  98.  
  99. private void button1_Click(object sender, EventArgs e)
  100. {
  101.  
  102. Point.rule = int.Parse(textBox1.Text);
  103. Point[,] table1 = new Point[200,200];
  104. for (int i = 0; i < 200; i++) for (int j = 0; j < 200; j++) table1[i, j] = new Point(table[i, j]);
  105.  
  106. draw(table1);
  107.  
  108. Point[,] table2 = new Point[200, 200];
  109. table2 = update(table, table1);
  110. for (int i = 0; i < 200; i++) for (int j = 0; j < 200; j++) table[i, j] = new Point(table2[i, j]);
  111.  
  112.  
  113.  
  114.  
  115. }
  116.  
  117.  
  118. }
  119.  
  120. public class Point
  121. {
  122. public int x, y, state;
  123. public static int rule;
  124.  
  125.  
  126. public Point(int x, int y, int state)
  127. {
  128. this.x = x;
  129. this.y = y;
  130. this.state = state;
  131.  
  132. }
  133.  
  134. public Point(Point p)
  135. {
  136. this.x = p.x;
  137. this.y = p.y;
  138. this.state = p.state;
  139.  
  140. }
  141.  
  142. public int getCountMate(List<Point> mate)
  143. {
  144. int cnt = 0;
  145. foreach (Point m in mate) if (m.state==1) cnt++;
  146. return cnt;
  147. }
  148.  
  149. public void changeState(List<Point> mate)
  150. {
  151. int cnt = getCountMate(mate);
  152. if(state == 0) { state = (rule >> (4-cnt)) & 1; }
  153. else { state = ((rule >> 5) >> (4-cnt)) & 1; }
  154.  
  155. }
  156.  
  157.  
  158. }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement