Advertisement
Guest User

Untitled

a guest
May 26th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. namespace Helen_sAbduction
  2. {
  3. using System;
  4.  
  5. public class Helen_sAbduction
  6. {
  7. static int helenRow = 0;
  8. static int helenCol = 0;
  9. static int parisRow = 0;
  10. static int parisCol = 0;
  11. public static void Main()
  12. {
  13. int energy = int.Parse(Console.ReadLine());
  14. int matrixRows = int.Parse(Console.ReadLine());
  15.  
  16. var matrix = new char[matrixRows][];
  17.  
  18. FillsMatrix(matrix, matrixRows);
  19.  
  20. for (int i = 0; i < matrixRows; i++)
  21. {
  22. var command = Console.ReadLine()
  23. .Split(' ', StringSplitOptions.RemoveEmptyEntries);
  24.  
  25. var direction = command[0];
  26. int spartanRow = int.Parse(command[1]);
  27. int spartanCol = int.Parse(command[2]);
  28. bool isInside = false;
  29.  
  30. isInside = IsInside(matrixRows, matrix, spartanRow, spartanCol, isInside);
  31.  
  32. energy--;
  33.  
  34. if (direction == "up"?.ToLower() && IsInside(matrixRows, matrix, parisRow - 1, parisCol, isInside))
  35. {
  36. matrix[spartanRow][spartanCol] = 'S';
  37.  
  38. if (matrix[parisRow - 1][parisCol] == '-')
  39. {
  40. ParisMovesUp(energy, matrix);
  41. }
  42.  
  43. else if (matrix[parisRow - 1][parisCol] == 'S')
  44. {
  45. energy -= 2;
  46. ParisMovesUp(energy, matrix);
  47. }
  48. else if (matrix[parisRow - 1][parisCol] == 'H')
  49. {
  50. HelenIsAbducted(energy, matrix);
  51. }
  52. }
  53. else if (direction == "down"?.ToLower() && IsInside(matrixRows, matrix, parisRow + 1, parisCol, isInside))
  54. {
  55. matrix[spartanRow][spartanCol] = 'S';
  56.  
  57. if (matrix[parisRow + 1][parisCol] == '-')
  58. {
  59. ParisMovesDown(energy, matrix);
  60. }
  61.  
  62. else if (matrix[parisRow + 1][parisCol] == 'S')
  63. {
  64. energy -= 2;
  65. ParisMovesDown(energy, matrix);
  66. }
  67. else if (matrix[parisRow + 1][parisCol] == 'H')
  68. {
  69. HelenIsAbducted(energy, matrix);
  70. }
  71. }
  72. else if (direction == "left"?.ToLower() && IsInside(matrixRows, matrix, parisRow, parisCol - 1, isInside))
  73. {
  74. matrix[spartanRow][spartanCol] = 'S';
  75.  
  76. if (matrix[parisRow][parisCol - 1] == '-')
  77. {
  78. ParisMovesLeft(energy, matrix);
  79. }
  80.  
  81. else if (matrix[parisRow][parisCol - 1] == 'S')
  82. {
  83. energy -= 2;
  84. ParisMovesLeft(energy, matrix);
  85. }
  86. else if (matrix[parisRow][parisCol - 1] == 'H')
  87. {
  88. HelenIsAbducted(energy, matrix);
  89. }
  90. }
  91. else if (direction == "right"?.ToLower() && IsInside(matrixRows, matrix, parisRow, parisCol + 1, isInside))
  92. {
  93. matrix[spartanRow][spartanCol] = 'S';
  94.  
  95. if (matrix[parisRow][parisCol + 1] == '-')
  96. {
  97. ParisMovesRight(energy, matrix);
  98. }
  99.  
  100. else if (matrix[parisRow][parisCol + 1] == 'S')
  101. {
  102. energy -= 2;
  103. ParisMovesRight(energy, matrix);
  104. }
  105. else if (matrix[parisRow][parisCol + 1] == 'H')
  106. {
  107. HelenIsAbducted(energy, matrix);
  108. }
  109. }
  110.  
  111. if (energy <= 0)
  112. {
  113. return;
  114. }
  115. }
  116. }
  117.  
  118. private static void HelenIsAbducted(int energy, char[][] matrix)
  119. {
  120. Console.WriteLine($"Paris has successfully abducted Helen! Energy left: {energy}");
  121. matrix[parisRow][parisCol] = '-';
  122. matrix[helenRow][helenCol] = '-';
  123. PrintsMatrix(matrix);
  124. }
  125.  
  126. private static void ParisMovesUp(int energy, char[][] matrix)
  127. {
  128. matrix[parisRow][parisCol] = '-';
  129. matrix[parisRow - 1][parisCol] = 'P';
  130. parisRow--;
  131.  
  132. if (energy <= 0)
  133. {
  134. ParisDies(matrix);
  135. PrintsMatrix(matrix);
  136. return;
  137. }
  138. }
  139. private static void ParisMovesDown(int energy, char[][] matrix)
  140. {
  141. matrix[parisRow][parisCol] = '-';
  142. matrix[parisRow + 1][parisCol] = 'P';
  143. parisRow++;
  144.  
  145. if (energy <= 0)
  146. {
  147. ParisDies(matrix);
  148. PrintsMatrix(matrix);
  149. return;
  150. }
  151. }
  152.  
  153. private static void ParisMovesLeft(int energy, char[][] matrix)
  154. {
  155. matrix[parisRow][parisCol] = '-';
  156. matrix[parisRow][parisCol - 1] = 'P';
  157. parisCol--;
  158.  
  159. if (energy <= 0)
  160. {
  161. ParisDies(matrix);
  162. PrintsMatrix(matrix);
  163. return;
  164. }
  165. }
  166.  
  167. private static void ParisMovesRight(int energy, char[][] matrix)
  168. {
  169. matrix[parisRow][parisCol] = '-';
  170. matrix[parisRow][parisCol + 1] = 'P';
  171. parisCol++;
  172.  
  173. if (energy <= 0)
  174. {
  175. ParisDies(matrix);
  176. PrintsMatrix(matrix);
  177. return;
  178. }
  179. }
  180.  
  181. private static void ParisDies(char[][] matrix)
  182. {
  183. matrix[parisRow][parisCol] = 'X';
  184. Console.WriteLine($"Paris died at {parisRow};{parisCol}.");
  185. }
  186.  
  187. private static bool IsInside(int matrixRows, char[][] matrix, int spartanRow, int spartanCol, bool isInside)
  188. {
  189. for (int row = 0; row < matrixRows; row++)
  190. {
  191. for (int col = 0; col < matrix[row].Length; col++)
  192. {
  193. isInside = spartanRow < matrix[row].Length && spartanRow >= 0
  194. && spartanCol < matrix[row].Length && spartanCol >= 0;
  195. }
  196. }
  197.  
  198. return isInside;
  199. }
  200.  
  201. private static void PrintsMatrix(char[][] matrix)
  202. {
  203. foreach (var row in matrix)
  204. {
  205. Console.WriteLine(string.Join("", row));
  206. }
  207. }
  208.  
  209. private static void FillsMatrix(char[][] matrix, int matrixSize)
  210. {
  211. for (int row = 0; row < matrixSize; row++)
  212. {
  213. var input = Console.ReadLine();
  214. matrix[row] = new char[input.Length];
  215.  
  216. for (int col = 0; col < matrix[row].Length; col++)
  217. {
  218. matrix[row][col] = input[col];
  219.  
  220. if (matrix[row][col] == 'H')
  221. {
  222. helenRow = row;
  223. helenCol = col;
  224. }
  225.  
  226. if (matrix[row][col] == 'P')
  227. {
  228. parisRow = row;
  229. parisCol = col;
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement