예제 #1
0
    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);

    }
예제 #2
0
 public SticksNode(WeewarMap map, Coordinate coord, Unit unit)
 {
   _map = map;
   _coord = coord;
   _unit = unit;
 }
예제 #3
0
 public SticksNode(WeewarMap map, Unit unit) : this(map, unit.Coordinate, unit) { }
예제 #4
0
    private Coordinate MatchFreeBase(List<Coordinate> coords, WeewarMap wmap, Game g, Faction myFaction)
    {

      //Console.WriteLine("Coords:"+coords );
      foreach (Coordinate c in coords)
      {
        Terrain t = wmap.get(c);
        //Console.WriteLine("type :"+t.Type );
        Faction owner = g.getTerrainOwner(c);
        if (t.Type == TerrainType.Base && (owner == null || owner != myFaction) && g.getUnit(c) == null)
          return c;
      }
      return null;
    }
예제 #5
0
 private List<Coordinate> getEnemyCities(Game game, WeewarMap wmap, Faction myFaction)
 {
   List<Coordinate> targets = new List<Coordinate>();
   List<Terrain> bases = wmap.getTerrainsByType(TerrainType.Base);
   foreach (Terrain ter in bases)
     if (game.getTerrainOwner(ter.Coordinate) != myFaction)
       targets.Add(ter.Coordinate);
   return targets;
 }
예제 #6
0
 private List<Coordinate> getTargets(Game game, WeewarMap wmap, Faction myFaction)
 {
   List<Coordinate> targets = new List<Coordinate>();
   foreach (Faction faction in game.Factions)
   {
     if (faction == myFaction)
       continue;
     foreach (Unit otherUnit in faction.Units)
       targets.Add(otherUnit.Coordinate);
   }
   return targets;
 }