Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Maze
- {
- public class LeeAlgorithm
- {
- public int[,] StartGraph { get; private set; }
- public int[,] ArrayGraph { get; private set; }
- public List<Tuple<int, int>> Path { get; private set; }
- public int Width { get; private set; }
- public int Heidth { get; private set; }
- public bool PathFound { get; private set; }
- public int LengthPath { get { return Path.Count; } }
- private int _step;
- private bool _finishingCellMarked;
- private int _finishPointI;
- private int _finishPointJ;
- public LeeAlgorithm(int startX, int startY, int[,] array)
- {
- ArrayGraph = array;
- Width = ArrayGraph.GetLength(0);
- Heidth = ArrayGraph.GetLength(1);
- SetStarCell(startX, startY);
- PathFound = PathSearch();
- }
- public LeeAlgorithm(int[,] array)
- {
- ArrayGraph = array;
- StartGraph = array;
- Width = ArrayGraph.GetLength(0);
- Heidth = ArrayGraph.GetLength(1);
- int startX;
- int startY;
- FindStartCell(out startX, out startY);
- SetStarCell(startX, startY);
- PathFound = PathSearch();
- }
- private void FindStartCell(out int startX, out int startY)
- {
- int w = Width;
- int h = Heidth;
- for (int i = 0; i < w; i++)
- {
- for (int j = 0; j < h; j++)
- {
- if (ArrayGraph[i, j] == (int)Figures.StartPosition)
- {
- startX = i;
- startY = j;
- return;
- }
- }
- }
- throw new AggregateException("Нет начальной точки");
- }
- private void SetStarCell(int startX, int startY)
- {
- if (startX > this.ArrayGraph.GetLength(0) || startX < 0)
- throw new ArgumentException("Неправильная координата x");
- if (startY > this.ArrayGraph.GetLength(1) || startY < 0)
- throw new ArgumentException("Неправильная координата x");
- _step = 0;
- ArrayGraph[startX, startY] = _step;
- }
- private bool PathSearch()
- {
- if (WavePropagation())
- if (RestorePath())
- return true;
- return false;
- }
- private bool WavePropagation()
- {
- int w = Width;
- int h = Heidth;
- bool finished = false;
- do
- {
- for (int i = 0; i < w; i++)
- {
- for (int j = 0; j < h; j++)
- {
- if (ArrayGraph[i, j] == _step)
- {
- if (i != w - 1)
- if (ArrayGraph[i + 1, j] == (int)Figures.EmptySpace) ArrayGraph[i + 1, j] = _step + 1;
- if (j != h - 1)
- if (ArrayGraph[i, j + 1] == (int)Figures.EmptySpace) ArrayGraph[i, j + 1] = _step + 1;
- if (i != 0)
- if (ArrayGraph[i - 1, j] == (int)Figures.EmptySpace) ArrayGraph[i - 1, j] = _step + 1;
- if (j != 0)
- if (ArrayGraph[i, j - 1] == (int)Figures.EmptySpace) ArrayGraph[i, j - 1] = _step + 1;
- if (i < w - 1)
- if (ArrayGraph[i + 1, j] == (int)Figures.Destination)
- {
- _finishPointI = i + 1;
- _finishPointJ = j;
- finished = true;
- }
- if (j < h - 1)
- if (ArrayGraph[i, j + 1] == (int)Figures.Destination)
- {
- _finishPointI = i;
- _finishPointJ = j + 1;
- finished = true;
- }
- if (i > 0)
- if (ArrayGraph[i - 1, j] == (int)Figures.Destination)
- {
- _finishPointI = i - 1;
- _finishPointJ = j;
- finished = true;
- }
- if (j > 0)
- if (ArrayGraph[i, j - 1] == (int)Figures.Destination)
- {
- _finishPointI = i;
- _finishPointJ = j - 1;
- finished = true;
- }
- }
- }
- }
- _step++;
- } while (!finished && _step < w * h);
- _finishingCellMarked = finished;
- return finished;
- }
- private bool RestorePath()
- {
- if (!_finishingCellMarked)
- return false;
- int w = Width;
- int h = Heidth;
- int i = _finishPointI;
- int j = _finishPointJ;
- Path = new List<Tuple<int, int>>();
- AddToPath(i, j);
- do
- {
- if (i < w - 1)
- if (ArrayGraph[i + 1, j] == _step - 1)
- {
- AddToPath(++i, j);
- }
- if (j < h - 1)
- if (ArrayGraph[i, j + 1] == _step - 1)
- {
- AddToPath(i, ++j);
- }
- if (i > 0)
- if (ArrayGraph[i - 1, j] == _step - 1)
- {
- AddToPath(--i, j);
- }
- if (j > 0)
- if (ArrayGraph[i, j - 1] == _step - 1)
- {
- AddToPath(i, --j);
- }
- _step--;
- } while (_step != 0);
- return true;
- }
- private void AddToPath(int x, int y)
- {
- Path.Add(new Tuple<int, int>(x, y));
- }
- }
- }
- namespace Maze
- {
- public enum Figures
- {
- StartPosition = 0,
- EmptySpace = -1,
- Destination = -2,
- Path = -3,
- Barrier = -4
- }
- public enum Level
- {
- FirstFloor = 1,
- SecondFloor = 2,
- ThirdFloor = 3
- }
- public enum Room
- {
- Lab = 1,
- Lib = 2,
- Control = 3
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Maze
- {
- static class ExpertSystem
- {
- static Level StartLevel;
- static Room StartRoom;
- static (int, int) RoomCoord;
- static (int, int) Size = (15, 30);
- static int RngCoef = 70;
- public static void DefinePlace()
- {
- Console.WriteLine("Нажмите:\n1, если стены красного цвета;\n2, если стены жёлтого цвета;\n3, если стены зелёного цвета.");
- switch (int.Parse(Console.ReadLine()))
- {
- case (int)Level.FirstFloor: StartLevel = Level.FirstFloor; break;
- case (int)Level.SecondFloor: StartLevel = Level.SecondFloor; break;
- case (int)Level.ThirdFloor: StartLevel = Level.ThirdFloor; break;
- }
- Console.WriteLine("Нажмите:\n1, если это лаборатория;\n2, если библиотека;\n3, если центр управления.");
- switch (int.Parse(Console.ReadLine()))
- {
- case (int)Room.Lab: RoomCoord = (0, 0); break;
- case (int)Room.Lib: RoomCoord = (0, (Size.Item2 - 1)); break;
- case (int)Room.Control: RoomCoord = (Size.Item1 - 1, 0); break;
- }
- }
- public static void DrawGame()
- {
- var rand = new Random();
- int iStart = RoomCoord.Item1;
- int jStart = RoomCoord.Item2;
- int iLift2Pos, jLift2Pos, iLiftPos, jLiftPos;
- iLiftPos = rand.Next(1, Size.Item1);
- jLiftPos = rand.Next(1, Size.Item2);
- iLift2Pos = rand.Next(1, Size.Item1);
- jLift2Pos = rand.Next(1, Size.Item2);
- LeeAlgorithm maze = null;
- switch (StartLevel)
- {
- case Level.FirstFloor:
- Console.WriteLine("Первый этаж:");;
- maze = CreateMaze(iStart, jStart);
- ExpertSystem.Print(maze.ArrayGraph);
- break;
- case Level.SecondFloor:
- Console.WriteLine("Второй этаж:");
- maze = CreateMaze(iStart, jStart, iLiftPos, jLiftPos);
- ExpertSystem.Print(maze.ArrayGraph);
- Console.WriteLine("Первый этаж:"); ;
- maze = CreateMaze(iLiftPos, jLiftPos);
- ExpertSystem.Print(maze.ArrayGraph);
- break;
- case Level.ThirdFloor:
- Console.WriteLine("Третий этаж:");
- maze = CreateMaze(iStart, jStart, iLift2Pos, jLift2Pos);
- ExpertSystem.Print(maze.ArrayGraph);
- Console.WriteLine("Второй этаж:");
- maze = CreateMaze(iLift2Pos, jLift2Pos, iLiftPos, jLiftPos);
- ExpertSystem.Print(maze.ArrayGraph);
- Console.WriteLine("Первый этаж:"); ;
- maze = CreateMaze(iLiftPos, jLiftPos);
- ExpertSystem.Print(maze.ArrayGraph);
- break;
- }
- Console.WriteLine("Выход достигнут.");
- }
- public static void Print(int[,] array)
- {
- string msg = string.Empty;
- int x = array.GetLength(0);
- int y = array.GetLength(1);
- for (int i = 0; i < x; i++)
- {
- for (int j = 0; j < y; j++)
- {
- switch (array[i, j])
- {
- case (int)Figures.Path: msg = string.Format("{0,3}", "+"); Console.ForegroundColor = ConsoleColor.Yellow; break;
- case (int)Figures.StartPosition: msg = string.Format("{0,3}", "s"); Console.ForegroundColor = ConsoleColor.Green; break;
- case (int)Figures.Destination: msg = string.Format("{0,3}", "d"); Console.ForegroundColor = ConsoleColor.Red; break;
- case (int)Figures.EmptySpace: msg = string.Format("{0,3}", "'"); Console.ForegroundColor = ConsoleColor.DarkBlue; break;
- case (int)Figures.Barrier: msg = string.Format("{0,3}", "*"); Console.ForegroundColor = ConsoleColor.Blue; break;
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- case 8:
- case 9:
- case 10:
- case 11:
- case 12:
- case 13:
- case 14:
- case 15:
- case 16:
- case 17:
- case 18:
- case 19:
- case 20:
- default:
- //msg = string.Format("{0,3}", array[i, j]); Console.ForegroundColor = ConsoleColor.DarkGray; break;
- msg = string.Format("{0,3}", "'"); Console.ForegroundColor = ConsoleColor.DarkBlue; break;
- //default:
- break;
- }
- Console.Write(msg);
- Console.ResetColor();
- }
- Console.WriteLine();
- }
- Console.WriteLine(msg);
- }
- public static LeeAlgorithm CreateMaze(int startI, int startJ, int finishI = 14, int finishJ = 15)
- {
- var rand = new Random();
- LeeAlgorithm maze = null;
- int heigth = Size.Item1;
- int width = Size.Item2;
- bool isMazeCorrect = false;
- while (!isMazeCorrect)
- {
- int[,] my = new int[heigth, width];
- for (int i = 0; i < heigth; i++)
- {
- for (int j = 0; j < width; j++)
- {
- if (rand.Next(100) > RngCoef)
- my[i, j] = (int)Figures.Barrier;
- else
- my[i, j] = (int)Figures.EmptySpace;
- }
- }
- var random = new Random(unchecked((int)DateTime.Now.Millisecond));
- //my[random.Next(heigth), random.Next(width)] = (int)Figures.StartPosition;
- //my[random.Next(heigth), random.Next(width)] = (int)Figures.Destination;
- my[startI, startJ] = (int)Figures.StartPosition;
- my[finishI, finishJ] = (int)Figures.Destination;
- var li = new LeeAlgorithm(my);
- if (li.PathFound)
- {
- isMazeCorrect = true;
- foreach (var item in li.Path)
- {
- if (item == li.Path.Last())
- my[item.Item1, item.Item2] = (int)Figures.StartPosition;
- else if (item == li.Path.First())
- my[item.Item1, item.Item2] = (int)Figures.Destination;
- else
- my[item.Item1, item.Item2] = (int)Figures.Path;
- }
- }
- maze = li;
- }
- return maze;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ExpertSystem.DefinePlace();
- ExpertSystem.DrawGame();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement