/
Maze.cs
134 lines (133 loc) · 4.34 KB
/
Maze.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace PathFinder
{
class Maze
{
private List<Cell> StringPointers;
private Cell exitCell;
private Cell entryCell;
private List<Cell> points;
private string way;
public Maze(string mazepath)
{
this.way = string.Empty;
this.points = new List<Cell>();
this.StringPointers = new List<Cell>();
var filedata = File.ReadLines(mazepath).ToList();
Cell currentCell;
foreach (string s in filedata)
{
currentCell = new Cell(s[0]);
StringPointers.Add(currentCell);
for (int i = 1; i < s.Length; i++)
{
currentCell.right = new Cell(s[i]);
if (s[i] == 'e')
this.exitCell = currentCell.right;
if (s[i] == '*')
this.entryCell = currentCell.right;
if ("123456789".Contains(s[i]))
points.Add(currentCell.right);
currentCell.right.left = currentCell;
currentCell = currentCell.right;
}
}
Cell nextLineCurrentCell;
for (int i = 0; i < StringPointers.Count-1; i++)
{
currentCell = StringPointers[i];
nextLineCurrentCell = StringPointers[i + 1];
while (currentCell != null | nextLineCurrentCell != null)
{
currentCell.lower = nextLineCurrentCell;
nextLineCurrentCell.upper = currentCell;
currentCell = currentCell.right;
nextLineCurrentCell = nextLineCurrentCell.right;
}
}
}
public List<string> GetMaze()
{
List<string> maze = new List<string>();
foreach (Cell c in StringPointers)
{
maze.Add(c.GetLine());
}
return maze;
}
public List<string> GetDistances()
{
List<string> maze = new List<string>();
foreach (Cell c in StringPointers)
{
maze.Add(c.GetDistances());
}
return maze;
}
private void Locate(Cell cellPointer)
{
cellPointer.SetDistance(0);
}
public void FindBestWay()
{
List<Cell> unavailable = new List<Cell>();
Cell startCell = this.entryCell;
Cell finishCell = null;
bool target_found = false;
string partOfWay = string.Empty;
while (finishCell != this.exitCell)
{
this.Locate(startCell);
if (points.Count == 0)
finishCell = exitCell;
else
{
unavailable = points.Where(x => x.GetDistance() == int.MaxValue - 1).ToList();
foreach (Cell c in unavailable)
{
points.Remove(c);
}
points = points.OrderBy(x => x.GetDistance()).ToList();
finishCell = points.First();
points.Remove(finishCell);
}
finishCell.Walk(finishCell.GetDistance() + 1, ref partOfWay, ref target_found);
startCell = finishCell;
this.SetWay(partOfWay);
partOfWay = string.Empty;
target_found = false;
this.ResetDistances();
}
}
private void SetWay(string part)
{
this.way += part;
}
public string GetWay()
{
if (this.way.Length == 0)
return "There is no way out";
return this.way;
}
private void ResetDistances()
{
foreach (Cell c in this.StringPointers)
{
c.ResetDistances();
}
}
public void Print(List<string> printable)
{
foreach (string s in printable)
{
Console.WriteLine(s);
}
Console.WriteLine();
}
}
}