Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 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.Windows.Forms;
  9.  
  10. namespace LabyGen1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         Graphics g;
  15.         bool[,] laby = new bool[100, 100];
  16.         Bitmap b = new Bitmap(600, 600);
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.             g = panel1.CreateGraphics();
  22.         }
  23.  
  24.         private void DrawSetka()
  25.         {
  26.             for (int x = 0; x < 600; x ++)
  27.             {
  28.                 for (int y = 0; y < 600; y++)
  29.                 {
  30.                     if((x + 1) % 6 == 0 )
  31.                         b.SetPixel(x, y, Color.Gray);
  32.                     if ((y + 1) % 6 == 0)
  33.                         b.SetPixel(x, y, Color.Gray);
  34.                 }
  35.             }
  36.         }
  37.  
  38.         private void PKletka(int ix, int iy)
  39.         {
  40.             int tx = ix * 6;
  41.             int ty = iy * 6;
  42.  
  43.             for (int x = tx; x < (tx + 5); x++)
  44.             {
  45.                 for (int y = ty; y < (ty + 5); y++)
  46.                 {
  47.                     b.SetPixel(x, y, Color.Red);
  48.                 }
  49.             }
  50.         }
  51.  
  52.         private void ViewResult()
  53.         {
  54.             for (int x = 1; x < 99; x++)
  55.             {
  56.                 for (int y = 1; y < 99; y++)
  57.                 {
  58.                     if (laby[x, y] == true)
  59.                         PKletka(x, y);
  60.                 }
  61.             }
  62.  
  63.             g.DrawImageUnscaled(b, 1, 1);
  64.         }
  65.  
  66.         private void ClearAll()
  67.         {
  68.             g.Clear(Color.Black);
  69.             b = new Bitmap(600, 600);
  70.             for (int x = 1; x < 99; x++)
  71.             {
  72.                 for (int y = 1; y < 99; y++)
  73.                 {
  74.                     laby[x, y] = false;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         private void LabyInit()
  80.         {
  81.             Random r = new Random();
  82.             int t = 0;
  83.             for (int x = 1; x < 99; x++)
  84.             {
  85.                 for (int y = 1; y < 99; y++)
  86.                 {
  87.                     t = r.Next(1, 11);
  88.                     if (t == 1) laby[x, y] = true;
  89.                 }
  90.             }
  91.  
  92.             for (int x = 1; x < 99; x++)
  93.             {
  94.                 for (int y = 1; y < 99; y++)
  95.                 {
  96.                     PathMaker(x, y);
  97.                 }
  98.             }
  99.         }
  100.  
  101.         private void PathMaker(int x, int y)
  102.         {
  103.             int c = 0;
  104.             if (y > 1)
  105.                 if (laby[(x), (y - 1)] == true) c++;
  106.             if (x < 99)
  107.                 if (laby[(x + 1), (y)] == true) c++;
  108.             if (y < 99)
  109.                 if (laby[(x), (y + 1)] == true) c++;
  110.             if (x > 1)
  111.                 if (laby[(x - 1), (y)] == true) c++;
  112.  
  113.             if (c == 4) laby[x, y] = false;
  114.  
  115.             if (c < 2)
  116.             {
  117.                 Random r = new Random();
  118.             lable1:
  119.                 switch(r.Next(1, 5))
  120.                 {
  121.                     case 1:
  122.                         if (y > 1)
  123.                         {
  124.                             if (laby[(x), (y - 1)] == true) goto lable1;
  125.                             else
  126.                             {
  127.                                 laby[(x), (y - 1)] = true;
  128.                                 PathMaker((x), (y - 1));
  129.                             }
  130.                         }
  131.                         else goto lable1;
  132.                         break;
  133.                     case 2:
  134.                         if (x < 99)
  135.                         {
  136.                             if (laby[(x + 1), (y)] == true) goto lable1;
  137.                             else
  138.                             {
  139.                                 laby[(x + 1), (y)] = true;
  140.                                 PathMaker((x + 1), (y));
  141.                             }
  142.                         }
  143.                         else goto lable1;
  144.                         break;
  145.                     case 3:
  146.                         if (y < 99)
  147.                         {
  148.                             if (laby[(x), (y + 1)] == true) goto lable1;
  149.                             else
  150.                             {
  151.                                 laby[(x), (y + 1)] = true;
  152.                                 PathMaker((x), (y + 1));
  153.                             }
  154.                         }
  155.                         else goto lable1;
  156.                         break;
  157.                     case 4:
  158.                         if (x > 1)
  159.                         {
  160.                             if (laby[(x - 1), (y)] == true) goto lable1;
  161.                             else
  162.                             {
  163.                                 laby[(x - 1), (y)] = true;
  164.                                 PathMaker((x - 1), (y));
  165.                             }
  166.                         }
  167.                         else goto lable1;
  168.                         break;
  169.                 }
  170.             }
  171.         }
  172.  
  173.  
  174.  
  175.         private void button1_Click(object sender, EventArgs e)
  176.         {
  177.             ClearAll();
  178.             DrawSetka();
  179.             LabyInit();
  180.  
  181.             //
  182.  
  183.             ViewResult();
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement