Advertisement
StreetKatya

HeroLib v1

Feb 8th, 2022
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 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 Labirint { 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. if (!stackCrossWay.Contains(stackWay.Count))
  26. {
  27. int countCrossWay = 0;
  28. if (Labirint.IsDownFree())
  29. {
  30. countCrossWay++;
  31. }
  32. if (Labirint.IsLeftFree())
  33. {
  34. countCrossWay++;
  35. }
  36. if (Labirint.IsRightFree())
  37. {
  38. countCrossWay++;
  39. }
  40. if (Labirint.IsUpFree())
  41. {
  42. countCrossWay++;
  43. }
  44. if (countCrossWay > 2)
  45. {
  46. stackCrossWay.Push(stackWay.Count);
  47. }
  48. }
  49. }
  50. //right - 1 down - 2 left - 3 up - 4
  51. public void SearchPath() //1 - wall, 0 - road
  52. {
  53. Labirint.OnExit += GameOver;
  54. stackWay.Push(0);
  55. bool right = false;
  56. bool down = false;
  57. bool left = false;
  58. bool up = false;
  59. while (onExit == false)
  60. {
  61. CrossWay();
  62. if (Labirint.IsRightFree() && stackWay.Peek() != 3 && right == false)
  63. {
  64. stackWay.Push(1);
  65. Labirint.MoveRight();
  66. }
  67. else if (Labirint.IsDownFree() && stackWay.Peek() != 4 && down == false)
  68. {
  69. stackWay.Push(2);
  70. Labirint.MoveDown();
  71. }
  72. else if (Labirint.IsLeftFree() && stackWay.Peek() != 1 && left == false)
  73. {
  74. stackWay.Push(3);
  75. Labirint.MoveLeft();
  76. }
  77. else if (Labirint.IsUpFree() && stackWay.Peek() != 2 && up == false)
  78. {
  79. stackWay.Push(4);
  80. Labirint.MoveUp();
  81. }
  82. else //Тупик = возврат к предыдущей развилке.
  83. {
  84. int lastStep = 0;
  85. while (stackWay.Count != stackCrossWay.Peek())
  86. {
  87. if (stackWay.Peek() == 1)
  88. {
  89. Labirint.MoveLeft();
  90. stackWay.Pop();
  91. lastStep = 1;
  92. }
  93. else if (stackWay.Peek() == 2)
  94. {
  95. Labirint.MoveUp();
  96. stackWay.Pop();
  97. lastStep = 2;
  98. }
  99. else if (stackWay.Peek() == 3)
  100. {
  101. Labirint.MoveRight();
  102. stackWay.Pop();
  103. lastStep = 3;
  104. }
  105. else if (stackWay.Peek() == 4)
  106. {
  107. Labirint.MoveDown();
  108. stackWay.Pop();
  109. lastStep = 4;
  110. }
  111. }
  112. if (lastStep == 1)
  113. {
  114. right = true;
  115. }
  116. else if (lastStep == 2)
  117. {
  118. down = true;
  119. }
  120. else if (lastStep == 3)
  121. {
  122. left = true;
  123. }
  124. else if (lastStep == 4)
  125. {
  126. up = true;
  127. }
  128. if (stackWay.Peek() == 1)
  129. {
  130. right = true;
  131. }
  132. else if (stackWay.Peek() == 2)
  133. {
  134. down = true;
  135. }
  136. else if (stackWay.Peek() == 3)
  137. {
  138. left = true;
  139. }
  140. else if (stackWay.Peek() == 4)
  141. {
  142. up = true;
  143. }
  144. if (right && down && left && up)
  145. {
  146. stackCrossWay.Pop();
  147. while (stackWay.Count != stackCrossWay.Peek())
  148. {
  149. if (stackWay.Peek() == 1)
  150. {
  151. Labirint.MoveLeft();
  152. stackWay.Pop();
  153. }
  154. else if (stackWay.Peek() == 2)
  155. {
  156. Labirint.MoveUp();
  157. stackWay.Pop();
  158. }
  159. else if (stackWay.Peek() == 3)
  160. {
  161. Labirint.MoveRight();
  162. stackWay.Pop();
  163. }
  164. else if (stackWay.Peek() == 4)
  165. {
  166. Labirint.MoveDown();
  167. stackWay.Pop();
  168. }
  169. }
  170. right = false;
  171. down = false;
  172. left = false;
  173. up = false;
  174. }
  175. }
  176. }
  177. }
  178.  
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement