Advertisement
Guest User

BlackHoles

a guest
Jun 26th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. namespace SecondExercise
  2. {
  3. using System;
  4. using System.Linq;
  5.  
  6. public class SecondExercise
  7. {
  8. public static void Main()
  9. {
  10. int size = int.Parse(Console.ReadLine());
  11.  
  12. char[,] matrix = new char[size, size];
  13. //Initialize the matrix
  14. int stephenRow = 0;
  15. int stephenCol = 0;
  16. int energy = 0;
  17.  
  18. for (int row = 0; row < size; row++)
  19. {
  20. char[] inputRows = Console.ReadLine()
  21. .ToCharArray();
  22. for (int col = 0; col < size; col++)
  23. {
  24. matrix[row, col] = inputRows[col];
  25.  
  26. if (matrix[row, col] == 'S')
  27. {
  28. stephenRow = row;
  29. stephenCol = col;
  30. }
  31. }
  32. }
  33.  
  34. //Black holes are marked with O their count will be either 0 or 2
  35. //Stars are marked with a single digit
  36. //Empty positions are '-'
  37.  
  38. while (true)
  39. {
  40. string direction = Console.ReadLine()
  41. .ToLower();
  42.  
  43. if (direction == "up")
  44. {
  45. if (stephenRow - 1 >= 0)
  46. {
  47. if (matrix[stephenRow - 1, stephenCol] == 'O')
  48. {
  49. matrix[stephenRow - 1, stephenCol] = '-';
  50. TeleportToOtherBlackhole(size, matrix, ref stephenRow, ref stephenCol);
  51. }
  52. else if (char.IsDigit(matrix[stephenRow - 1, stephenCol]))
  53. {
  54. int currentStar = matrix[stephenRow - 1, stephenCol];
  55. energy += currentStar - '0';
  56. matrix[stephenRow - 1, stephenCol] = 'S';
  57. matrix[stephenRow, stephenCol] = '-';
  58. stephenRow--;
  59. }
  60. else
  61. {
  62. matrix[stephenRow - 1, stephenCol] = 'S';
  63. matrix[stephenRow, stephenCol] = '-';
  64. stephenRow--;
  65. }
  66. }
  67. else
  68. {
  69. matrix[stephenRow, stephenCol] = '-';
  70. Console.WriteLine("Bad news, the spaceship went to the void.");
  71. break;
  72. }
  73. }
  74. else if (direction == "down")
  75. {
  76. if (stephenRow < size - 1)
  77. {
  78. if (matrix[stephenRow + 1, stephenCol] == 'O')
  79. {
  80. matrix[stephenRow + 1, stephenCol] = '-';
  81. TeleportToOtherBlackhole(size, matrix, ref stephenRow, ref stephenCol);
  82. }
  83. else if (char.IsDigit(matrix[stephenRow + 1, stephenCol]))
  84. {
  85. int currentStar = matrix[stephenRow + 1, stephenCol];
  86. energy += currentStar - '0';
  87. matrix[stephenRow + 1, stephenCol] = 'S';
  88. matrix[stephenRow, stephenCol] = '-';
  89. stephenRow++;
  90. }
  91. else
  92. {
  93. matrix[stephenRow + 1, stephenCol] = 'S';
  94. matrix[stephenRow, stephenCol] = '-';
  95. stephenRow++;
  96. }
  97. }
  98. else
  99. {
  100. matrix[stephenRow, stephenCol] = '-';
  101. Console.WriteLine("Bad news, the spaceship went to the void.");
  102. break;
  103. }
  104. }
  105. else if (direction == "left")
  106. {
  107. if (stephenCol - 1 >= 0)
  108. {
  109. if (matrix[stephenRow, stephenCol - 1] == 'O')
  110. {
  111. matrix[stephenRow, stephenCol - 1] = '-';
  112. TeleportToOtherBlackhole(size, matrix, ref stephenRow, ref stephenCol);
  113. }
  114. else if (char.IsDigit(matrix[stephenRow, stephenCol - 1]))
  115. {
  116. int currentStar = matrix[stephenRow, stephenCol - 1];
  117. energy += currentStar - '0';
  118. matrix[stephenRow, stephenCol - 1] = 'S';
  119. matrix[stephenRow, stephenCol] = '-';
  120. stephenCol--;
  121. }
  122. else
  123. {
  124. matrix[stephenRow, stephenCol - 1] = 'S';
  125. matrix[stephenRow, stephenCol] = '-';
  126. stephenCol--;
  127. }
  128. }
  129. else
  130. {
  131. matrix[stephenRow, stephenCol] = '-';
  132. Console.WriteLine("Bad news, the spaceship went to the void.");
  133. break;
  134. }
  135. }
  136. else if (direction == "right")
  137. {
  138. if (stephenCol < size - 1)
  139. {
  140. if (matrix[stephenRow, stephenCol + 1] == 'O')
  141. {
  142. matrix[stephenRow, stephenCol + 1] = '-';
  143. TeleportToOtherBlackhole(size, matrix, ref stephenRow, ref stephenCol);
  144. }
  145. else if (char.IsDigit(matrix[stephenRow, stephenCol + 1]))
  146. {
  147. int currentStar = matrix[stephenRow, stephenCol + 1];
  148. energy += currentStar - '0';
  149. matrix[stephenRow, stephenCol + 1] = 'S';
  150. matrix[stephenRow, stephenCol] = '-';
  151. stephenCol++;
  152. }
  153. else
  154. {
  155. matrix[stephenRow, stephenCol + 1] = 'S';
  156. matrix[stephenRow, stephenCol] = '-';
  157. stephenCol++;
  158. }
  159. }
  160. else
  161. {
  162. matrix[stephenRow, stephenCol] = '-';
  163. Console.WriteLine("Bad news, the spaceship went to the void.");
  164. break;
  165. }
  166. }
  167. if (energy >= 50)
  168. {
  169. Console.WriteLine("Good news! Stephen succeeded in collecting enough star power!");
  170. break;
  171. }
  172.  
  173. }
  174.  
  175. Console.WriteLine($"Star power collected: {energy}");
  176.  
  177. for (int row = 0; row < size; row++)
  178. {
  179. for (int col = 0; col < size; col++)
  180. {
  181. Console.Write(matrix[row, col]);
  182. }
  183. Console.WriteLine();
  184. }
  185. }
  186.  
  187. private static void TeleportToOtherBlackhole(int size, char[,] matrix, ref int stephenRow, ref int stephenCol)
  188. {
  189. bool found = false;
  190. int blackHoles = 1;
  191. for (int row = 0; row < size; row++)
  192. {
  193. for (int col = 0; col < size; col++)
  194. {
  195. if (matrix[row, col] == 'O')
  196. {
  197. blackHoles++;
  198. }
  199. }
  200. }
  201.  
  202. if (blackHoles == 2)
  203. {
  204. for (int row = 0; row < size; row++)
  205. {
  206. for (int col = 0; col < size; col++)
  207. {
  208. if (matrix[row, col] == 'O')
  209. {
  210. matrix[stephenRow, stephenCol] = '-';
  211. stephenRow = row;
  212. stephenCol = col;
  213. matrix[stephenRow, stephenCol] = 'S';
  214. found = true;
  215. break;
  216. }
  217. }
  218. if (found)
  219. {
  220. break;
  221. }
  222. }
  223. }
  224. else
  225. {
  226. matrix[stephenRow, stephenCol] = 'S';
  227. }
  228. }
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement