This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.cs
94 lines (83 loc) · 3.26 KB
/
DFS.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
using System;
using System.Linq;
namespace LevelGenerator
{
class DFS : PathFinding
{
public DFS(Dungeon _dungeon)
: base(_dungeon) {}
/// The DFS Algorithm.
public int FindRoute(
ref Random _rand
) {
openList.Add(start);
path.Add(start);
while (openList.Count > 0)
{
// Get the first
Location current = openList.First();
ValidateKeyRoom(current);
// Add the current square to the closed list
ClosedList.Add(current);
if (((map[current.X, current.Y] >= 0) && (map[current.X, current.Y] < 100)) || (map[current.X, current.Y] == 102))
{
NVisitedRooms++;
}
// Check if the actual room is a locked one. If it is, add 1 to the number of locks needed to reach the goal
foreach (var locked in allLocksLocation)
{
if (locked.X == current.X && locked.Y == current.Y)
{
NeededLocks++;
break;
}
}
// Remove it from the open list
openList.Remove(current);
// If we added the destination to the closed list, we've found a path
if (ClosedList.Count > 0)
{
if (ClosedList.FirstOrDefault(l => l.X == target.X && l.Y == target.Y) != null)
{
break;
}
}
// Check all adjacent squares from the current node
var adjacentSquares = GetWalkableAdjacentSquares(current.X, current.Y, sizeX, sizeY, map);
int value = _rand.Next();
adjacentSquares = adjacentSquares.OrderBy(X => value).ToList();
foreach (var adjacentSquare in adjacentSquares)
{
if (current.Parent == adjacentSquare)
{
adjacentSquares.Remove(adjacentSquare);
break;
}
}
foreach (var adjacentSquare in adjacentSquares)
{
// If this adjacent square is already in the closed list, ignore it
if (ClosedList.FirstOrDefault(l => l.X == adjacentSquare.X
&& l.Y == adjacentSquare.Y) != null)
{
continue;
}
// If it's not in the open list...
if (openList.FirstOrDefault(l => l.X == adjacentSquare.X
&& l.Y == adjacentSquare.Y) == null)
{
adjacentSquare.Parent = current;
// and add it to the open list and add to your path
openList.Insert(0, adjacentSquare);
path.Add(adjacentSquare);
}
else
{
adjacentSquare.Parent = current;
}
}
}
return NeededLocks;
}
}
}