Advertisement
MZlatev

Untitled

Apr 15th, 2020
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.70 KB | None | 0 0
  1. namespace P02.CollectingEggs
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. public class Program
  8. {
  9. public static void Main()
  10. {
  11. int n = int.Parse(Console.ReadLine());
  12. char[,] matrix = new char[n, n];
  13.  
  14. int rabbitRow = 0;
  15. int rabbitCol = 0;
  16. Queue<char> basket = new Queue<char>();
  17.  
  18. InitializeMatrix(n, matrix, ref rabbitRow, ref rabbitCol);
  19.  
  20. string command = Console.ReadLine();
  21.  
  22. while (command != "end")
  23. {
  24. if (command == "up")
  25. {
  26. MovingUp(n, matrix, rabbitRow, rabbitCol, basket);
  27. }
  28. else if (command == "down")
  29. {
  30. MovingDown(n, matrix, rabbitRow, rabbitCol, basket);
  31. }
  32. else if (command == "right")
  33. {
  34. MovingRight(n, matrix, rabbitRow, rabbitCol, basket);
  35. }
  36. else if (command == "left")
  37. {
  38. MovingLeft(n, matrix, rabbitRow, rabbitCol, basket);
  39. }
  40.  
  41. command = Console.ReadLine();
  42. }
  43.  
  44. PrintOutputMessage(matrix, basket);
  45.  
  46. PrintCurrentStateOfMatrix(matrix);
  47. }
  48.  
  49. private static void InitializeMatrix(int n, char[,] matrix, ref int rabbitRow, ref int rabbitCol)
  50. {
  51. for (int row = 0; row < n; row++)
  52. {
  53. string[] currentRow = Console
  54. .ReadLine()
  55. .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  56.  
  57. for (int col = 0; col < n; col++)
  58. {
  59. matrix[row, col] = char.Parse(currentRow[col]);
  60.  
  61. char symbol = matrix[row, col];
  62.  
  63. if (symbol == 'B')
  64. {
  65. rabbitRow = row;
  66. rabbitCol = col;
  67. }
  68. }
  69. }
  70. }
  71.  
  72. public static bool IsIndexValid(int row, int col, int n)
  73. {
  74. bool validCoordinates = row >= 0 &&
  75. row < n &&
  76. col >= 0 &&
  77. col < n;
  78.  
  79. return validCoordinates;
  80. }
  81.  
  82. private static void FindOutCurrentLocationOfBunny
  83. (int n, char[,] matrix, ref int rabbitRow, ref int rabbitCol)
  84. {
  85. for (int row = 0; row < n; row++)
  86. {
  87. for (int col = 0; col < n; col++)
  88. {
  89. char symbol = matrix[row, col];
  90.  
  91. if (symbol == 'B')
  92. {
  93. rabbitRow = row;
  94. rabbitCol = col;
  95. }
  96. }
  97. }
  98. }
  99.  
  100. public static void BunnyMovesToTheNewPlace
  101. (char[,] matrix, int currentRow, int currentCol, int newRow, int newCol)
  102. {
  103. matrix[currentRow, currentCol] = '-';
  104. matrix[newRow, newCol] = 'B';
  105. }
  106.  
  107. private static void MovingUp(int n, char[,] matrix, int rabbitRow, int rabbitCol, Queue<char> basket)
  108. {
  109. FindOutCurrentLocationOfBunny(n, matrix, ref rabbitRow, ref rabbitCol);
  110.  
  111. int afterStepRow = rabbitRow - 1;
  112. int afterStepCol = rabbitCol;
  113.  
  114. if (!IsIndexValid(afterStepRow, afterStepCol, n))
  115. {
  116. if (basket.Any())
  117. {
  118. basket.Dequeue();
  119. }
  120. }
  121. else
  122. {
  123. char nextStep = matrix[afterStepRow, afterStepCol];
  124.  
  125. if (char.IsLower(nextStep))
  126. {
  127. basket.Enqueue(nextStep);
  128. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  129. }
  130. else if (nextStep == 'C')
  131. {
  132. if (afterStepRow == 0)
  133. {
  134. char positionAfterLeap = matrix[n - 1, rabbitCol];
  135.  
  136. if (char.IsLower(positionAfterLeap))
  137. {
  138. basket.Enqueue(positionAfterLeap);
  139. }
  140.  
  141. matrix[rabbitRow, rabbitCol] = '-';
  142. matrix[afterStepRow, afterStepCol] = '-';
  143. matrix[n - 1, rabbitCol] = 'B';
  144. }
  145. else
  146. {
  147. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  148. }
  149. }
  150. else
  151. {
  152. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  153. }
  154. }
  155. }
  156.  
  157. private static void MovingDown(int n, char[,] matrix, int rabbitRow, int rabbitCol, Queue<char> basket)
  158. {
  159. FindOutCurrentLocationOfBunny(n, matrix, ref rabbitRow, ref rabbitCol);
  160.  
  161. int afterStepRow = rabbitRow + 1;
  162. int afterStepCol = rabbitCol;
  163.  
  164. if (!IsIndexValid(afterStepRow, afterStepCol, n))
  165. {
  166. if (basket.Any())
  167. {
  168. basket.Dequeue();
  169. }
  170. }
  171. else
  172. {
  173. char nextStep = matrix[afterStepRow, afterStepCol];
  174.  
  175. if (char.IsLower(nextStep))
  176. {
  177. basket.Enqueue(nextStep);
  178. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  179. }
  180. else if (nextStep == 'C')
  181. {
  182. if (afterStepRow == n - 1)
  183. {
  184. char positionAfterLeap = matrix[0, rabbitCol];
  185.  
  186. if (char.IsLower(positionAfterLeap))
  187. {
  188. basket.Enqueue(positionAfterLeap);
  189. }
  190.  
  191. matrix[rabbitRow, rabbitCol] = '-';
  192. matrix[afterStepRow, afterStepCol] = '-';
  193. matrix[0, rabbitCol] = 'B';
  194. }
  195. else
  196. {
  197. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  198. }
  199. }
  200. else
  201. {
  202. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  203. }
  204. }
  205. }
  206.  
  207. private static void MovingRight(int n, char[,] matrix, int rabbitRow, int rabbitCol, Queue<char> basket)
  208. {
  209. FindOutCurrentLocationOfBunny(n, matrix, ref rabbitRow, ref rabbitCol);
  210.  
  211. int afterStepRow = rabbitRow;
  212. int afterStepCol = rabbitCol + 1;
  213.  
  214. if (!IsIndexValid(afterStepRow, afterStepCol, n))
  215. {
  216. if (basket.Any())
  217. {
  218. basket.Dequeue();
  219. }
  220. }
  221. else
  222. {
  223. char nextStep = matrix[afterStepRow, afterStepCol];
  224.  
  225. if (char.IsLower(nextStep))
  226. {
  227. basket.Enqueue(nextStep);
  228. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  229. }
  230. else if (nextStep == 'C')
  231. {
  232. if (afterStepCol == n - 1)
  233. {
  234. char positionAfterLeap = matrix[rabbitRow, 0];
  235.  
  236. if (char.IsLower(positionAfterLeap))
  237. {
  238. basket.Enqueue(positionAfterLeap);
  239. }
  240.  
  241. matrix[rabbitRow, rabbitCol] = '-';
  242. matrix[afterStepRow, afterStepCol] = '-';
  243. matrix[rabbitRow, 0] = 'B';
  244. }
  245. else
  246. {
  247. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  248. }
  249. }
  250. else
  251. {
  252. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  253. }
  254. }
  255. }
  256.  
  257. private static void MovingLeft(int n, char[,] matrix, int rabbitRow, int rabbitCol, Queue<char> basket)
  258. {
  259. FindOutCurrentLocationOfBunny(n, matrix, ref rabbitRow, ref rabbitCol);
  260.  
  261. int afterStepRow = rabbitRow;
  262. int afterStepCol = rabbitCol - 1;
  263.  
  264. if (!IsIndexValid(afterStepRow, afterStepCol, n))
  265. {
  266. if (basket.Any())
  267. {
  268. basket.Dequeue();
  269. }
  270. }
  271. else
  272. {
  273. char nextStep = matrix[afterStepRow, afterStepCol];
  274.  
  275. if (char.IsLower(nextStep))
  276. {
  277. basket.Enqueue(nextStep);
  278. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  279. }
  280. else if (nextStep == 'C')
  281. {
  282. if (afterStepCol == 0)
  283. {
  284. char positionAfterLeap = matrix[rabbitRow, n - 1];
  285.  
  286. if (char.IsLower(positionAfterLeap))
  287. {
  288. basket.Enqueue(positionAfterLeap);
  289. }
  290.  
  291. matrix[rabbitRow, rabbitCol] = '-';
  292. matrix[afterStepRow, afterStepCol] = '-';
  293. matrix[n - 1, rabbitCol] = 'B';
  294. }
  295. else
  296. {
  297. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  298. }
  299. }
  300. else
  301. {
  302. BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
  303. }
  304. }
  305. }
  306.  
  307. private static void PrintOutputMessage(char[,] matrix, Queue<char> basket)
  308. {
  309. int numberOfEggs = 0;
  310.  
  311. //Find out how many eggs we have in the matrix
  312. for (int row = 0; row < matrix.GetLength(0); row++)
  313. {
  314. for (int col = 0; col < matrix.GetLength(1); col++)
  315. {
  316. if (char.IsLower(matrix[row, col]) && char.IsLetter(matrix[row, col]))
  317. {
  318. numberOfEggs++;
  319. }
  320. }
  321. }
  322.  
  323. //Output
  324. if (numberOfEggs == 0)
  325. {
  326. Console.WriteLine
  327. ($"Happy Easter! The Easter bunny collected {basket.Count} eggs: {string.Join(", ", basket)}.");
  328. }
  329. else if (numberOfEggs > 0)
  330. {
  331. Console.WriteLine
  332. ($"The Easter bunny failed to gather every egg. There are {numberOfEggs} eggs left to collect.");
  333. }
  334. }
  335.  
  336. private static void PrintCurrentStateOfMatrix(char[,] matrix)
  337. {
  338. for (int row = 0; row < matrix.GetLength(0); row++)
  339. {
  340. for (int col = 0; col < matrix.GetLength(1); col++)
  341. {
  342. Console.Write($"{matrix[row, col]} ");
  343. }
  344.  
  345. Console.WriteLine();
  346. }
  347. }
  348. }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement