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.Windows.Forms;
- namespace LabyGen1
- {
- public partial class Form1 : Form
- {
- Graphics g;
- bool[,] laby = new bool[100, 100];
- Bitmap b = new Bitmap(600, 600);
- public Form1()
- {
- InitializeComponent();
- g = panel1.CreateGraphics();
- }
- private void DrawSetka()
- {
- for (int x = 0; x < 600; x ++)
- {
- for (int y = 0; y < 600; y++)
- {
- if((x + 1) % 6 == 0 )
- b.SetPixel(x, y, Color.Gray);
- if ((y + 1) % 6 == 0)
- b.SetPixel(x, y, Color.Gray);
- }
- }
- }
- private void PKletka(int ix, int iy)
- {
- int tx = ix * 6;
- int ty = iy * 6;
- for (int x = tx; x < (tx + 5); x++)
- {
- for (int y = ty; y < (ty + 5); y++)
- {
- b.SetPixel(x, y, Color.Red);
- }
- }
- }
- private void ViewResult()
- {
- for (int x = 1; x < 99; x++)
- {
- for (int y = 1; y < 99; y++)
- {
- if (laby[x, y] == true)
- PKletka(x, y);
- }
- }
- g.DrawImageUnscaled(b, 1, 1);
- }
- private void ClearAll()
- {
- g.Clear(Color.Black);
- b = new Bitmap(600, 600);
- for (int x = 1; x < 99; x++)
- {
- for (int y = 1; y < 99; y++)
- {
- laby[x, y] = false;
- }
- }
- }
- private void LabyInit()
- {
- Random r = new Random();
- int t = 0;
- for (int x = 1; x < 99; x++)
- {
- for (int y = 1; y < 99; y++)
- {
- t = r.Next(1, 11);
- if (t == 1) laby[x, y] = true;
- }
- }
- for (int x = 1; x < 99; x++)
- {
- for (int y = 1; y < 99; y++)
- {
- PathMaker(x, y);
- }
- }
- }
- private void PathMaker(int x, int y)
- {
- int c = 0;
- if (y > 1)
- if (laby[(x), (y - 1)] == true) c++;
- if (x < 99)
- if (laby[(x + 1), (y)] == true) c++;
- if (y < 99)
- if (laby[(x), (y + 1)] == true) c++;
- if (x > 1)
- if (laby[(x - 1), (y)] == true) c++;
- if (c == 4) laby[x, y] = false;
- if (c < 2)
- {
- Random r = new Random();
- lable1:
- switch(r.Next(1, 5))
- {
- case 1:
- if (y > 1)
- {
- if (laby[(x), (y - 1)] == true) goto lable1;
- else
- {
- laby[(x), (y - 1)] = true;
- PathMaker((x), (y - 1));
- }
- }
- else goto lable1;
- break;
- case 2:
- if (x < 99)
- {
- if (laby[(x + 1), (y)] == true) goto lable1;
- else
- {
- laby[(x + 1), (y)] = true;
- PathMaker((x + 1), (y));
- }
- }
- else goto lable1;
- break;
- case 3:
- if (y < 99)
- {
- if (laby[(x), (y + 1)] == true) goto lable1;
- else
- {
- laby[(x), (y + 1)] = true;
- PathMaker((x), (y + 1));
- }
- }
- else goto lable1;
- break;
- case 4:
- if (x > 1)
- {
- if (laby[(x - 1), (y)] == true) goto lable1;
- else
- {
- laby[(x - 1), (y)] = true;
- PathMaker((x - 1), (y));
- }
- }
- else goto lable1;
- break;
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- ClearAll();
- DrawSetka();
- LabyInit();
- //
- ViewResult();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement