Advertisement
csaki

labirintus

Nov 13th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace mazeGenerationConsole
  8. {
  9.     class Program
  10.     {
  11.         static int[,] maze;
  12.         static int width, height;
  13.         static void Main(string[] args)
  14.         {
  15.             Console.Write("Szélesség: ");
  16.             width = int.Parse(Console.ReadLine());
  17.             Console.WriteLine();
  18.             Console.Write("Magasság: ");
  19.             height = int.Parse(Console.ReadLine());
  20.             maze = new int[width, height];
  21.  
  22.             Initialize();
  23.             GenerateMaze();
  24.             Display();
  25.             Console.ReadKey();
  26.         }
  27.  
  28.         static void GenerateMaze()
  29.         {
  30.             Random rand = new Random();
  31.             List<int> beenRandoms = new List<int>();
  32.             bool topIsDone = false;    // 3
  33.             bool leftIsDone = false;   // 2
  34.             bool bottomIsDone = false; // 1
  35.             bool rightIsDone = false;  // 0
  36.             int row, column, limit;
  37.  
  38.             while (!(topIsDone & leftIsDone & bottomIsDone & rightIsDone))
  39.             {
  40.                 switch (rand.Next(4))
  41.                 {
  42.                     case 0:
  43.                         int sor = rand.Next(height); // 20 magasság esetén 0-19
  44.                         int hossz = rand.Next(1, width); // 10 szélesség esetén 1-9 max hosszú falat húzhatunk
  45.                         BuildWallFromRight(sor, hossz);
  46.                         break;
  47.                 }
  48.             }
  49.         }
  50.  
  51.         static void BuildWallFromRight(int row, int limit)
  52.         {
  53.             int index = maze.GetLength(0);
  54.             for (int i = 0; i < limit && maze[row, index - 1] == 0; i++)
  55.             {
  56.                 maze[row, index] += 1;
  57.                 index--;
  58.             }
  59.         }
  60.  
  61.         static void BuildWallFromLeft(int row, int limit)
  62.         {
  63.             for (int i = 0; i < limit && maze[row, i + 1] == 0; i++)
  64.             {
  65.                 maze[row, i] += 1;
  66.             }
  67.         }
  68.  
  69.         static void BuildWallFromTop(int column, int limit)
  70.         {
  71.             for (int i = 0; i < limit && maze[i + 1, column] <= 1; i++)
  72.             {
  73.                 maze[i, column] += 2;
  74.             }
  75.         }
  76.  
  77.         static void BuildWallFromTop(int column, int limit)
  78.         {
  79.             int index = maze.GetLength(1);
  80.             for (int i = 0; i < limit && maze[index - 1, column] <= 1; i++)
  81.             {
  82.                 maze[index, column] += 2;
  83.                 index++;
  84.             }
  85.         }
  86.  
  87.         static void Initialize()
  88.         {
  89.             int lastInRow = maze.GetLength(1) - 1;
  90.             int lastInColumn = maze.GetLength(0) - 1;
  91.  
  92.             for (int i = 0; i < maze.GetLength(0); i++)
  93.             {
  94.                 maze[i, lastInRow] += 1;
  95.             }
  96.             for (int i = 0; i < maze.GetLength(1); i++)
  97.             {
  98.                 maze[lastInColumn, i] += 2;
  99.             }
  100.         }
  101.  
  102.         static void Display()
  103.         {
  104.             Console.Clear();
  105.             for (int i = 0; i < maze.GetLength(0); i++)
  106.             {
  107.                 for (int j = 0; j < maze.GetLength(1); j++)
  108.                 {
  109.                     Console.SetCursorPosition(i, j);
  110.                     Console.WriteLine(maze[i, j]);
  111.                 }
  112.             }
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement