Advertisement
StreetKatya

libHero

Feb 8th, 2022
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using CommonPart;
  5.  
  6. namespace herodll
  7. {
  8.     public class Hero : IHero
  9.     {
  10.         private int heroX = 0;
  11.         private int heroY = 0;
  12.         public ILabirint Maze { get; set; }
  13.         private Stack<int> stackWay = new Stack<int>();
  14.         private Stack<int> stackCrossWay = new Stack<int>();
  15.  
  16.         private List<List<int>> usedCells = new List<List<int>>();
  17.         private bool onExit = false;
  18.  
  19.         public void GameOver(object sender, EventArgs e) //2 - exit
  20.         {
  21.             onExit = true;
  22.         }
  23.         public void CrossWay()
  24.         {
  25.             int countCrossWay = 0;
  26.             if (Maze.IsDownFree())
  27.             {
  28.                 countCrossWay++;
  29.             }
  30.             if (Maze.IsLeftFree())
  31.             {
  32.                 countCrossWay++;
  33.             }
  34.             if (Maze.IsRightFree())
  35.             {
  36.                 countCrossWay++;
  37.             }
  38.             if (Maze.IsUpFree())
  39.             {
  40.                 countCrossWay++;
  41.             }
  42.             if (countCrossWay > 2)
  43.             {
  44.                 stackCrossWay.Push(stackWay.Count);
  45.             }
  46.         }
  47.         //right - 1  down - 2  left - 3 up - 4
  48.         public void SearchPath() //1 - wall, 0 - road
  49.         {
  50.             Maze.OnExit += GameOver;
  51.             while (onExit == false)
  52.             {
  53.                 CrossWay();
  54.                 if(Maze.IsRightFree() && stackWay.Peek() != 3)
  55.                 {
  56.                     heroX++;
  57.                     stackWay.Push(1);
  58.                     Maze.MoveRight();
  59.                 }
  60.                 else if(Maze.IsDownFree() && stackWay.Peek() != 4)
  61.                 {
  62.                     heroY--;
  63.                     stackWay.Push(2);
  64.                     Maze.MoveDown();
  65.                 }
  66.                 else if(Maze.IsLeftFree() && stackWay.Peek() != 1)
  67.                 {
  68.                     heroX--;
  69.                     stackWay.Push(3);
  70.                     Maze.MoveLeft();
  71.                 }
  72.                 else if(Maze.IsUpFree() && stackWay.Peek() != 2)
  73.                 {
  74.                     heroY++;
  75.                     stackWay.Push(4);
  76.                     Maze.MoveUp();
  77.                 }
  78.                 else //Тупик = возврат к предыдущей развилке.
  79.                 {
  80.                     while(stackWay.Count != stackCrossWay.Peek())
  81.                     {
  82.                         if(stackWay.Peek() == 1)
  83.                         {
  84.                             Maze.MoveLeft();
  85.                             stackWay.Pop();
  86.                         }
  87.                         else if (stackWay.Peek() == 2)
  88.                         {
  89.                             Maze.MoveUp();
  90.                             stackWay.Pop();
  91.                         }
  92.                         else if (stackWay.Peek() == 3)
  93.                         {
  94.                             Maze.MoveRight();
  95.                             stackWay.Pop();
  96.                         }
  97.                         else if (stackWay.Peek() == 4)
  98.                         {
  99.                             Maze.MoveDown();
  100.                             stackWay.Pop();
  101.                         }
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement