Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 CellAutomat
- {
- public partial class Form1 : Form
- {
- Point[,] table = new Point[200,200];
- public Form1()
- {
- InitializeComponent();
- pictureBox1.Image = Image.FromFile("C:\\Users\\Filipp\\Desktop\\123.png");
- table = GetTable();
- draw(table);
- }
- public Point[,] update(Point[,] pointss, Point[,] newp)
- {
- var points = new Point[200, 200];
- for (int i = 0; i < 200; i++) for (int j = 0; j < 200; j++) points[i, j] = new Point(pointss[i, j]);
- for (int i = 0; i < 200; i++)
- {
- for (int j = 0; j < 200; j++)
- {
- List<Point> p = new List<Point>();
- if (i > 0) p.Add(newp[i - 1,j]);
- if (j > 0) p.Add(newp[i,j - 1]);
- if (i < 199) p.Add(newp[i + 1,j]);
- if (j < 199) p.Add(newp[i,j + 1]);
- points[i,j].changeState(p);
- }
- }
- return points;
- }
- public Point[,] GetTable()
- {
- Point[,] t = new Point[200,200];
- for (int i = 0; i < 200; i++)
- {
- for (int j = 0; j < 200; j++)
- {
- if (i == 100 && j == 100) t[i,j]=new Point(i, j, 1);
- else t[i,j]=new Point(i, j, 0);
- }
- }
- return t;
- }
- public void draw(Point[,] pointss)
- {
- Bitmap bm = new Bitmap(pictureBox1.Image);
- for (int i = 0; i < 200; i++)
- {
- for (int j = 0; j < 200; j++)
- {
- if (pointss[i,j].state == 0)
- {
- bm.SetPixel(pointss[i,j].x, pointss[i,j].y, Color.Black);
- }
- if (pointss[i,j].state == 1)
- {
- bm.SetPixel(pointss[i, j].x, pointss[i, j].y, Color.White);
- }
- }
- }
- pictureBox1.Image = bm;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Point.rule = int.Parse(textBox1.Text);
- Point[,] table1 = new Point[200,200];
- for (int i = 0; i < 200; i++) for (int j = 0; j < 200; j++) table1[i, j] = new Point(table[i, j]);
- draw(table1);
- Point[,] table2 = new Point[200, 200];
- table2 = update(table, table1);
- for (int i = 0; i < 200; i++) for (int j = 0; j < 200; j++) table[i, j] = new Point(table2[i, j]);
- }
- }
- public class Point
- {
- public int x, y, state;
- public static int rule;
- public Point(int x, int y, int state)
- {
- this.x = x;
- this.y = y;
- this.state = state;
- }
- public Point(Point p)
- {
- this.x = p.x;
- this.y = p.y;
- this.state = p.state;
- }
- public int getCountMate(List<Point> mate)
- {
- int cnt = 0;
- foreach (Point m in mate) if (m.state==1) cnt++;
- return cnt;
- }
- public void changeState(List<Point> mate)
- {
- int cnt = getCountMate(mate);
- if(state == 0) { state = (rule >> (4-cnt)) & 1; }
- else { state = ((rule >> 5) >> (4-cnt)) & 1; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement