Play the real-time strategy, RPG and other types of game friends will know that when we use the mouse to select some units and command them to reach the location on the map, these units can always automatically choose the shortest path to arrive. This time we will associate the famous A * seek algorithm, the following is a brief introduction to the principle of implementation, and the C # implementation method.
Algorithm principle See: http://data.gameres.com/message.asp?TopicID=25439
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Collections; Namespace ConsoleApplication2 {class Program {static void Main (string[] args) {test
mytest = new test ();
Define departure Point pa = new points ();
pa.x = 1;
PA.Y = 1;
Define Destination Point PB = new Point ();
pb.x = 8;
PB.Y = 8; MyTest.
Findway (PA, Pb); MyTest.
Printmap ();
Console.ReadLine (); The class Test {//array is passed in 1, 0 represents the obstacle byte[,] R = new byte[10, 10] {1, 1, 1, 1,
1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 1, 1, 1, 1}, { 1, 1, 1, 1, 0, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1
}
};
Open list list<point> open_list = new list<point> ();
Close list list<point> close_list = new list<point> ();
Find the node with the lowest F value from the Open list Private point getminffromopenlist () {point Pmin = null; foreach (point P in Open_list) if (Pmin==null | |
PMIN.G + Pmin.H > p.g + p.H) Pmin = p;
return Pmin; //Determine if a point is a barrier private bool Isbar (points P, byte[,] map) {if (map[p.y, p.x] = 0) re
Turn true;
return false; //Determine if the close list contains a coordinate dot private bool Isincloselist (int x, int y) {foreach (Point P in C
lose_list) if (p.x = = x && p.y = y) return true;
return false; ///From the closed list to return the coordinates of the dot private point GETPOINTFROMCLoselist (int x, int y) {foreach (point P in Close_list) if (p.x = = x && p.y = y) return p;
return null; //Determine if the open list contains a coordinate dot private bool Isinopenlist (int x, int y) {foreach (Point P in Op
en_list) if (p.x = = x && p.y = y) return true;
return false; ///From the open list to return the coordinates of the dot private point getpointfromopenlist (int x, int y) {foreach P
In open_list) if (p.x = = x && p.y = y) return p;
return null;
//Compute a point's G value private int GETG (points p) {if (P.father = null) return 0;
if (p.x = = P.father.x | | | p.y = = P.FATHER.Y) return P.FATHER.G + 10;
else return P.FATHER.G + 14; //Compute the H value of a point private int geth (points p, point Pb) {return Math.Abs (p.x-pb.x) + Math .
Abs (P.Y-PB.Y); //Check node PR near the current nodeivate void CheckP8 (Point p0, byte[,] map, point PA, ref. pb) {for (int xt = p0.x-1; XT <= P 0.x + 1;
xt++) {for (int yt = p0.y-1; YT <= p0.y + 1; yt++) { Excludes points that exceed the bounds and equals themselves (XT >= 0 && xt < >= && yt 0 && yt <) &A mp;&!
(XT = = p0.x && YT = p0.y)) {///Remove barriers and close list of points if (Map[yt, XT]!= 0 &&!
Isincloselist (XT, YT)) {if (Isinopenlist (XT, YT))
{Point pt = getpointfromopenlist (XT, YT);
int g_new = 0; if (p0.x = = Pt.x | | p0.y = = PT.Y) g_new = P0.
G + 10; else G_new = P0.
G + 14; if (G_new < Pt.
G {Open_list.remove (PT);
Pt.father = P0; Pt.
G = g_new;
Open_list.add (PT);
}} else {
Not on the open list Point pt = new Point ();
Pt.x = XT;
Pt.y = YT;
Pt.father = P0; Pt.
G = GETG (PT); Pt.
H = Geth (PT, PB);
Open_list.add (PT); }}}}} public void Findway (P
Oint PA, point pb) {Open_list.add (PA); while (!) ( Isinopenlist (pb.x, PB. Y) | |
Open_list.count = = 0)) {Point p0 = Getminffromopenlist ();
if (P0 = null) return;
Open_list.remove (P0);
Close_list.add (P0);
CheckP8 (P0, R, PA, ref PB);
Point P = getpointfromopenlist (pb.x, PB.Y);
while (P.father!= null) {p = p.father;
R[P.Y, p.x] = 3;
} public void Saveway (point pb) {point p = PB;
while (P.father!= null) {p = p.father;
R[P.Y, p.x] = 3;
} public void Printmap () {for (int a = 0; a < a++) {
for (int b = 0; b < b++) {if (r[a, b] = = 1) console.write ("█");
else if (r[a, b] = = 3) console.write ("★"); else if (r[a, b) == 4) Console.Write ("0");
else Console.Write ("");
} console.write ("\ n");
}} class Point {public int y;
public int x;
public int G;
public int H; Public point () {} (int x0, int y0, int G0, int H0, point F) {x =
x0;
y = y0;
G = G0;
H = H0;
Father = F;
Public point Father; }
}