Repository navigation
Expand file tree
/
Copy pathMap.cs
More file actions
327 lines (309 loc) · 10.7 KB
/
Copy pathMap.cs
File metadata and controls
327 lines (309 loc) · 10.7 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using SplashKitSDK;
namespace PathFinder
{
public class Map
{
private int _xBound;
private int _yBound;
private Cell _root;
private List<Cell> _goals;
private Cell[,] _cells;
private int _nCells;
public Map(StreamReader filePath)
{
// Create cells
string[] boundary = filePath.ReadLine().Split("x");
_xBound = Int32.Parse(boundary[1]);
_yBound = Int32.Parse(boundary[0]);
_nCells = _xBound * _yBound;
_cells = new Cell[_xBound, _yBound];
int y = 0;
while (y < _yBound)
{
int x = 0;
while (x < _xBound)
{
_cells[x, y] = new Cell(x, y);
x++;
}
y++;
}
Set_neighbours();
// set up inital location state
string[] initialLocation = Regex.Split(filePath.ReadLine(), @"\D");
int init_x = Int32.Parse(initialLocation[1]);
int init_y = Int32.Parse(initialLocation[2]);
_cells[init_x, init_y].Type = Type.ROOT;
_root = _cells[init_x, init_y];
// Set up goal states
_goals = new List<Cell>();
Set_goals(filePath.ReadLine());
// Set up wall states
string line;
List<string> walls = new List<string>();
while ((line = filePath.ReadLine()) != null)
{
walls.Add(line);
}
for (int i = 0; i < walls.Count(); i++)
{
Set_Walls(walls[i]);
}
}
// Properties
public Cell Root
{
get
{
return _root;
}
}
public List<Cell> Goals
{
get
{
return _goals;
}
}
public int xbound
{
get
{
return _xBound;
}
}
public int ybound
{
get
{
return _yBound;
}
}
public int nCells
{
get
{
return _nCells;
}
}
// Methods
public List<Cell> List_cells()
{
List<Cell> cells = new List<Cell>();
for (int i = 0; i < _xBound; i++)
{
for (int j = 0; j < _yBound; j++)
{
cells.Add(_cells[i,j]);
}
}
return cells;
}
private void Set_Walls(string wall)
{
// fileLine wall format (position X, position Y, wide, High)
string[] wall_info = Regex.Split(wall, @"\D+");
int x_start = Int32.Parse(wall_info[1]);
int y_start = Int32.Parse(wall_info[2]);
int wide = Int32.Parse(wall_info[3]);
int high = Int32.Parse(wall_info[4]);
int x_end = x_start + wide;
int y_end = y_start + high;
for (int i = x_start; i < x_end; i++) // horizontal
{
for (int j = y_start; j < y_end; j++) // vertcal
{
_cells[i, j].Type = Type.WALL;
}
}
}
private void Set_goals(string str_goals)
{
if (!str_goals.Contains("|"))
{
// single goal
string[] goals = Regex.Split(str_goals, @"\D+");
string x = goals[1].ToString().Trim();
string y = goals[2].ToString().Trim();
_cells[Int32.Parse(x), Int32.Parse(y)].Type = Type.GOAL;
_goals.Add(_cells[Int32.Parse(x), Int32.Parse(y)]);
}
else
{
// multiple goals separated with '|'
string[] goals = str_goals.Split("|");
foreach (string g in goals)
{
string[] goal = Regex.Split(g, @"\D+");
int x = Int32.Parse(goal[1]);
int y = Int32.Parse(goal[2]);
_cells[x, y].Type = Type.GOAL;
_goals.Add(_cells[x,y]);
}
}
}
private void Set_neighbours()
{
int y_index = 0;
while (y_index < _yBound)
{
int x_index = 0;
while (x_index < _xBound)
{
// by order of expansion UP DOWN LEFT RIGHT
Cell cells = _cells[x_index, y_index];
if (y_index != 0) // up
{
cells.AddNeighbour(_cells[x_index, y_index - 1]);
}
if (x_index != 0) // left
{
cells.AddNeighbour(_cells[x_index - 1, y_index]);
}
if (y_index != _yBound - 1) // down
{
cells.AddNeighbour(_cells[x_index, y_index + 1]);
}
if (x_index != _xBound - 1) // right
{
cells.AddNeighbour(_cells[x_index + 1, y_index]);
}
x_index += 1;
}
y_index += 1;
}
}
public Cell Get_neighbour(Cell c, string direction)
{
int x = 0;
int y = 0;
if (direction == "LEFT")
{
if (c.X != 0) // not most left side
{
x = c.X - 1;
y = c.Y;
return _cells[x,y];
}
}
if (direction == "RIGHT")
{
if (!(c.X == _xBound - 1)) // not most right cell
{
x = c.X + 1;
y = c.Y;
return _cells[x,y];
}
}
if (direction == "UP")
{
if (c.Y != 0) // not most top cell
{
x = c.X;
y = c.Y - 1;
return _cells[x,y];
}
}
if (direction == "DOWN")
{
if (!(c.Y == _yBound - 1)) // not most bottom cell
{
x = c.X;
y = c.Y + 1;
return _cells[x,y];
}
}
return null;
}
public double Distance(Cell c1, Cell c2)
{
// straight line distance between two points using pythagoras theorem
int x1 = c1.X;
int x2 = c2.X;
int y1 = c1.Y;
int y2 = c2.Y;
return Math.Sqrt(Math.Pow(x1-x2, 2) + Math.Pow(y1 - y2, 2));
}
public void Reset()
{
for (int i = 0; i < _xBound; i++)
{
for (int j = 0; j < _yBound; j++)
{
if (_cells[i,j].Type != Type.WALL)
{
if (_cells[i,j].Type != Type.ROOT)
{
if (_cells[i,j].Type != Type.GOAL)
{
_cells[i,j].Type = Type.ROUTE;
_cells[i,j].Parent = null;
}
}
}
}
}
}
public void Draw()
{
for (int i = 0; i < xbound; i++)
{
for (int j = 0; j < ybound; j++)
{
SplashKit.FillRectangle(Color.Black, i * 50, j * 50, 50, 50); // border
if (_cells[i,j].Type == Type.WALL)
{
SplashKit.FillRectangle(Color.Black, i * 50 + 1, j * 50 + 1, 49, 49);
}
if (_cells[i,j].Type == Type.ROUTE)
{
SplashKit.FillRectangle(Color.White, i * 50 + 1, j * 50 + 1, 49, 49);
}
if (_cells[i,j].Type == Type.ROOT)
{
SplashKit.FillRectangle(Color.BrightGreen, i * 50 + 1, j * 50 + 1, 49, 49);
}
if (_cells[i,j].Type == Type.GOAL)
{
SplashKit.FillRectangle(Color.Red, i * 50 + 1, j * 50 + 1, 49, 49);
}
if (_cells[i,j].Type == Type.EXPANDED)
{
SplashKit.FillRectangle(Color.DarkGray, i * 50 + 1, j * 50 + 1, 49, 49);
}
if (_cells[i,j].Type == Type.VISITED)
{
SplashKit.FillRectangle(Color.Yellow, i * 50 + 1, j * 50 + 1, 49, 49);
}
if (_cells[i,j].Type == Type.PATH)
{
SplashKit.FillRectangle(Color.DarkBlue, i * 50 + 1, j * 50 + 1, 49, 49);
}
}
}
}
public void draw_buttons()
{
SplashKit.FillRectangle(Color.LightBlue, 0, 500, 100, 100);
SplashKit.FillRectangle(Color.LightBlue, 120, 500, 100, 100);
SplashKit.FillRectangle(Color.LightBlue, 240, 500, 100, 100);
SplashKit.FillRectangle(Color.LightBlue, 360, 500, 100, 100);
SplashKit.FillRectangle(Color.LightBlue, 480, 500, 100, 100);
SplashKit.FillRectangle(Color.LightBlue, 600, 500, 100, 100);
SplashKit.DrawText("BFS", Color.Black, "italics", 100, 25, 550);
SplashKit.DrawText("DFS", Color.Black, "italics", 100, 145, 550);
SplashKit.DrawText("GBFS", Color.Black, "italics", 100, 265, 550);
SplashKit.DrawText("A*", Color.Black, "italics", 100, 385, 550);
SplashKit.DrawText("Strat 1", Color.Black, "italics", 100, 505, 550);
SplashKit.DrawText("Strat 2", Color.Black, "italics", 100, 625, 550);
}
}
}