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;
- using System.Threading.Tasks;
- namespace mazeGenerationConsole
- {
- class Program
- {
- static int[,] maze;
- static int width, height;
- static void Main(string[] args)
- {
- Console.Write("Szélesség: ");
- width = int.Parse(Console.ReadLine());
- Console.WriteLine();
- Console.Write("Magasság: ");
- height = int.Parse(Console.ReadLine());
- maze = new int[width, height];
- Initialize();
- GenerateMaze();
- Display();
- Console.ReadKey();
- }
- static void GenerateMaze()
- {
- Random rand = new Random();
- List<int> beenRandoms = new List<int>();
- bool topIsDone = false; // 3
- bool leftIsDone = false; // 2
- bool bottomIsDone = false; // 1
- bool rightIsDone = false; // 0
- int row, column, limit;
- while (!(topIsDone & leftIsDone & bottomIsDone & rightIsDone))
- {
- switch (rand.Next(4))
- {
- case 0:
- int sor = rand.Next(height); // 20 magasság esetén 0-19
- int hossz = rand.Next(1, width); // 10 szélesség esetén 1-9 max hosszú falat húzhatunk
- BuildWallFromRight(sor, hossz);
- break;
- }
- }
- }
- static void BuildWallFromRight(int row, int limit)
- {
- int index = maze.GetLength(0);
- for (int i = 0; i < limit && maze[row, index - 1] == 0; i++)
- {
- maze[row, index] += 1;
- index--;
- }
- }
- static void BuildWallFromLeft(int row, int limit)
- {
- for (int i = 0; i < limit && maze[row, i + 1] == 0; i++)
- {
- maze[row, i] += 1;
- }
- }
- static void BuildWallFromTop(int column, int limit)
- {
- for (int i = 0; i < limit && maze[i + 1, column] <= 1; i++)
- {
- maze[i, column] += 2;
- }
- }
- static void BuildWallFromTop(int column, int limit)
- {
- int index = maze.GetLength(1);
- for (int i = 0; i < limit && maze[index - 1, column] <= 1; i++)
- {
- maze[index, column] += 2;
- index++;
- }
- }
- static void Initialize()
- {
- int lastInRow = maze.GetLength(1) - 1;
- int lastInColumn = maze.GetLength(0) - 1;
- for (int i = 0; i < maze.GetLength(0); i++)
- {
- maze[i, lastInRow] += 1;
- }
- for (int i = 0; i < maze.GetLength(1); i++)
- {
- maze[lastInColumn, i] += 2;
- }
- }
- static void Display()
- {
- Console.Clear();
- for (int i = 0; i < maze.GetLength(0); i++)
- {
- for (int j = 0; j < maze.GetLength(1); j++)
- {
- Console.SetCursorPosition(i, j);
- Console.WriteLine(maze[i, j]);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement