Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp2
  5. {
  6.     class Program
  7.     {
  8.         static Cell[,] map = new Cell[10, 10];
  9.         static List<Cell> ClosedList = new List<Cell>();
  10.         static void Main(string[] args)
  11.         {   int start_x = int.Parse(Console.ReadLine());
  12.             int start_y = int.Parse(Console.ReadLine());
  13.             int end_x = int.Parse(Console.ReadLine());
  14.             int end_y = int.Parse(Console.ReadLine());
  15.             Cell start_cell = map[start_x, start_y];
  16.             Cell end_cell = map[end_x, end_y];
  17.             for (int i = 0; i < 10; i++)
  18.             {
  19.                 for (int j = 0; j < 10; j++)
  20.                 {
  21.                     map[j, i] = new Cell('#', ConsoleColor.White);
  22.                     Cell k = map[j, i];
  23.                     k.H = Math.Abs(j - end_x) + Math.Abs(i - end_y);
  24.                 }
  25.             }            
  26.             Cell[] er = GetNearby(9, 9);
  27.             foreach (var u in er)
  28.              {
  29.              u.IsLocked = true;
  30.              }
  31.             //PutBlocks(3);
  32.             Render();
  33.             Console.ReadLine();
  34.         }
  35.         static void Render()
  36.         {
  37.             for (int i = 0; i < 10; i++)
  38.             {
  39.                 for (int j = 0; j < 10; j++)
  40.                 {
  41.                     Cell cell = map[j, i];
  42.                     Console.SetCursorPosition(j, i);
  43.                     if (cell.IsLocked)
  44.                     {
  45.                         Console.ForegroundColor = ConsoleColor.Red;
  46.                     }
  47.                     else if (cell.IsPath)
  48.                     {
  49.                         Console.ForegroundColor = ConsoleColor.Yellow;
  50.                     }
  51.                     else
  52.                     {
  53.                         Console.ForegroundColor = cell.Color;
  54.                     }
  55.  
  56.                     Console.Write(cell.Symbol);
  57.  
  58.                 }
  59.             }
  60.         }
  61.         static void FindPath(int x1, int y1, int x2, int y2)
  62.         {
  63.             List<Cell> OpenList = new List<Cell>(GetNearby(x1,y1));
  64.             List<Cell> ClosedList = new List<Cell>();
  65.             OpenList.AddRange(GetNearby(x1, y1));
  66.             OpenList.Remove(map[x1, y1]);
  67.             ClosedList.Add(map[x1, y1]);
  68.             Cell start = map[x1, y1];
  69.             start.IsPath= true;
  70.         }
  71.         static void PutBlocks (int number)
  72.         {
  73.             Random rand = new Random();
  74.             int x = rand.Next(4, 7);
  75.             int y = rand.Next(2, 6);
  76.             Cell k = map[x,y];
  77.             k.IsLocked = true;
  78.             for (int i = 0; i < number; i++)
  79.             {
  80.                 Cell ki = map[x, y + i];
  81.                 ki.IsLocked = true;
  82.             }
  83.         }
  84.         static Cell[] GetNearby(int x , int y)
  85.         {
  86.            
  87.                 List<Cell> NearbyCell = new List<Cell>();
  88.                 int xi = 0, yi = 0;
  89.             Cell StartPoint = map[x, y];
  90.             if (StartPoint.IsLocked)
  91.             {
  92.                 Console.WriteLine("не та точка");
  93.             }
  94.             else
  95.             {
  96.                 for (xi = x - 1; xi <= x + 1; xi++)
  97.                 {
  98.                     for (yi = y - 1; yi <= y + 1; yi++)
  99.                     {
  100.                         if (xi < 0 || yi < 0 || xi > 9 || yi > 9)
  101.                         {
  102.                             continue;
  103.                         }
  104.                         else
  105.                         {
  106.                             Cell k = map[xi, yi];
  107.  
  108.                             if (k.IsLocked == false)
  109.                             {
  110.                                 k = map[xi, yi];
  111.                                 k.Parent = map[x, y];
  112.                                 NearbyCell.Add(k);
  113.                             }
  114.                         }
  115.  
  116.                     }
  117.                 }
  118.             }
  119.          return NearbyCell.ToArray();        
  120.         }
  121.     }
  122.     class Cell
  123.     {
  124.         public int F, G, H;
  125.         public char Symbol = '@';
  126.         public ConsoleColor Color;
  127.         public bool IsLocked = false;
  128.         public bool IsPath = false;
  129.         public Cell Parent;
  130.        
  131.         public Cell(char symbol, ConsoleColor color)
  132.         {
  133.             Symbol = symbol;
  134.             Color = color;
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement