Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace MazePathfinder {
  9.     #region toolClasses
  10.     class Pos
  11.     {
  12.         public int X { get; set; }
  13.         public int Y { get; set; }
  14.  
  15.         public Pos(int x, int y)
  16.         {
  17.             X = x;
  18.             Y = y;
  19.         }
  20.     }
  21.  
  22.     class Explorer
  23.     {
  24.         public static List<Pos> AllHistory { get; set; } //collection of all positions in maze explored by any Explorer object
  25.  
  26.         public static bool BeenExplored(Pos p)
  27.         {
  28.             return AllHistory.Any(h => h.X == p.X && h.Y == p.Y);
  29.         }
  30.  
  31.         public Pos Position { get; private set; }
  32.         public List<Pos> Path { get; private set; } //the path that this explorer object has traversed
  33.  
  34.         public Explorer(int x, int y)
  35.         {
  36.             Position = new Pos(x, y);
  37.             Path = new List<Pos>();
  38.             Path.Add(Position);
  39.  
  40.             if (Explorer.AllHistory == null)
  41.             {
  42.                 Explorer.AllHistory = new List<Pos>();
  43.             }
  44.  
  45.             if (!Explorer.BeenExplored(Position)) {
  46.                 Explorer.AllHistory.Add(Position);
  47.             }
  48.         }
  49.  
  50.         public Explorer newPosition(int x, int y)
  51.         {
  52.             Explorer newEx = new Explorer(x, y);
  53.             newEx.Path = Path.ToList();
  54.             if (!newEx.Path.Any(p => p.X == Position.X && p.Y == Position.Y)) {
  55.                 newEx.Path.Add(Position);
  56.             }
  57.            
  58.             if (!Explorer.BeenExplored(Position)) {
  59.                 Explorer.AllHistory.Add(Position);
  60.             }
  61.  
  62.             return newEx;
  63.         }
  64.     }
  65.     #endregion toolClasses
  66.  
  67.     internal class Program
  68.     {
  69.  
  70.         private static void Main(string[] args) {
  71.             char[,] mazeTxt = getMapFromFile();
  72.  
  73.             string waypoints = "01234567";
  74.  
  75.             Pos start = new Pos(-1, -1);
  76.  
  77.             for (int y = 0; y < mazeTxt.GetLength(1); y++) {
  78.                 for (int x = 0; x < mazeTxt.GetLength(0); x++) {
  79.                     if (mazeTxt[x, y] == waypoints[0]) {
  80.                         start = new Pos(x, y);
  81.                     }
  82.                 }
  83.             }
  84.  
  85.             if (start.X == -1 || start.Y == -1)
  86.             {
  87.                 throw new Exception("Start not found");
  88.             }
  89.  
  90.  
  91.             int totalPath = 0;
  92.  
  93.             for (int w = 1; w < waypoints.Length; w++) {
  94.  
  95.                 Explorer reachedPoint = explore(start, waypoints[w], mazeTxt);
  96.                
  97.                 displayCompleted(reachedPoint, (char[,])mazeTxt.Clone(), waypoints);
  98.  
  99.                 Console.WriteLine("");
  100.  
  101.                 start = reachedPoint.Position;
  102.  
  103.                 int steps = reachedPoint.Path.Count() - 1;
  104.  
  105.                 totalPath += steps;
  106.  
  107.                 Explorer.AllHistory.Clear();
  108.  
  109.                 Console.WriteLine("Steps this leg: " + steps);
  110.  
  111.                 Console.WriteLine("Total steps: " + totalPath.ToString());
  112.  
  113.                 Console.ReadLine();
  114.             }
  115.  
  116.             Console.WriteLine("Complete");
  117.  
  118.             Console.ReadLine();
  119.         }
  120.  
  121.  
  122.         private static char[,] getMapFromFile() {
  123.             string[] file = System.IO.File.ReadAllLines("maze.txt");
  124.  
  125.             int txtWidth = file.Max(l => l.Trim().Length);
  126.             int txtHeight = file.Length;
  127.  
  128.             char[,] mazeTxt = new char[txtWidth, txtHeight];
  129.  
  130.             Pos start = new Pos(-1, -1);
  131.  
  132.             for (int l = 0; l < file.Length; l++) //convert maze file to a 2-dimensional char array
  133.             {
  134.                 char[] line = file[l].ToCharArray();
  135.                 for (int c = 0; c < line.Length; c++) {
  136.                     mazeTxt[c, l] = line[c];
  137.                 }
  138.             }
  139.  
  140.             return mazeTxt;
  141.         }
  142.  
  143.         private static Explorer explore(Pos Start, char End, char[,] mazeTxt) {
  144.             List<Explorer> explorerList = new List<Explorer>() { new Explorer(Start.X, Start.Y) };
  145.             //will hold a list of Explorer objects representing one generation of the exploration
  146.  
  147.             Explorer reachedFinish = null; //will hold the Explorer object that reaches the exit
  148.  
  149.  
  150.  
  151.             while (reachedFinish == null) {
  152.                 List<Explorer> newExplorerList = new List<Explorer>();
  153.  
  154.                 foreach (Explorer explorer in explorerList) //go through each explorer in the list
  155.                 {
  156.                     Pos thisRoom = explorer.Position;
  157.  
  158.                     if (mazeTxt[thisRoom.X, thisRoom.Y] == End) //make sure we haven't reached the exit
  159.                     {
  160.                         reachedFinish = explorer;
  161.                         break;
  162.                     } else {
  163.                         foreach (
  164.                             Pos direction in
  165.                                 new List<Pos>() //check the four cardinal directions around the current position
  166.                                 {
  167.                                     new Pos(thisRoom.X + 1, thisRoom.Y),
  168.                                     new Pos(thisRoom.X - 1, thisRoom.Y),
  169.                                     new Pos(thisRoom.X, thisRoom.Y + 1),
  170.                                     new Pos(thisRoom.X, thisRoom.Y - 1)
  171.                                 }) {
  172.                             if (mazeTxt[direction.X, direction.Y] != '#' &&
  173.                                 !Explorer.BeenExplored(direction))
  174.                             //if the searched direction isn't a wall or a location that has allready been explored...
  175.                             {
  176.                                 newExplorerList.Add(explorer.newPosition(direction.X, direction.Y));
  177.                                 //...generate a new Explorer object for that position and add it to the list for the next generation.
  178.                             }
  179.                         }
  180.                     }
  181.                 }
  182.  
  183.                 explorerList = newExplorerList;
  184.  
  185.                 if (!explorerList.Any() && reachedFinish == null) {
  186.                     throw new Exception("No path to exit");
  187.                 }
  188.  
  189.             }
  190.            
  191.             return reachedFinish;
  192.         }
  193.  
  194.  
  195.  
  196.  
  197.         private static void displayCompleted(Explorer reachedFinish, char[,] mazeTxt, string waypoints) {
  198.             for (int x = 0; x < reachedFinish.Path.Count(); x++)
  199.             //once an Explorer has found the exit, go through the path it took and write it to the maze array
  200.             {
  201.                 Pos pos = reachedFinish.Path[x];
  202.                 Pos lastPos = (x == 0 ? pos : reachedFinish.Path[x - 1]);
  203.                 Pos nextPos = (x == reachedFinish.Path.Count() - 1 ? pos : reachedFinish.Path[x + 1]);
  204.  
  205.                 if (!waypoints.Contains(mazeTxt[pos.X, pos.Y])) //fancy path graphics
  206.                 {
  207.                     if ((lastPos.X > nextPos.X && lastPos.Y > nextPos.Y && lastPos.Y == pos.Y) ||
  208.                         (lastPos.X < nextPos.X && lastPos.Y < nextPos.Y && lastPos.X == pos.X))
  209.                         mazeTxt[pos.X, pos.Y] = '└';
  210.                     else if ((lastPos.X > nextPos.X && lastPos.Y > nextPos.Y && lastPos.X == pos.X) ||
  211.                              (lastPos.X < nextPos.X && lastPos.Y < nextPos.Y && lastPos.Y == pos.Y))
  212.                         mazeTxt[pos.X, pos.Y] = '┐';
  213.                     else if ((lastPos.X > nextPos.X && lastPos.Y < nextPos.Y && lastPos.Y == pos.Y) ||
  214.                              (lastPos.X < nextPos.X && lastPos.Y > nextPos.Y && lastPos.X == pos.X))
  215.                         mazeTxt[pos.X, pos.Y] = '┌';
  216.                     else if ((lastPos.X > nextPos.X && lastPos.Y < nextPos.Y && lastPos.X == pos.X) ||
  217.                              (lastPos.X < nextPos.X && lastPos.Y > nextPos.Y && lastPos.Y == pos.Y))
  218.                         mazeTxt[pos.X, pos.Y] = '┘';
  219.                     else if (lastPos.X == pos.X)
  220.                         mazeTxt[pos.X, pos.Y] = '│';
  221.                     else if (lastPos.Y == pos.Y)
  222.                         mazeTxt[pos.X, pos.Y] = '─';
  223.                 }
  224.             }
  225.  
  226.  
  227.             Console.ForegroundColor = ConsoleColor.Gray;
  228.  
  229.  
  230.             int width = mazeTxt.GetLength(1);
  231.             int height = mazeTxt.GetLength(0);
  232.  
  233.             for (int y = 0; y < width; y++) {
  234.                 string line = "";
  235.                 for (int x = 0; x < height; x++) {
  236.                         ConsoleColor color;
  237.  
  238.                         if (mazeTxt[x, y] == '#')
  239.                             color = ConsoleColor.Gray;
  240.                         else if (waypoints.Contains(mazeTxt[x, y]))
  241.                             color = ConsoleColor.Red;
  242.                         else
  243.                             color = ConsoleColor.Yellow;
  244.  
  245.                         Console.ForegroundColor = color;
  246.                         Console.Write(mazeTxt[x, y]);
  247.                         Console.ForegroundColor = ConsoleColor.Gray;
  248.  
  249.                 }
  250.                 Console.Write(Environment.NewLine);
  251.             }
  252.  
  253.         }
  254.     }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement