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;
- using System.Windows.Forms;
- namespace GameOfLife
- {
- public partial class Form1 : Form
- {
- Graphics graphics;
- Pen pen = new Pen(Color.Black);
- static int cells_cnt = 3;
- int width = 600;
- int height = 600;
- int points_cnt = 10;
- int[,] states_of_cells = new int[cells_cnt, cells_cnt];
- int[,] states_of_cells_step_2 = new int[cells_cnt, cells_cnt];
- Brush brush = new SolidBrush(Color.Green);
- Brush brush_2 = new SolidBrush(Color.Red);
- public Form1()
- {
- InitializeComponent();
- graphics = panel1.CreateGraphics();
- drawGrid();
- }
- private void drawGrid()
- {
- panel1.Size = new Size(width, height);
- int w, h;
- if (width % cells_cnt != 0)
- {
- w = width - width % cells_cnt;
- }
- else
- {
- w = width;
- }
- if (height % cells_cnt != 0)
- {
- h = height - height % cells_cnt;
- }
- else
- {
- h = height;
- }
- int x = w / cells_cnt;
- int y = h / cells_cnt;
- for (int i = 0; i < cells_cnt; i++)
- {
- for (int j = 0; j < cells_cnt; j++)
- {
- Rectangle rectangle = new Rectangle(x * i, y * j, x, y);
- graphics.DrawRectangle(pen, rectangle);
- }
- }
- panel1.Update();
- int a, b;
- a = 1;
- b = 1;
- states_of_cells[a, b] = 1;
- states_of_cells[a - 1, b] = 1;
- states_of_cells[a + 1, b] = 1;
- for (int i = 0; i < cells_cnt; i++)
- {
- for (int j = 0; j < cells_cnt; j++)
- {
- if (states_of_cells[i, j] == 1)
- {
- Rectangle rectangle = new Rectangle(x * i + 1, y * j + 1, x - 1, y - 1);
- graphics.FillRectangle(brush, rectangle);
- panel1.Update();
- }
- else if (states_of_cells[i, j] == 0)
- {
- Rectangle rectangle = new Rectangle(x * i + 1, y * j + 1, x - 1, y - 1);
- graphics.FillRectangle(brush_2, rectangle);
- panel1.Update();
- }
- }
- }
- int x1 = w / cells_cnt;
- int y1 = h / cells_cnt;
- int cells_around = 0;
- states_of_cells_step_2 = states_of_cells;
- for (int i = 0; i < cells_cnt; i++)
- {
- for (int j = 0; j < cells_cnt; j++)
- {
- cells_around = 0;
- if (i - 1 >= 0 && j - 1 >= 0)
- {
- // x = i - 1;
- // y = j - 1;
- if (states_of_cells[i - 1, j - 1] == 1)
- cells_around++;
- }
- if (j - 1 >= 0)
- {
- // x = i;
- // y = j - 1;
- if (states_of_cells[i, j - 1] == 1)
- cells_around++;
- }
- if (j - 1 >= 0 && i + 1 < cells_cnt)
- {
- // x = i + 1;
- // y = j - 1;
- if (states_of_cells[j - 1, i + 1] == 1)
- cells_around++;
- }
- if (i - 1 >= 0)
- {
- // x = i - 1;
- // y = j;
- if (states_of_cells[i - 1, j] == 1)
- cells_around++;
- }
- if (i >= 0 && j >= 0)
- {
- x = i;
- y = j;
- if (states_of_cells[i, j] == 1)
- cells_around++;
- }
- if (i + 1 < cells_cnt)
- {
- x = i + 1;
- y = j;
- if (states_of_cells[i + 1, j] == 1)
- cells_around++;
- }
- if (i - 1 >= 0 && j + 1 < cells_cnt)
- {
- x = i - 1;
- y = j + 1;
- if (states_of_cells[i - 1, j + 1] == 1)
- cells_around++;
- }
- if (j + 1 < cells_cnt)
- {
- x = i;
- y = j + 1;
- if (states_of_cells[i, j + 1] == 1)
- cells_around++;
- }
- if (j + 1 < cells_cnt && i + 1 < cells_cnt)
- {
- x = i + 1;
- y = j + 1;
- if (states_of_cells[i + 1, j + 1] == 1)
- cells_around++;
- }
- if (cells_around == 0 || cells_around == 1)
- {
- states_of_cells_step_2[i, j] = 0;
- }
- else if (cells_around == 2)
- {
- break;
- }
- else if (cells_around == 3 && states_of_cells[i, j] == 0)
- {
- states_of_cells_step_2[i, j] = 1;
- }
- else if (cells_around == 3)
- {
- break;
- }
- else if (cells_around > 3)
- {
- states_of_cells_step_2[i, j] = 0;
- }
- if (states_of_cells_step_2[i, j] == 1)
- {
- Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
- graphics.FillRectangle(brush, rectangle);
- panel1.Update();
- }
- else if (states_of_cells_step_2[i, j] == 0)
- {
- Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
- graphics.FillRectangle(brush_2, rectangle);
- panel1.Update();
- }
- }
- }
- states_of_cells = states_of_cells_step_2;
- }
- private int[,] set_seeds(int points_cnt)
- {
- Array.Clear(states_of_cells, 0, states_of_cells.Length);
- Brush brush = new SolidBrush(Color.Green);
- Brush brush_2 = new SolidBrush(Color.Empty);
- int w, h;
- if (width % cells_cnt != 0)
- {
- w = width - (width % cells_cnt);
- }
- else
- {
- w = width;
- }
- if (height % cells_cnt != 0)
- {
- h = height - height % cells_cnt;
- }
- else
- {
- h = height;
- }
- int x1 = w / cells_cnt;
- int y1 = h / cells_cnt;
- int x, y;
- for (int i = 0; i < cells_cnt; i++)
- {
- for (int j = 0; j < cells_cnt; j++)
- {
- if (states_of_cells[i, j] == 1)
- {
- Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
- graphics.FillRectangle(brush, rectangle);
- }
- else if (states_of_cells[i, j] == 0)
- {
- Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
- graphics.FillRectangle(brush_2, rectangle);
- }
- }
- }
- return states_of_cells;
- }
- private void fun()
- {
- int w, h;
- if (width % cells_cnt != 0)
- {
- w = width - width % cells_cnt;
- }
- else
- {
- w = width;
- }
- if (height % cells_cnt != 0)
- {
- h = height - height % cells_cnt;
- }
- else
- {
- h = height;
- }
- int x = w / cells_cnt;
- int y = h / cells_cnt;
- int x1 = w / cells_cnt;
- int y1 = h / cells_cnt;
- int cells_around = 0;
- states_of_cells_step_2 = states_of_cells;
- for (int i = 0; i < cells_cnt; i++)
- {
- for (int j = 0; j < cells_cnt; j++)
- {
- cells_around = 0;
- if (i - 1 >= 0 && j - 1 >= 0)
- {
- // x = i - 1;
- // y = j - 1;
- if (states_of_cells[i - 1, j - 1] == 1)
- cells_around++;
- }
- if (j - 1 >= 0)
- {
- // x = i;
- // y = j - 1;
- if (states_of_cells[i, j - 1] == 1)
- cells_around++;
- }
- if (j - 1 >= 0 && i + 1 < cells_cnt)
- {
- // x = i + 1;
- // y = j - 1;
- if (states_of_cells[j - 1, i + 1] == 1)
- cells_around++;
- }
- if (i - 1 >= 0)
- {
- // x = i - 1;
- // y = j;
- if (states_of_cells[i - 1, j] == 1)
- cells_around++;
- }
- if (i >= 0 && j >= 0)
- {
- x = i;
- y = j;
- if (states_of_cells[i, j] == 1)
- cells_around++;
- }
- if (i + 1 < cells_cnt)
- {
- x = i + 1;
- y = j;
- if (states_of_cells[i + 1, j] == 1)
- cells_around++;
- }
- if (i - 1 >= 0 && j + 1 < cells_cnt)
- {
- x = i - 1;
- y = j + 1;
- if (states_of_cells[i - 1, j + 1] == 1)
- cells_around++;
- }
- if (j + 1 < cells_cnt)
- {
- x = i;
- y = j + 1;
- if (states_of_cells[i, j + 1] == 1)
- cells_around++;
- }
- if (j + 1 < cells_cnt && i + 1 < cells_cnt)
- {
- x = i + 1;
- y = j + 1;
- if (states_of_cells[i + 1, j + 1] == 1)
- cells_around++;
- }
- if (cells_around == 0 || cells_around == 1)
- {
- states_of_cells_step_2[i, j] = 0;
- }
- else if (cells_around == 2)
- {
- break;
- }
- else if (cells_around == 3 && states_of_cells[i, j] == 0)
- {
- states_of_cells_step_2[i, j] = 1;
- }
- else if (cells_around == 3)
- {
- break;
- }
- else if (cells_around > 3)
- {
- states_of_cells_step_2[i, j] = 0;
- }
- if (states_of_cells_step_2[i, j] == 1)
- {
- Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
- graphics.FillRectangle(brush, rectangle);
- panel1.Update();
- }
- else if (states_of_cells_step_2[i, j] == 0)
- {
- Rectangle rectangle = new Rectangle(x1 * i + 1, y1 * j + 1, x1 - 1, y1 - 1);
- graphics.FillRectangle(brush_2, rectangle);
- panel1.Update();
- }
- }
- }
- states_of_cells = states_of_cells_step_2;
- }
- private void detect_(int [,] arr)
- {
- Brush brush = new SolidBrush(Color.Green);
- Brush brush_2 = new SolidBrush(Color.Empty);
- int w, h;
- if (width % cells_cnt != 0)
- {
- w = width - (width % cells_cnt);
- }
- else
- {
- w = width;
- }
- if (height % cells_cnt != 0)
- {
- h = height - height % cells_cnt;
- }
- else
- {
- h = height;
- }
- int x1 = w / cells_cnt;
- int y1 = h / cells_cnt;
- int[,] states_of_cells_step_2 = new int[cells_cnt, cells_cnt];
- int x, y;
- int cells_around = 0;
- states_of_cells_step_2 = arr;
- int i = 0;
- int j = 0;
- bool flag = true;
- while (flag)
- {
- if (i - 1 > 0 && j - 1 > 0)
- {
- x = i - 1;
- y = j - 1;
- if (arr[x, y] == 1)
- cells_around++;
- }
- if (j - 1 > 0)
- {
- x = i;
- y = j - 1;
- if (arr[x, y] == 1)
- cells_around++;
- }
- if (j - 1 > 0 && i + 1 < cells_cnt)
- {
- x = i + 1;
- y = j - 1;
- if (arr[x, y] == 1)
- cells_around++;
- }
- if (i - 1 > 0)
- {
- x = i - 1;
- y = j;
- if (arr[x, y] == 1)
- cells_around++;
- }
- if (i > 0 && j > 0)
- {
- x = i;
- y = j;
- if (arr[x, y] == 1)
- cells_around++;
- }
- if (i + 1 < cells_cnt)
- {
- x = i + 1;
- y = j;
- if (arr[x, y] == 1)
- cells_around++;
- }
- if (i - 1 > 0 && j + 1 < cells_cnt)
- {
- x = i - 1;
- y = j + 1;
- if (arr[x, y] == 1)
- cells_around++;
- }
- if (j + 1 < cells_cnt)
- {
- x = i;
- y = j + 1;
- if (arr[x, y] == 1)
- cells_around++;
- }
- if (j + 1 < cells_cnt && i + 1 < cells_cnt )
- {
- x = i + 1;
- y = j + 1;
- if (arr[x, y] == 1)
- cells_around++;
- }
- if (cells_around == 0 || cells_around == 1)
- {
- states_of_cells_step_2[i, j] = 0;
- }
- else if (cells_around == 2)
- {
- }
- else if (cells_around == 3 && arr[i, j] == 0)
- {
- states_of_cells_step_2[i, j] = 1;
- }
- else if (cells_around == 3)
- {
- }
- else if (cells_around > 3)
- {
- states_of_cells_step_2[i, j] = 0;
- }
- j++;
- cells_around = 0;
- if(j==cells_cnt)
- {
- j = 0;
- i++;
- arr = states_of_cells_step_2;
- for (int k = 0; k < cells_cnt; k++)
- {
- for (int z = 0; z < cells_cnt; z++)
- {
- if (states_of_cells_step_2[k, z] == 1)
- {
- Rectangle rectangle = new Rectangle(x1 * k + 1, y1 * z + 1, x1 - 1, y1 - 1);
- graphics.FillRectangle(brush, rectangle);
- }
- else if (states_of_cells_step_2[k, z] == 0)
- {
- Rectangle rectangle = new Rectangle(x1 * k + 1, y1 * z + 1, x1 - 1, y1 - 1);
- graphics.FillRectangle(brush_2, rectangle);
- }
- }
- }
- }
- if(i==cells_cnt)
- {
- flag = false;
- }
- }
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- graphics = panel1.CreateGraphics();
- drawGrid();
- //fun();
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement