static void astar(String[] args) { XmlDocument doc = new XmlDocument(); doc.Load("c:/njones/src/SkippingRock/weewar.net/map.xml"); WeewarMap map = new WeewarMap(doc.DocumentElement); AStarSearch astarsearch = new AStarSearch(); bool[] path = new bool[map.Height * map.Width]; Unit u = new Unit(); u.Type = UnitType.Trooper; List<Terrain> bases = map.getTerrainsByType(TerrainType.Base); Coordinate cs = bases[0].Coordinate; Coordinate ce = bases[bases.Count-1].Coordinate; SticksNode start = new SticksNode(map, cs, u); SticksNode end = new SticksNode(map, ce, u); astarsearch.SetStartAndGoalStates(start, end); SearchState searchState; uint searchSteps = 0; do { searchState = astarsearch.SearchStep(); searchSteps++; } while (searchState == SearchState.Searching); if (searchState == SearchState.Succeeded) { Console.WriteLine("Search found goal state"); SticksNode node = astarsearch.GetSolutionStart() as SticksNode; Console.WriteLine( "Displaying solution"); int steps = 0; for (; ; ) { node = astarsearch.GetSolutionNext() as SticksNode; if (node == null) { break; } path[node.Coordinate.Y * map.Height + node.Coordinate.X] = true; //node.PrintNodeInfo(); steps++; }; for (int y = 0; y < map.Height; y++) { if (y % 2 == 1) Console.Write(" "); for (int x = 0; x < map.Width; x++) { if (x == start.Coordinate.X && y == start.Coordinate.Y) Console.Write("S"); else if (x == end.Coordinate.X && y == end.Coordinate.Y) Console.Write("G"); else Console.Write(path[y * map.Height + x] ? "x" : "o"); } Console.WriteLine(); } Console.WriteLine("Solution steps {0}", steps); // Once you're done with the solution you can free the nodes up astarsearch.FreeSolutionNodes(); } else if (searchState == SearchState.Failed) { Console.WriteLine("Search terminated. Did not find goal state"); } // Display the number of loops the search went through Console.WriteLine("searchSteps : " + searchSteps); }
public SticksNode(WeewarMap map, Coordinate coord, Unit unit) { _map = map; _coord = coord; _unit = unit; }
public SticksNode(WeewarMap map, Unit unit) : this(map, unit.Coordinate, unit) { }
private Coordinate getMovementCoordinate(Game game, Unit unit, List<Coordinate> possibleMovementCoordinates) { List<Coordinate> coords = possibleMovementCoordinates; while (coords.Count != 0) { int n = dice(coords.Count); Coordinate c = coords[n - 1]; //Console.WriteLine( " "+".. trying to move to "+c+" =>"+ game.getUnit( c )); if (game.getUnit(c) == null) return c; else coords.Remove(c); } return null; }
private Coordinate getAttackCoordinate(Game game, Unit unit, Coordinate from) { List<Coordinate> coords = eliza.GetAttackCoords(game.Id, from, unit.Type); if (coords.Count > 0) { int n = dice(coords.Count); return coords[n - 1]; } return null; }
private Game parseGame(XmlElement gameEle) { Game g = new Game(); String att = gameEle.GetAttribute("inNeedOfAttention"); g.IsInNeedOfAttention = att == "true"; g.Name = gameEle.SelectSingleNode("name").InnerText; g.Id = Int32.Parse(gameEle.SelectSingleNode("id").InnerText); XmlNode map = gameEle.SelectSingleNode("map"); if (map != null && map.InnerText != null) g.MapId = Int32.Parse(map.InnerText); g.Link = gameEle.SelectSingleNode("url").InnerText; g.RequiresAnInviteAccept = g.Link.Contains("join"); if (gameEle.SelectSingleNode("factions") != null) { XmlNodeList factions = gameEle.SelectNodes("factions/faction"); foreach (XmlElement faction in factions) { Faction f = new Faction(); g.Factions.Add(f); f.PlayerName = faction.GetAttribute("playerName"); f.State = faction.GetAttribute("state"); if (!String.IsNullOrEmpty(faction.GetAttribute("credits"))) f.Credits = Int32.Parse(faction.GetAttribute("credits")); foreach (XmlElement unit in faction.SelectNodes("unit")) { Unit u = new Unit(); u.Coordinate = new Coordinate(Int32.Parse(unit.GetAttribute("x")), Int32.Parse(unit.GetAttribute("y"))); u.Type = Unit.ToType(unit.GetAttribute("type")); u.Finished = "true" == unit.GetAttribute("finished"); u.Quantity = Int32.Parse(unit.GetAttribute("quantity")); f.Units.Add(u); } foreach (XmlElement unit in faction.SelectNodes("terrain")) { Terrain t = new Terrain(); t.Coordinate = new Coordinate(Int32.Parse(unit.GetAttribute("x")), Int32.Parse(unit.GetAttribute("y"))); t.Type = Terrain.ToType(unit.GetAttribute("type")); t.Finished = "true" == unit.GetAttribute("finished"); f.Terrains.Add(t); } } } return g; }