Advertisement
Prohause

Exam 14010.2018 Problem 03

Oct 14th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. ?using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Problem_03
  6. {
  7. class Program
  8. {
  9. private static int _remainingCoals;
  10. private static int _collectedCoals;
  11. private static KeyValuePair<int, int> _startingPosition;
  12. static void Main(string[] args)
  13. {
  14. var size = int.Parse(Console.ReadLine());
  15. var commands = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  16.  
  17. var mine = new String[size, size];
  18. _remainingCoals = 0;
  19. _collectedCoals = 0;
  20.  
  21.  
  22. for (int i = 0; i < size; i++)
  23. {
  24. var currentRow = Console.ReadLine().Split(new []{' '},StringSplitOptions.RemoveEmptyEntries).ToList();
  25. for (int j = 0; j < size; j++)
  26. {
  27. mine[i, j] = currentRow[j];
  28. if (currentRow[j].Equals("c"))
  29. {
  30. _remainingCoals++;
  31. }
  32.  
  33. if (currentRow[j].Equals("s"))
  34. {
  35. _startingPosition = new KeyValuePair<int, int>(i, j);
  36. }
  37. }
  38. }
  39. while (_remainingCoals > 0 && commands.Any())
  40. {
  41. var currentCommand = commands[0];
  42. commands.RemoveAt(0);
  43. switch (currentCommand)
  44. {
  45. case "up": MoveUp(mine); break;
  46. case "down": MoveDown(mine); break;
  47. case "left": MoveLeft(mine); break;
  48. case "right": MoveRight(mine); break;
  49. }
  50. }
  51.  
  52. Console.WriteLine(_remainingCoals == 0
  53. ? $"You collected all coals! ({_startingPosition.Key}, {_startingPosition.Value})"
  54. : $"{_remainingCoals} coals left. ({_startingPosition.Key}, {_startingPosition.Value})");
  55. }
  56.  
  57. private static void MoveUp(string[,] mine)
  58. {
  59. var row = _startingPosition.Key;
  60. var col = _startingPosition.Value;
  61. try
  62. {
  63. var current = mine[row - 1, col];
  64. mine[row, col] = "*";
  65. mine[row - 1, col] = "s";
  66. _startingPosition = new KeyValuePair<int, int>(row - 1, col);
  67. if (current.Equals("e"))
  68. {
  69. GameOver();
  70. }
  71. if (!current.Equals("c")) return;
  72. _remainingCoals--;
  73. _collectedCoals++;
  74.  
  75. }
  76. catch (Exception)
  77. {
  78. //ignored;
  79. }
  80. }
  81.  
  82. private static void MoveDown(string[,] mine)
  83. {
  84. var row = _startingPosition.Key;
  85. var col = _startingPosition.Value;
  86. try
  87. {
  88. var current = mine[row + 1, col];
  89. mine[row, col] = "*";
  90. mine[row + 1, col] = "s";
  91. _startingPosition = new KeyValuePair<int, int>(row + 1, col);
  92. if (current.Equals("e"))
  93. {
  94. GameOver();
  95. }
  96. if (!current.Equals("c")) return;
  97. _remainingCoals--;
  98. _collectedCoals++;
  99.  
  100. }
  101. catch (Exception)
  102. {
  103. //ignored;
  104. }
  105. }
  106.  
  107. private static void MoveLeft(string[,] mine)
  108. {
  109. var row = _startingPosition.Key;
  110. var col = _startingPosition.Value;
  111. try
  112. {
  113. var current = mine[row, col - 1];
  114. mine[row, col] = "*";
  115. mine[row, col - 1] = "s";
  116. _startingPosition = new KeyValuePair<int, int>(row, col - 1);
  117. if (current.Equals("e"))
  118. {
  119. GameOver();
  120. }
  121. if (!current.Equals("c")) return;
  122. _remainingCoals--;
  123. _collectedCoals++;
  124.  
  125. }
  126. catch (Exception)
  127. {
  128. //ignored;
  129. }
  130. }
  131.  
  132. private static void MoveRight(string[,] mine)
  133. {
  134. var row = _startingPosition.Key;
  135. var col = _startingPosition.Value;
  136. try
  137. {
  138. var current = mine[row, col + 1];
  139. mine[row, col] = "*";
  140. mine[row, col + 1] = "s";
  141. _startingPosition = new KeyValuePair<int, int>(row, col + 1);
  142. if (current.Equals("e"))
  143. {
  144. GameOver();
  145. }
  146. if (!current.Equals("c")) return;
  147. _remainingCoals--;
  148. _collectedCoals++;
  149.  
  150. }
  151. catch (Exception)
  152. {
  153. //ignored;
  154. }
  155. }
  156.  
  157. private static void GameOver()
  158. {
  159. Console.WriteLine($"Game over! ({_startingPosition.Key}, {_startingPosition.Value})");
  160. Environment.Exit(0);
  161. }
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement