private List <BodMapy> NajdiPruchoziBody(char[,] stavHriste, BodMapy start, BodMapy cil) { var moznosti = new List <BodMapy>() { new BodMapy { X = start.X, Y = start.Y - 1, Nadrazeny = start, Cena = start.Cena + 1 }, new BodMapy { X = start.X, Y = start.Y + 1, Nadrazeny = start, Cena = start.Cena + 1 }, new BodMapy { X = start.X - 1, Y = start.Y, Nadrazeny = start, Cena = start.Cena + 1 }, new BodMapy { X = start.X + 1, Y = start.Y, Nadrazeny = start, Cena = start.Cena + 1 }, new BodMapy { X = start.X - 1, Y = start.Y - 1, Nadrazeny = start, Cena = start.Cena + 1 }, new BodMapy { X = start.X + 1, Y = start.Y - 1, Nadrazeny = start, Cena = start.Cena + 1 }, new BodMapy { X = start.X - 1, Y = start.Y + 1, Nadrazeny = start, Cena = start.Cena + 1 }, new BodMapy { X = start.X + 1, Y = start.Y + 1, Nadrazeny = start, Cena = start.Cena + 1 }, }; moznosti.ForEach(BodMapy => BodMapy.NastavVzdalenost(cil.X, cil.Y)); for (int c = moznosti.Count - 1; c >= 0; c--) { if (VejdeSeNaHriste(moznosti[c].X, moznosti[c].Y)) { var b = stavHriste[moznosti[c].X, moznosti[c].Y]; if (b == ' ') { continue; } } moznosti.RemoveAt(c); } return(moznosti); }
public Point NajdiNejblizsiCil(Point polohaPsa, char[,] stavHriste) { var start = new BodMapy(); start.X = (int)this.poloha.X; start.Y = (int)this.poloha.Y; var cil = new BodMapy(); cil.X = (int)polohaPsa.X; cil.Y = (int)polohaPsa.Y; start.NastavVzdalenost(cil.X, cil.Y); var aktivniBody = new List <BodMapy>(); aktivniBody.Add(start); var navstivene = new List <BodMapy>(); while (aktivniBody.Any()) { var testBod = aktivniBody.OrderBy(x => x.CV).First(); if (testBod.X == cil.X && testBod.Y == cil.Y) { var bod = testBod; while (true) { if (stavHriste[bod.X, bod.Y] == ' ') { stavHriste[bod.X, bod.Y] = '@'; } bod = bod.Nadrazeny; if (bod == null) { /* StringBuilder sb = new StringBuilder(); * for (int i = 0; i < stavHriste.GetLength(0); i++) * { * for (int j = 0; j < stavHriste.GetLength(1); j++) * { * Console.Write(stavHriste[i,j]); * } * Console.Write(System.Environment.NewLine); * }*/ return(PrvniKrok(stavHriste, new Point(start.X, start.Y), 1)); } } } navstivene.Add(testBod); aktivniBody.Remove(testBod); var moznePosuny = NajdiPruchoziBody(stavHriste, testBod, cil); foreach (var mp in moznePosuny) { if (navstivene.Any(x => x.X == mp.X && x.Y == mp.Y)) { continue; } //It's already in the active list, but that's OK, maybe this new tile has a better value (e.g. We might zigzag earlier but this is now straighter). if (aktivniBody.Any(x => x.X == mp.X && x.Y == mp.Y)) { var exBod = aktivniBody.First(x => x.X == mp.X && x.Y == mp.Y); if (exBod.CV > testBod.CV) { aktivniBody.Remove(exBod); aktivniBody.Add(mp); } } else { aktivniBody.Add(mp); } } } return(new Point(-1, -1)); }