Advertisement
Guest User

Untitled

a guest
May 27th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace AI_Lab1.lib
  8. {
  9.     public interface IPathFinder
  10.     {
  11.         Result FindPath(Field field, Coordinate start, Coordinate target, int depth = 4);
  12.     }
  13.  
  14.     public class PathFinder : IPathFinder
  15.     {
  16.         public Result FindPath(Field field, Coordinate start, Coordinate target, int depth = 8)
  17.         {
  18.             var openNodesCount = 0;
  19.             int maxDepth = depth;
  20.             while(!ReachTarget(field, start, target, new LinkedList<Diff>(), maxDepth, ref openNodesCount, 0))
  21.             {
  22.                 maxDepth++;
  23.                 Console.WriteLine(maxDepth);
  24.             }
  25.             return null;
  26.         }
  27.  
  28.         private bool ReachTarget(Field field
  29.             , Coordinate current
  30.             , Coordinate target
  31.             , LinkedList<Diff> path
  32.             , int maxDepth
  33.             , ref int openNodesCount
  34.             , int depth = 0)
  35.         {
  36.             openNodesCount++;
  37.  
  38.             // Рассчитать возможные ходы и положить в массив
  39.             List<Coordinate> potentialPositions = new List<Coordinate>
  40.             {
  41.                 new Coordinate { X = current.X + 2, Y = current.Y + 1},
  42.                 new Coordinate { X = current.X + 1, Y = current.Y + 2},
  43.                 new Coordinate { X = current.X - 2, Y = current.Y + 1},
  44.                 new Coordinate { X = current.X - 1, Y = current.Y + 2},
  45.                 new Coordinate { X = current.X + 2, Y = current.Y - 1},
  46.                 new Coordinate { X = current.X + 1, Y = current.Y - 2},
  47.                 new Coordinate { X = current.X - 2, Y = current.Y - 1},
  48.                 new Coordinate { X = current.X - 1, Y = current.Y - 2},
  49.             };
  50.  
  51.             var lastDiff = path.Last;
  52.             if (lastDiff != null)
  53.             {
  54.                 var prevCoord = lastDiff.Value.Act.PrevCoord;
  55.                 potentialPositions.RemoveAll(x => x.X == prevCoord.X && x.Y == prevCoord.Y); //.Remove(lastDiff.Value.Act.PrevCoord);
  56.             }
  57.  
  58.             //var newState = field;
  59.             // Пройтись циклом по массиву и посмотреть, что будет
  60.             for (int i = 0; i < potentialPositions.Count && depth < maxDepth; i++)
  61.             {
  62.                 if (field.CanMove(potentialPositions[i], target, PlaceStates.Horse))
  63.                 {
  64.                     var newState = field.MoveFigure(current, potentialPositions[i], PlaceStates.Horse);
  65.                     var diff = new Diff
  66.                     {
  67.                         Act = new Act { NewCoord = potentialPositions[i], PrevCoord = current },
  68.                         Field = newState
  69.                     };
  70.  
  71.                     //Console.WriteLine("--------------------");
  72.                     //Console.WriteLine(newState.ToString());
  73.                     //Console.WriteLine($"from ({diff.Act.PrevCoord.X};{diff.Act.PrevCoord.Y}) to ({diff.Act.NewCoord.X};{diff.Act.NewCoord.Y})");
  74.                     //Console.WriteLine("--------------------");
  75.  
  76.                     // Добавить этап пройденного пути
  77.                     path.AddLast(diff);
  78.  
  79.                     // Фигура на позиции, цель достигнута
  80.                     if (newState.IsOnPosition(PlaceStates.Horse, target))
  81.                     {                        
  82.                         return true;
  83.                     }
  84.  
  85.                     if (depth < maxDepth)
  86.                     {
  87.                         var targetReached = ReachTarget(newState
  88.                             , potentialPositions[i]
  89.                             , target
  90.                             , path
  91.                             , maxDepth
  92.                             , ref openNodesCount
  93.                             , depth + 1);
  94.  
  95.                         if (targetReached)
  96.                         {
  97.                             return true;
  98.                         }
  99.                     }
  100.                 }                
  101.             }
  102.             if (path.Count > 0)
  103.             {
  104.                 path.RemoveLast();
  105.             }    
  106.             return false;
  107.  
  108.             throw new NotImplementedException();
  109.         }
  110.     }
  111.  
  112.     public class Result
  113.     {
  114.         public LinkedList<Diff> Path { get; set; }
  115.         public int OpenNodes { get; set; }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement