-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
83 lines (72 loc) · 2.62 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Astarfrompsuedo
{
class Program
{
static void Main(string[] args)
{
string[] map = new string[]
{
"+------+",
"| |",
"|A X |",
"| XX |",
"| X |",
"| B |",
"| |",
"+------+",
};
foreach (var line in map)
Console.WriteLine(line);
Node current = null;
Node start = methods.newnode(1, 2);
Node target = methods.newnode(2, 5);
List<Node> openList = new List<Node>();
List<Node> closedList = new List<Node>();
int g = 0;
openList.Add(start);
while (openList.Count > 0)
{
int lowest = openList.Min(l => l.F);
current = openList.First(l => l.F == lowest);
closedList.Add(current);
openList.Remove(current);
if (closedList.FirstOrDefault(l => l.X == target.X && l.Y == target.Y) != null)
break;
List<Node> Neighbours = methods.GetNeighbours(current.X, current.Y, map);
g++;
foreach (Node neighbour in Neighbours)
{
if (closedList.FirstOrDefault(l => l.X == neighbour.X
&& l.Y == neighbour.Y) != null)
continue;
if (openList.FirstOrDefault(l => l.X == neighbour.X
&& l.Y == neighbour.Y) == null)
{
neighbour.G = g;
neighbour.H = methods.GetH(neighbour.X, neighbour.Y, target.X, target.Y);
neighbour.F = neighbour.G + neighbour.H;
neighbour.Parent = current;
openList.Insert(0, neighbour);
}
else
{
if (g + neighbour.H < neighbour.F)
{
neighbour.G = g;
neighbour.F = neighbour.G + neighbour.H;
neighbour.Parent = current;
}
}
}
}
if (current != null)
{
Console.WriteLine("Pathfound");
}
Console.ReadLine();
}
}
}