Advertisement
believe_me

Untitled

Sep 9th, 2022
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Maze
  5. {
  6.     public class LeeAlgorithm
  7.     {
  8.         public int[,] StartGraph { get; private set; }
  9.         public int[,] ArrayGraph { get; private set; }
  10.         public List<Tuple<int, int>> Path { get; private set; }
  11.         public int Width { get; private set; }
  12.         public int Heidth { get; private set; }
  13.         public bool PathFound { get; private set; }
  14.         public int LengthPath { get { return Path.Count; } }
  15.  
  16.         private int _step;
  17.         private bool _finishingCellMarked;
  18.         private int _finishPointI;
  19.         private int _finishPointJ;
  20.         public LeeAlgorithm(int startX, int startY, int[,] array)
  21.         {
  22.             ArrayGraph = array;
  23.             Width = ArrayGraph.GetLength(0);
  24.             Heidth = ArrayGraph.GetLength(1);
  25.             SetStarCell(startX, startY);
  26.             PathFound = PathSearch();
  27.         }
  28.         public LeeAlgorithm(int[,] array)
  29.         {
  30.             ArrayGraph = array;
  31.             StartGraph = array;
  32.             Width = ArrayGraph.GetLength(0);
  33.             Heidth = ArrayGraph.GetLength(1);
  34.             int startX;
  35.             int startY;
  36.             FindStartCell(out startX, out startY);
  37.             SetStarCell(startX, startY);
  38.             PathFound = PathSearch();
  39.         }
  40.  
  41.         private void FindStartCell(out int startX, out int startY)
  42.         {
  43.             int w = Width;
  44.             int h = Heidth;
  45.  
  46.             for (int i = 0; i < w; i++)
  47.             {
  48.                 for (int j = 0; j < h; j++)
  49.                 {
  50.                     if (ArrayGraph[i, j] == (int)Figures.StartPosition)
  51.                     {
  52.                         startX = i;
  53.                         startY = j;
  54.                         return;
  55.                     }
  56.                 }
  57.             }
  58.             throw new AggregateException("Нет начальной точки");
  59.         }
  60.  
  61.         private void SetStarCell(int startX, int startY)
  62.         {
  63.             if (startX > this.ArrayGraph.GetLength(0) || startX < 0)
  64.                 throw new ArgumentException("Неправильная координата x");
  65.             if (startY > this.ArrayGraph.GetLength(1) || startY < 0)
  66.                 throw new ArgumentException("Неправильная координата x");
  67.             _step = 0;
  68.             ArrayGraph[startX, startY] = _step;
  69.         }
  70.  
  71.         private bool PathSearch()
  72.         {
  73.             if (WavePropagation())
  74.                 if (RestorePath())
  75.                     return true;
  76.  
  77.             return false;
  78.         }
  79.         private bool WavePropagation()
  80.         {
  81.             int w = Width;
  82.             int h = Heidth;
  83.             bool finished = false;
  84.             do
  85.             {
  86.                 for (int i = 0; i < w; i++)
  87.                 {
  88.                     for (int j = 0; j < h; j++)
  89.                     {
  90.                         if (ArrayGraph[i, j] == _step)
  91.                         {
  92.                             if (i != w - 1)
  93.                                 if (ArrayGraph[i + 1, j] == (int)Figures.EmptySpace) ArrayGraph[i + 1, j] = _step + 1;
  94.                             if (j != h - 1)
  95.                                 if (ArrayGraph[i, j + 1] == (int)Figures.EmptySpace) ArrayGraph[i, j + 1] = _step + 1;
  96.                             if (i != 0)
  97.                                 if (ArrayGraph[i - 1, j] == (int)Figures.EmptySpace) ArrayGraph[i - 1, j] = _step + 1;
  98.                             if (j != 0)
  99.                                 if (ArrayGraph[i, j - 1] == (int)Figures.EmptySpace) ArrayGraph[i, j - 1] = _step + 1;
  100.                             if (i < w - 1)
  101.                                 if (ArrayGraph[i + 1, j] == (int)Figures.Destination)
  102.                                 {
  103.                                     _finishPointI = i + 1;
  104.                                     _finishPointJ = j;
  105.                                     finished = true;
  106.                                 }
  107.                             if (j < h - 1)
  108.                                 if (ArrayGraph[i, j + 1] == (int)Figures.Destination)
  109.                                 {
  110.                                     _finishPointI = i;
  111.                                     _finishPointJ = j + 1;
  112.                                     finished = true;
  113.                                 }
  114.                             if (i > 0)
  115.                                 if (ArrayGraph[i - 1, j] == (int)Figures.Destination)
  116.                                 {
  117.                                     _finishPointI = i - 1;
  118.                                     _finishPointJ = j;
  119.                                     finished = true;
  120.                                 }
  121.                             if (j > 0)
  122.                                 if (ArrayGraph[i, j - 1] == (int)Figures.Destination)
  123.                                 {
  124.                                     _finishPointI = i;
  125.                                     _finishPointJ = j - 1;
  126.                                     finished = true;
  127.                                 }
  128.                         }
  129.  
  130.                     }
  131.                 }
  132.                 _step++;
  133.             } while (!finished && _step < w * h);
  134.             _finishingCellMarked = finished;
  135.             return finished;
  136.         }
  137.         private bool RestorePath()
  138.         {
  139.             if (!_finishingCellMarked)
  140.                 return false;
  141.             int w = Width;
  142.             int h = Heidth;
  143.             int i = _finishPointI;
  144.             int j = _finishPointJ;
  145.             Path = new List<Tuple<int, int>>();
  146.             AddToPath(i, j);
  147.             do
  148.             {
  149.                 if (i < w - 1)
  150.                     if (ArrayGraph[i + 1, j] == _step - 1)
  151.                     {
  152.                         AddToPath(++i, j);
  153.                     }
  154.                 if (j < h - 1)
  155.                     if (ArrayGraph[i, j + 1] == _step - 1)
  156.                     {
  157.                         AddToPath(i, ++j);
  158.                     }
  159.                 if (i > 0)
  160.                     if (ArrayGraph[i - 1, j] == _step - 1)
  161.                     {
  162.                         AddToPath(--i, j);
  163.                     }
  164.                 if (j > 0)
  165.                     if (ArrayGraph[i, j - 1] == _step - 1)
  166.                     {
  167.                         AddToPath(i, --j);
  168.                     }
  169.                 _step--;
  170.             } while (_step != 0);
  171.             return true;
  172.         }
  173.         private void AddToPath(int x, int y)
  174.         {
  175.             Path.Add(new Tuple<int, int>(x, y));
  176.         }
  177.     }
  178. }
  179.  
  180. namespace  Maze
  181. {
  182.     public enum Figures
  183.     {
  184.         StartPosition = 0,
  185.         EmptySpace = -1,
  186.         Destination = -2,
  187.         Path = -3,
  188.         Barrier = -4
  189.     }
  190.  
  191.     public enum Level
  192.     {
  193.         FirstFloor = 1,
  194.         SecondFloor = 2,
  195.         ThirdFloor = 3
  196.     }
  197.     public enum Room
  198.     {  
  199.         Lab = 1,
  200.         Lib = 2,
  201.         Control = 3
  202.     }
  203. }
  204.  
  205.  
  206. using System;
  207. using System.Collections.Generic;
  208. using System.Linq;
  209. using System.Text;
  210. using System.Threading.Tasks;
  211.  
  212. namespace Maze
  213. {
  214.     static class ExpertSystem
  215.     {
  216.         static Level StartLevel;
  217.         static Room StartRoom;
  218.         static (int, int) RoomCoord;
  219.         static (int, int) Size = (15, 30);
  220.         static int RngCoef = 70;
  221.         public static void DefinePlace()
  222.         {
  223.             Console.WriteLine("Нажмите:\n1, если стены красного цвета;\n2, если стены жёлтого цвета;\n3, если стены зелёного цвета.");
  224.             switch (int.Parse(Console.ReadLine()))
  225.             {
  226.                 case (int)Level.FirstFloor: StartLevel = Level.FirstFloor;  break;
  227.                 case (int)Level.SecondFloor: StartLevel = Level.SecondFloor; break;
  228.                 case (int)Level.ThirdFloor: StartLevel = Level.ThirdFloor; break;
  229.             }
  230.             Console.WriteLine("Нажмите:\n1, если это лаборатория;\n2, если библиотека;\n3, если центр управления.");
  231.             switch (int.Parse(Console.ReadLine()))
  232.             {
  233.                 case (int)Room.Lab: RoomCoord = (0, 0); break;
  234.                 case (int)Room.Lib: RoomCoord = (0, (Size.Item2 - 1)); break;
  235.                 case (int)Room.Control: RoomCoord = (Size.Item1 - 1, 0); break;
  236.             }
  237.         }
  238.  
  239.         public static void DrawGame()
  240.         {
  241.             var rand = new Random();
  242.             int iStart = RoomCoord.Item1;
  243.             int jStart = RoomCoord.Item2;
  244.             int iLift2Pos, jLift2Pos, iLiftPos, jLiftPos;
  245.             iLiftPos = rand.Next(1, Size.Item1);
  246.             jLiftPos = rand.Next(1, Size.Item2);
  247.             iLift2Pos = rand.Next(1, Size.Item1);
  248.             jLift2Pos = rand.Next(1, Size.Item2);
  249.             LeeAlgorithm maze = null;
  250.             switch (StartLevel)
  251.             {
  252.                 case Level.FirstFloor:
  253.                     Console.WriteLine("Первый этаж:");;
  254.                     maze = CreateMaze(iStart, jStart);
  255.                     ExpertSystem.Print(maze.ArrayGraph);    
  256.                     break;
  257.                 case Level.SecondFloor:
  258.                     Console.WriteLine("Второй этаж:");
  259.                     maze = CreateMaze(iStart, jStart, iLiftPos, jLiftPos);
  260.                     ExpertSystem.Print(maze.ArrayGraph);
  261.                     Console.WriteLine("Первый этаж:"); ;
  262.                     maze = CreateMaze(iLiftPos, jLiftPos);
  263.                     ExpertSystem.Print(maze.ArrayGraph);
  264.                     break;
  265.                 case Level.ThirdFloor:
  266.                     Console.WriteLine("Третий этаж:");
  267.                     maze = CreateMaze(iStart, jStart, iLift2Pos, jLift2Pos);
  268.                     ExpertSystem.Print(maze.ArrayGraph);
  269.                     Console.WriteLine("Второй этаж:");
  270.                     maze = CreateMaze(iLift2Pos, jLift2Pos, iLiftPos, jLiftPos);
  271.                     ExpertSystem.Print(maze.ArrayGraph);
  272.                     Console.WriteLine("Первый этаж:"); ;
  273.                     maze = CreateMaze(iLiftPos, jLiftPos);
  274.                     ExpertSystem.Print(maze.ArrayGraph);
  275.                     break;
  276.             }
  277.             Console.WriteLine("Выход достигнут.");
  278.         }
  279.  
  280.         public static void Print(int[,] array)
  281.         {
  282.             string msg = string.Empty;
  283.             int x = array.GetLength(0);
  284.             int y = array.GetLength(1);
  285.             for (int i = 0; i < x; i++)
  286.             {
  287.                 for (int j = 0; j < y; j++)
  288.                 {
  289.                     switch (array[i, j])
  290.                     {
  291.                         case (int)Figures.Path: msg = string.Format("{0,3}", "+"); Console.ForegroundColor = ConsoleColor.Yellow; break;
  292.                         case (int)Figures.StartPosition: msg = string.Format("{0,3}", "s"); Console.ForegroundColor = ConsoleColor.Green; break;
  293.                         case (int)Figures.Destination: msg = string.Format("{0,3}", "d"); Console.ForegroundColor = ConsoleColor.Red; break;
  294.                         case (int)Figures.EmptySpace: msg = string.Format("{0,3}", "'"); Console.ForegroundColor = ConsoleColor.DarkBlue; break;
  295.                         case (int)Figures.Barrier: msg = string.Format("{0,3}", "*"); Console.ForegroundColor = ConsoleColor.Blue; break;
  296.                         case 1:
  297.                         case 2:
  298.                         case 3:
  299.                         case 4:
  300.                         case 5:
  301.                         case 6:
  302.                         case 7:
  303.                         case 8:
  304.                         case 9:
  305.                         case 10:
  306.                         case 11:
  307.                         case 12:
  308.                         case 13:
  309.                         case 14:
  310.                         case 15:
  311.                         case 16:
  312.                         case 17:
  313.                         case 18:
  314.                         case 19:
  315.                         case 20:
  316.                         default:
  317.                             //msg = string.Format("{0,3}", array[i, j]); Console.ForegroundColor = ConsoleColor.DarkGray; break;
  318.                             msg = string.Format("{0,3}", "'"); Console.ForegroundColor = ConsoleColor.DarkBlue; break;
  319.                         //default:
  320.                             break;
  321.                     }
  322.                     Console.Write(msg);
  323.                     Console.ResetColor();
  324.                 }
  325.                 Console.WriteLine();
  326.             }
  327.             Console.WriteLine(msg);
  328.         }
  329.  
  330.         public static LeeAlgorithm CreateMaze(int startI, int startJ, int finishI = 14, int finishJ = 15)
  331.         {
  332.             var rand = new Random();
  333.             LeeAlgorithm maze = null;
  334.             int heigth = Size.Item1;
  335.             int width = Size.Item2;
  336.             bool isMazeCorrect = false;
  337.             while (!isMazeCorrect)
  338.             {
  339.                 int[,] my = new int[heigth, width];
  340.                 for (int i = 0; i < heigth; i++)
  341.                 {
  342.                     for (int j = 0; j < width; j++)
  343.                     {
  344.                         if (rand.Next(100) > RngCoef)
  345.                             my[i, j] = (int)Figures.Barrier;
  346.                         else
  347.                             my[i, j] = (int)Figures.EmptySpace;
  348.                     }
  349.                 }
  350.                 var random = new Random(unchecked((int)DateTime.Now.Millisecond));
  351.                 //my[random.Next(heigth), random.Next(width)] = (int)Figures.StartPosition;
  352.                 //my[random.Next(heigth), random.Next(width)] = (int)Figures.Destination;
  353.                 my[startI, startJ] = (int)Figures.StartPosition;
  354.                 my[finishI, finishJ] = (int)Figures.Destination;
  355.                 var li = new LeeAlgorithm(my);
  356.                 if (li.PathFound)
  357.                 {
  358.                     isMazeCorrect = true;
  359.                     foreach (var item in li.Path)
  360.                     {
  361.                         if (item == li.Path.Last())
  362.                             my[item.Item1, item.Item2] = (int)Figures.StartPosition;
  363.                         else if (item == li.Path.First())
  364.                             my[item.Item1, item.Item2] = (int)Figures.Destination;
  365.                         else
  366.                             my[item.Item1, item.Item2] = (int)Figures.Path;
  367.                     }
  368.                 }
  369.                 maze = li;
  370.             }
  371.             return maze;
  372.         }
  373.     }
  374. }
  375.  
  376.  
  377. class Program
  378.         {
  379.             static void Main(string[] args)
  380.             {
  381.                 ExpertSystem.DefinePlace();
  382.                 ExpertSystem.DrawGame();
  383.             }
  384.         }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement