public Node(Board b, BoardUI UI) { Board = b; Handle = this; Location = Seek(); CheckedBoard = new bool[Board.Width, Board.Height]; All = new Dictionary<Point,Node>(); Node.UI = UI; }
public Path(Node start, Node end) { Start = start; End = end; AddPoint(Start.Location); if (end != null) { AddPoint(end.Location); } }
private void GenerateMatrix(Node Prev, Point Aqui, Path Path) { if (CheckedBoard == null) { CheckedBoard = new bool[Board.Width, Board.Height]; } CheckedBoard[Aqui.X, Aqui.Y] = true; if (Path == null) { Path = new Path(this, null); } if (All.ContainsKey(Aqui) && (!Prev.Location.Equals(Aqui) || Path.Points.Count > 1)) { Node node; All.TryGetValue(Aqui, out node); Path.AddEnd(node); Prev.Paths.Add(Path); node.Paths.Add(Path.Reverse()); //foreach (Point Point in Path.Points) //{ // UI.RegisterSprite(new StaticSprite(new Element { X = Point.X, Y = Point.Y }, "fruit/banana", 20)); //} return; } if (SurroundingFreeSpaces(Aqui) > 2 && !Path.Start.Location.Equals(Aqui) && !All.ContainsKey(Aqui)) { //UI.RegisterSprite(new StaticSprite(new Element { X = Aqui.X, Y = Aqui.Y }, "fruit/orange", 20)); Node End = new Node(Aqui); All.Add(Aqui, End); //Path.AddEnd(End); //Prev.Paths.Add(Path); //End.Paths.Add(Path.Reverse()); //foreach (Point Point in Path.Points) //{ // UI.RegisterSprite(new StaticSprite(new Element { X = Point.X, Y = Point.Y }, "fruit/cherry", 20)); //} End.GenerateMatrix(End, End.Location, null); return; } for (double i = 0; i < 2 * MathHelper.Pi; i+= MathHelper.Pi/2) { Point Next = new Point(Aqui.X + (int)Math.Round(Math.Sin(i)), Aqui.Y + (int)Math.Round(Math.Cos(i))); if(IsInBounds(Next) && !Board.IsType(Enum.ElementTypes.Wall, Next) && !CheckedBoard[Next.X, Next.Y]){ GenerateMatrix(Prev, Next, Aqui.Equals(Location)?Path.Diverge():Path.Diverge(Aqui)); } } }
private void BuildPath(InstructionSet IS, ElementTypes Type, Node Start) { //AlreadySearched.Add(this); //Random rnd = new Random(); //for (int i = IS.Last.Paths.Count; i > 1; i--) //{ // int pos = rnd.Next(i); // var x = IS.Last.Paths[i - 1]; // IS.Last.Paths[i - 1] = IS.Last.Paths[pos]; // IS.Last.Paths[pos] = x; //} //for (int i = 0; i < IS.Last.Paths.Count; i++ ) //{ // Path Path = IS.Last.Paths[i]; // if (Path.ContainsType(Type, Board) && (Shortest == null || Shortest.distance > IS.distance)) // { // Shortest = IS.AddPath(Path); // continue; // } // if (AlreadySearched.Contains(Path.End)) // { // continue; // } // Path.End.BuildPath(IS.AddPath(Path), Type); //} //AlreadySearched.Remove(this); Queue<Node> State = new Queue<Node>(); List<Node> Traveled = new List<Node>(); Queue<InstructionSet> Instructions = new Queue<InstructionSet>(); Traveled.Add(Start); State.Enqueue(Start); Instructions.Enqueue(IS); while (State.Count > 0) { Node T = State.Dequeue(); InstructionSet How = Instructions.Dequeue(); foreach (Path P in T.Paths) { if (P.ContainsType(Type, Board)) { InstructionSet Found = How.AddPath(P); if (Shortest == null || Found.distance < Shortest.distance) { Shortest = Found; } continue; } Node N = P.End; if (!Traveled.Contains(N)) { Traveled.Add(N); State.Enqueue(N); Instructions.Enqueue(How.AddPath(P)); } } } }
public void AddEnd(Node End) { this.End = End; AddPoint(End.Location); }