protected override void ProcessGame(Game detailed) { Console.WriteLine(); Console.WriteLine("Game: " + detailed.Name + " (" + detailed.Id + ")"); Console.WriteLine(" Getting Map info: " + detailed.MapId); WeewarMap wmap = eliza.GetMap(detailed.MapId); //Console.WriteLine( " Getting Map info: "+wmap.Terrains); Faction f = detailed.GetFactionByPlayerName(eliza.User); Console.WriteLine(" .. moving my dudes. "); foreach (Unit unit in f.Units) { Console.WriteLine("-----------------------------------------------------------------------------"); Console.WriteLine(" " + unit.Type + "(" + unit.Quantity + ") on " + unit.Coordinate); // repair if quantity below 5 if (!unit.Finished && unit.Quantity < 5) { String m = eliza.Repair(detailed.Id, unit.Coordinate); Console.WriteLine(" " + ".. repairing => " + m); unit.Finished = true; continue; } // // request movement coordinates List<Coordinate> possibleMovementCoordinates = eliza.GetMovementCoords(detailed.Id, unit.Coordinate, unit.Type); Util.Shuffle(possibleMovementCoordinates); possibleMovementCoordinates.Insert(0, unit.Coordinate); // check if there is a capturable base in range if (!unit.Finished && unit.CanCapture()) { Coordinate c = MatchFreeBase(possibleMovementCoordinates, wmap, detailed, f); if (c != null) { String m = eliza.MoveAttackCapture(detailed.Id, unit.Coordinate, c, null, true); unit.Coordinate = c; Console.WriteLine(" " + ".. moving to " + c + " and capturing =>" + m); unit.Finished = true; } } List<Coordinate> targets = getTargets(detailed, wmap, f); int minDistance = MinDistance(unit.Coordinate, targets); int n = 5; if (minDistance <= 5 && !unit.Finished) { // Different moving spots that will be evaluated // check for possible attack targets from one of the targets for (int i = 0; i < n && i < possibleMovementCoordinates.Count; i++) { Coordinate c = possibleMovementCoordinates[i]; Console.WriteLine(" " + ".. checking movement Option :" + c + " "); Coordinate a = getAttackCoordinate(detailed, unit, c); Console.WriteLine(" " + ".. attack coord :" + a + " "); if (a != null && detailed.getUnit(c) == null) { String m = eliza.MoveAttackCapture(detailed.Id, unit.Coordinate, c, a, false); Console.WriteLine(" " + ".. moving to " + c + " attacking " + a + " =>" + m); if (c != null) unit.Coordinate = c; unit.Finished = true; break; } } } if (!unit.Finished && possibleMovementCoordinates.Count > 1) { List<Coordinate> cities = getEnemyCities(detailed, wmap, f); AStarSearch s = new AStarSearch(); s.SetStartAndGoalStates(new SticksNode(wmap, unit), new SticksNode(wmap, cities[0], unit)); SearchState searchState; do { searchState = s.SearchStep(); } while (searchState == SearchState.Searching); if (searchState == SearchState.Succeeded) { } Util.Shuffle(targets); Util.Shuffle(cities); possibleMovementCoordinates.RemoveAt(0); while (possibleMovementCoordinates.Count > 5) possibleMovementCoordinates.RemoveAt(5); while (cities.Count > 3) cities.RemoveAt(3); while (targets.Count > 3) targets.RemoveAt(3); bool cnt = true; while (cnt) { Console.WriteLine(" " + ".. possible movement options: " + Coordinate.ToString(possibleMovementCoordinates)); Console.WriteLine(" " + ".. possible Targets: " + Coordinate.ToString(targets)); Coordinate c; if (unit.Type == UnitType.Trooper) c = getClosest(possibleMovementCoordinates, cities); else { c = getClosest(possibleMovementCoordinates, targets); if (c.Equals(unit.Coordinate) && targets.Count == 0 && possibleMovementCoordinates.Count > 1) c = possibleMovementCoordinates[1]; } if (!c.Equals(unit.Coordinate) && detailed.getUnit(c) == null) { String m = eliza.MoveAttackCapture(detailed.Id, unit.Coordinate, c, null, false); Console.WriteLine(" " + ".. moving to " + c + " =>" + m); unit.Coordinate = c; cnt = false; } else possibleMovementCoordinates.Remove(c); cnt = cnt && possibleMovementCoordinates.Count > 0; } } //Thread.sleep( 300 ); } if (f.Units.Count * 3 < (detailed.GetUnitCount() * 2) || f.Units.Count < 15) { Console.WriteLine(" Terrains :" + Terrain.ToString(f.Terrains)); if (f.Credits > 75) { foreach (Terrain terrain in f.Terrains) { Console.WriteLine(" " + terrain.Type + " on " + terrain.Coordinate + " finished:" + terrain.Finished + " unit: " + (detailed.getUnit(terrain.Coordinate) != null)); if (!terrain.Finished && detailed.getUnit(terrain.Coordinate) == null) { Console.WriteLine(" " + terrain.Type + " on " + terrain.Coordinate); List<UnitType> options = buildOptions[terrain.Type]; UnitType buildType; do { int nd = dice(options.Count); buildType = options[nd - 1]; } while (f.Credits < Unit.GetCost(buildType)); String x = eliza.Build(detailed.Id, terrain.Coordinate, buildType); Console.WriteLine(" .... building " + buildType + " " + x); f.Credits = (f.Credits - Unit.GetCost(buildType)); } } } } if (eliza.EndTurn(detailed.Id)) Console.WriteLine(" .. finished turn."); else Console.WriteLine(" .. failed to finish turn [" + eliza.GetLastResult() + "]"); }
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); }
// Main public static int main(string[] args) { Console.WriteLine("STL A* Search implementation\n(C)2001 Justin Heyes-Jones"); // Our sample problem defines the world as a 2d array representing a terrain // Each element contains an integer from 0 to 5 which indicates the cost // of travel across the terrain. Zero means the least possible difficulty // in travelling (think ice rink if you can skate) whilst 5 represents the // most difficult. 9 indicates that we cannot pass. // Create an instance of the search class... AStarSearch astarsearch = new AStarSearch(); bool[] path = new bool[MAP_HEIGHT * MAP_WIDTH]; // Create a start state MapSearchNode nodeStart = new MapSearchNode(); nodeStart.x = 0; nodeStart.y = 0; // Define the goal state MapSearchNode nodeEnd = new MapSearchNode(); nodeEnd.x = 19; nodeEnd.y = 19; // Set Start and goal states astarsearch.SetStartAndGoalStates(nodeStart, nodeEnd); SearchState searchState; uint searchSteps = 0; do { searchState = astarsearch.SearchStep(); searchSteps++; } while (searchState == SearchState.Searching); if (searchState == SearchState.Succeeded) { Console.Write("Search found goal state"); MapSearchNode node = astarsearch.GetSolutionStart() as MapSearchNode; Console.WriteLine( "Displaying solution"); int steps = 0; node.PrintNodeInfo(); for (; ; ) { node = astarsearch.GetSolutionNext() as MapSearchNode; if (node == null) { break; } path[node.y * MAP_HEIGHT + node.x] = true; //node.PrintNodeInfo(); steps++; }; for (int y = 0; y < MAP_HEIGHT; y++) { for (int x = 0; x < MAP_WIDTH; x++) { 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); return 0; }