Advertisement
Guest User

Untitled

a guest
Jul 16th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7.     public class Node
  8.     {
  9.         public readonly int x, y;
  10.         public int Hcost, Gcost;
  11.         bool Blockade;
  12.         public Node Parent;
  13.         public int FCost
  14.         {
  15.             get
  16.             {
  17.                 return Hcost + Gcost;
  18.             }
  19.         }
  20.  
  21.         public bool isBlockade()
  22.         {
  23.             return Blockade;
  24.         }
  25.  
  26.         public void SetToBlockade()
  27.         {
  28.             Blockade = true;
  29.         }
  30.  
  31.         public bool IsEqualTo(Node node)
  32.         {
  33.             return this.x == node.x && this.y == node.y;
  34.         }
  35.  
  36.         public Node(int x, int y)
  37.         {
  38.             this.x = x;
  39.             this.y = y;
  40.         }
  41.     }
  42.  
  43.     static List<Node> allNodes = new List<Node>();
  44.     static List<Node> openList = new List<Node>(), closedList = new List<Node>(), blockadesList = new List<Node>();
  45.     static Node current, StartNode, EndNode;
  46.     static int Width = 20, Height = 10;
  47.  
  48.     public static void Main()
  49.     {
  50.         //create nodes
  51.         for (int y = 1; y <= Height; y++)
  52.         {
  53.             for(int x = 1; x <= Width; x++)
  54.             {
  55.                 Node n = new Node(x, y);
  56.                 allNodes.Add(n);
  57.                 if (x == 1 && y == 1) StartNode = n;
  58.                 else if (x == Width && y == Height) EndNode = n;
  59.             }
  60.         }
  61.         openList.Add(StartNode);
  62.         while(openList.Count > 0)
  63.         {
  64.             current = openList[0];
  65.             for (int y = current.y + 1; y >= current.y - 1; y--)
  66.             {
  67.                 for (int x = current.x - 1; x <= current.x + 1; x++)
  68.                 {
  69.                     if (!(x == current.x && y == current.y) && y > 0 && y <= Height && x > 0 && x <= Width)
  70.                     {
  71.                         Node searchedNode = allNodes.Find(item => item.x == x && item.y == y);
  72.                         if (!openList.Contains(searchedNode) && !closedList.Contains(searchedNode) && !searchedNode.isBlockade())
  73.                         {
  74.                             //set parent
  75.                             searchedNode.Parent = current;
  76.                             if (searchedNode.IsEqualTo(EndNode))
  77.                             {
  78.                                 Console.WriteLine("i found way");
  79.                                 break;
  80.                             }
  81.                             //calc Gcost
  82.                             if (y == current.y + 1 && x == current.x - 1 ||
  83.                                 y == current.y + 1 && x == current.x + 1 ||
  84.                                 y == current.y - 1 && x == current.x - 1 ||
  85.                                 y == current.y + 1 && x == current.x + 1) searchedNode.Gcost = current.Gcost + 14;
  86.                             else searchedNode.Gcost = current.Gcost + 10;
  87.                             //calc Hcost
  88.                             if (y >= current.y)
  89.                             {
  90.                                 if (x >= current.x)
  91.                                 {
  92.                                     searchedNode.Hcost = (y - current.y) + (x - current.x);
  93.                                 }
  94.                                 else
  95.                                 {
  96.                                     searchedNode.Hcost = (y - current.y) + (current.x - x);
  97.                                 }
  98.                             }
  99.                             else
  100.                             {
  101.                                 if (x >= current.x)
  102.                                 {
  103.                                     searchedNode.Hcost = (current.y - y) + (x - current.x);
  104.                                 }
  105.                                 else
  106.                                 {
  107.                                     searchedNode.Hcost = (current.y - y) + (current.x - x);
  108.                                 }
  109.                             }
  110.  
  111.                             openList.Add(searchedNode);
  112.                         }
  113.                     }
  114.                     else Console.WriteLine("this node doesnt exist!");
  115.                 }
  116.             }
  117.             closedList.Add(current);
  118.             openList.Remove(current);
  119.             openList = openList.OrderBy(f => f.FCost).ToList(); //sort list by Fcost
  120.  
  121.             //check that some nodes didnt have the same Fcost
  122.             for (int i = 0; i < openList.Count; i++)
  123.             {
  124.                 for (int y = 0; y < openList.Count; y++)
  125.                 {
  126.                     if (!openList[i].IsEqualTo(openList[y]) && openList[i].FCost == openList[y].FCost)
  127.                     {
  128.                         if (openList[i].Hcost <= current.Hcost) openList[0] = openList[i];
  129.                         else openList[0] = openList[y];
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.         //create path
  135.         Node NextNode;
  136.         List<Node> Path = new List<Node>();
  137.         NextNode = current;
  138.         while(NextNode.Parent != null)
  139.         {
  140.             Path.Add(NextNode);
  141.             NextNode = NextNode.Parent;
  142.         }
  143.         for (int y = Height; y >= 1; y--)
  144.         {
  145.             for (int x = 1; x <= Width; x++)
  146.             {
  147.                 Node DrawedNode = new Node(x, y);
  148.                 if (x == StartNode.x && y == StartNode.y) Console.Write("A");
  149.                 else if (x == EndNode.x && y == EndNode.y) Console.Write("B");
  150.                 else if (Path.Contains(DrawedNode)) Console.Write("O");
  151.                 else if (blockadesList.Contains(DrawedNode)) Console.Write("X");
  152.                 else Console.Write(".");
  153.             }
  154.             Console.WriteLine();
  155.         }
  156.         Console.Write(openList.Count);
  157.         Console.Read();
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement