Advertisement
desislava_topuzakova

09. Miner

Sep 18th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. //КВАДРАТНА МАТРИЦА
  2. using System.Security;
  3.  
  4. int size = int.Parse(Console.ReadLine()); // бр. редове = бр. колони
  5.  
  6. string[] directions = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  7. //"up right right up right" -> ["up", "right", "right", "up", "right"]
  8. //валидни посоки: left, right, up, and down.
  9.  
  10. char[,] matrix = new char[size, size];
  11.  
  12. int currentRow = 0; //ред на стартиране
  13. int currentCol = 0; //колона на стартиране
  14. int countCoal = 0; //брой на полезни изкопаеми
  15.  
  16.  
  17. //Прочитане на матрица от конзолата
  18. for (int row = 0; row <= size - 1; row++) //всички редове от първия до последния
  19. {
  20. char[] symbols = Console.ReadLine()
  21. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  22. .Select(char.Parse)
  23. .ToArray();
  24. for (int col = 0; col <= size - 1; col++)
  25. {
  26. matrix[row, col] = symbols[col];
  27. if (symbols[col] == 's')
  28. {
  29. currentRow = row;
  30. currentCol = col;
  31. } else if (symbols[col] == 'c')
  32. {
  33. countCoal++;
  34. }
  35. }
  36. }
  37.  
  38. //Колко ползени изкопаеми имаме за събиране
  39. foreach (string direction in directions)
  40. {
  41. //direction: left, right, up, and down
  42. switch (direction)
  43. {
  44. case "left":
  45. //колона - 1
  46. //валидираме мястото, на което ще отидем преди преместване
  47. if (currentCol - 1 >= 0 && currentCol - 1 <= size - 1)
  48. {
  49. //преместване
  50. currentCol--;
  51. //проверка къде сме отишли
  52. char currentElement = matrix[currentRow, currentCol];
  53. if (currentElement == 'e')
  54. {
  55. //прекратим цикъла с посоките
  56. Console.WriteLine($"Game over! ({currentRow}, {currentCol})");
  57. return;
  58. }
  59. else if (currentElement == 'c')
  60. {
  61. //взимаме полезно изкопаемото
  62. matrix[currentRow, currentCol] = '*';
  63. countCoal--; //взели едно изкопаемо от матрицата
  64. if (countCoal == 0)
  65. {
  66. Console.WriteLine($"You collected all coals! ({currentRow}, {currentCol})");
  67. return;
  68. }
  69. }
  70.  
  71. }
  72.  
  73.  
  74. break;
  75.  
  76. case "right":
  77. //колона + 1
  78. if (currentCol + 1 >= 0 && currentCol + 1 <= size - 1)
  79. {
  80. currentCol++;
  81. //проверка къде сме отишли
  82. char currentElement = matrix[currentRow, currentCol];
  83. if (currentElement == 'e')
  84. {
  85. //прекратим цикъла с посоките
  86. Console.WriteLine($"Game over! ({currentRow}, {currentCol})");
  87. return;
  88. }
  89. else if (currentElement == 'c')
  90. {
  91. //взимаме полезно изкопаемото
  92. matrix[currentRow, currentCol] = '*';
  93. countCoal--; //взели едно изкопаемо от матрицата
  94. if (countCoal == 0)
  95. {
  96. Console.WriteLine($"You collected all coals! ({currentRow}, {currentCol})");
  97. return;
  98. }
  99. }
  100.  
  101. }
  102.  
  103. break;
  104.  
  105. case "up":
  106. //ред - 1
  107. if (currentRow - 1 >= 0 && currentRow - 1 <= size - 1)
  108. {
  109. currentRow--;
  110. //проверка къде сме отишли
  111. char currentElement = matrix[currentRow, currentCol];
  112. if (currentElement == 'e')
  113. {
  114. //прекратим цикъла с посоките
  115. Console.WriteLine($"Game over! ({currentRow}, {currentCol})");
  116. return;
  117. }
  118. else if (currentElement == 'c')
  119. {
  120. //взимаме полезно изкопаемото
  121. matrix[currentRow, currentCol] = '*';
  122. countCoal--; //взели едно изкопаемо от матрицата
  123. if (countCoal == 0)
  124. {
  125. Console.WriteLine($"You collected all coals! ({currentRow}, {currentCol})");
  126. return;
  127. }
  128. }
  129.  
  130. }
  131. break;
  132.  
  133. case "down":
  134. //ред + 1
  135. if (currentRow + 1 >= 0 && currentRow + 1 <= size - 1)
  136. {
  137. currentRow++;
  138. //проверка къде сме отишли
  139. char currentElement = matrix[currentRow, currentCol];
  140. if (currentElement == 'e')
  141. {
  142. //прекратим цикъла с посоките
  143. Console.WriteLine($"Game over! ({currentRow}, {currentCol})");
  144. return;
  145. }
  146. else if (currentElement == 'c')
  147. {
  148. //взимаме полезно изкопаемото
  149. matrix[currentRow, currentCol] = '*';
  150. countCoal--; //взели едно изкопаемо от матрицата
  151. if (countCoal == 0)
  152. {
  153. Console.WriteLine($"You collected all coals! ({currentRow}, {currentCol})");
  154. return;
  155. }
  156. }
  157. }
  158. break;
  159. }
  160. }
  161.  
  162. Console.WriteLine($"{countCoal} coals left. ({currentRow}, {currentCol})");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement