forked from onelei/Game-AI-Programming-Book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
185 lines (160 loc) · 6.04 KB
/
Program.cs
File metadata and controls
185 lines (160 loc) · 6.04 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace BFS
{
internal class Program
{
private static int[,] map;
public static void Main(string[] args)
{
map = new int[10,10]{ // 初始的map_maze
{ 1, 0, 3, 3, 3, 3, 3, 0, 3, 3 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 },
{ 0, 0, 0, 3, 0, 0, 3, 3, 3, 0 },
{ 3, 0, 0, 0, 0, 3, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 3, 0, 0, 0, 0, 0 },
{ 3, 0, 0, 0, 3, 0, 0, 3, 0, 3 },
{ 3, 0, 0, 0, 0, 3, 3, 0, 0, 0 },
{ 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 3, 3, 3, 0, 0, 3, 0, 3, 0, 3 },
{ 3, 0, 0, 0, 0, 3, 3, 3, 0, 3 },
};
getMapStrings();
BFS.InitMap(map);
List<Node> passNodes = new List<Node>();
int startX,startY;
int endX,endY;
getPoint(1,out startX,out startY);
getPoint(2,out endX,out endY);
Node start = new Node(startX,startY,map[startX,startY]);
Node end = new Node(endX,endY, map[endX,endY]);
BFS.Search(start,end,ref passNodes);
Console.WriteLine("\n === BFS 寻路结束 === " + (passNodes.Count>0));
Console.WriteLine("一共有 "+passNodes.Count +" 个中间节点");
for (int i = 0; i < passNodes.Count; i++)
{
int x = passNodes[i].X;
int y = passNodes[i].Y;
map[x, y] = 8;
}
PrintMapData();
getMapStrings();
/*
map = new int[10,10]{ // 初始的map_maze
{ 1, 0, 0, 3, 0, 3, 0, 0, 0, 0 },
{ 0, 0, 3, 0, 0, 3, 0, 3, 0, 3 },
{ 3, 0, 0, 0, 0, 3, 3, 3, 0, 3 },
{ 3, 0, 3, 0, 0, 0, 0, 0, 0, 3 },
{ 3, 0, 0, 2, 0, 3, 0, 3, 0, 3 },
{ 3, 0, 0, 3, 0, 0, 0, 3, 0, 3 },
{ 3, 0, 3, 0, 0, 3, 3, 0, 0, 0 },
{ 0, 0, 3, 0, 0, 0, 0, 0, 0, 0 },
{ 3, 3, 3, 0, 0, 3, 0, 3, 0, 3 },
{ 3, 0, 0, 0, 0, 3, 3, 3, 0, 3 },
};
*/
map = new int[10,10]{ // 初始的map_maze
{ 1, 0, 3, 3, 3, 3, 3, 0, 3, 3 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 },
{ 0, 0, 0, 3, 0, 0, 3, 3, 3, 0 },
{ 3, 0, 0, 0, 0, 3, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 3, 0, 0, 0, 0, 0 },
{ 3, 0, 0, 0, 3, 0, 0, 3, 0, 3 },
{ 3, 0, 0, 0, 0, 3, 3, 0, 0, 0 },
{ 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 3, 3, 3, 0, 0, 3, 0, 3, 0, 3 },
{ 3, 0, 0, 0, 0, 3, 3, 3, 0, 3 },
};
Console.WriteLine("DFS开始======================================");
PrintMapData();
getMapStrings();
DFS.InitMap(map);
passNodes.Clear();
getPoint(1,out startX,out startY);
getPoint(2,out endX,out endY);
start = new Node(startX,startY,map[startX,startY]);
end = new Node(endX,endY, map[endX,endY]);
DFS.Search(start,end,ref passNodes);
Console.WriteLine("\n === DFS 寻路结束 === " + (passNodes.Count>0));
Console.WriteLine("一共有 "+passNodes.Count +" 个中间节点");
for (int i = 0; i < passNodes.Count; i++)
{
int x = passNodes[i].X;
int y = passNodes[i].Y;
Console.Write(x+","+y+" ");
map[x, y] = 8;
}
PrintMapData();
getMapStrings();
}
private static void getPoint(int value,out int x,out int y)
{
x = 0;
y = 0;
int mapLength = map.GetLength(0);
int mapWidth = map.GetLength(1);
for (int i = 0; i < mapLength; i++)
{
for (int j = 0; j < mapWidth; j++)
{
if (map[i,j] == value)
{
x = i;
y = j;
return;
}
}
}
}
private static void PrintMapData()
{
int mapLength = map.GetLength(0);
int mapWidth = map.GetLength(1);
for (int i = 0; i < mapLength; i++)
{
for (int j = 0; j < mapWidth; j++)
{
Console.Write(map[i,j] + ", ");
}
Console.WriteLine();
}
}
private static void getMapStrings()
{
Console.WriteLine();
int mapLength = map.GetLength(0);
int mapWidth = map.GetLength(1);
string[,] result = new string[mapLength,mapWidth];
for (int i = 0; i < mapLength; i++)
{
for (int j = 0; j < mapWidth; j++)
{
switch (map[i, j])
{
case 0:
Console.Write(".");
break;
case 1:
Console.Write("S");
break;
case 2:
Console.Write("E");
break;
case 3:
Console.Write("@");
break;
case 4:
Console.Write(".");
break;
case 8:
Console.Write("A");
break;
}
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}