Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace LabyrinthGeneratorLibrary
- {
- public class RecursionGenerator : BaseGenerator
- {
- private bool[,] res;
- private int xSize, ySize;
- private Random r;
- private int generationFrequency = 30;
- private void StartInit()
- {
- for (int x = 3; x < xSize - 3; x++)
- {
- for (int y = 3; y < ySize - 3; y++)
- {
- if (r.Next(generationFrequency) == 0) res[x, y] = true;
- }
- }
- }
- private void AllPathFinder()
- {
- for (int i = 0; i < 10; i++)
- {
- for (int x = 2; x < xSize - 2; x++)
- {
- for (int y = 2; y < ySize - 2; y++)
- {
- if (res[x, y])
- {
- PathFinder(x, y);
- }
- }
- }
- }
- }
- private int LookAround(int x, int y)
- {
- int counter = 0;
- if(y + 1 < ySize - 1) if (res[x, y + 1]) counter++;
- if (x + 1 < xSize - 1) if (res[x + 1, y]) counter++;
- if (y - 1 > 1) if (res[x, y - 1]) counter++;
- if (x - 1 > 1) if (res[x - 1, y]) counter++;
- return counter;
- }
- private void PathFinder(int x, int y)
- {
- int around = LookAround(x, y);
- if (around == 3) return;
- if (around > 3) res[x, y] = false;
- if (around < 2)
- {
- pathlabel:
- int direction = r.Next(1, 5);
- // 1
- // 4 0 2
- // 3
- switch (direction)
- {
- case 1:
- {
- if (y - 1 < 2) goto pathlabel;
- else
- {
- res[x, y - 1] = true;
- PathFinder(x, y - 1);
- }
- break;
- }
- case 2:
- {
- if (x + 1 > xSize - 1) goto pathlabel;
- else
- {
- res[x + 1, y] = true;
- PathFinder(x + 1, y);
- }
- break;
- }
- case 3:
- {
- if (y + 1 > ySize - 1) goto pathlabel;
- else
- {
- res[x, y + 1] = true;
- PathFinder(x, y + 1);
- }
- break;
- }
- case 4:
- {
- if (x - 1 < 2) goto pathlabel;
- else
- {
- res[x - 1, y] = true;
- PathFinder(x - 1, y);
- }
- break;
- }
- }
- PathFinder(x, y);
- }
- }
- public override bool[,] Generate(int xSize, int ySize, Random r)
- {
- this.r = r;
- this.xSize = xSize;
- this.ySize = ySize;
- res = new bool[xSize, ySize];
- StartInit();
- AllPathFinder();
- return res;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement