Advertisement
pifka

Space Station Establishment

Jun 26th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace SpaceStationEstablishment
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. var row = int.Parse(Console.ReadLine());
  11.  
  12. var matrix = new char[row][];
  13.  
  14. var playerRow = -1;
  15. var playerCol = -1;
  16.  
  17. for (int i = 0; i < row; i++)
  18. {
  19. var col = Console.ReadLine().ToCharArray();
  20.  
  21. matrix[i] = col;
  22.  
  23. if (matrix[i].Contains('S'))
  24. {
  25. playerRow = i;
  26. playerCol = Array.IndexOf(matrix[i], 'S');
  27. }
  28. }
  29.  
  30. var energy = 0;
  31.  
  32. while (energy < 50)
  33. {
  34. var command = Console.ReadLine();
  35.  
  36. matrix[playerRow][playerCol] = '-';
  37.  
  38. switch (command)
  39. {
  40. case "up": playerRow--; break;
  41. case "down": playerRow++; break;
  42. case "left": playerCol--; break;
  43. case "right": playerCol++; break;
  44. }
  45.  
  46. if (IsInMatrix(matrix, playerRow, playerCol))
  47. {
  48. if (Char.IsDigit(matrix[playerRow][playerCol]))
  49. {
  50. energy += int.Parse(matrix[playerRow][playerCol].ToString());
  51. }
  52. else if (matrix[playerRow][playerCol] == 'O')
  53. {
  54. matrix[playerRow][playerCol] = '-';
  55.  
  56. for (int i = 0; i < row; i++)
  57. {
  58. if (matrix[i].Contains('O'))
  59. {
  60. playerRow = i;
  61. playerCol = Array.IndexOf(matrix[i], 'O');
  62. }
  63. }
  64. }
  65. matrix[playerRow][playerCol] = 'S';
  66. }
  67. else
  68. {
  69. Console.WriteLine("Bad news, the spaceship went to the void.");
  70. Console.WriteLine($"Star power collected: {energy}");
  71. PrintMatrix(matrix);
  72. return;
  73. }
  74. }
  75. Console.WriteLine("Good news! Stephen succeeded in collecting enough star power!");
  76. Console.WriteLine($"Star power collected: {energy}");
  77. PrintMatrix(matrix);
  78. }
  79.  
  80. private static void PrintMatrix(char[][] matrix)
  81. {
  82. foreach (var row in matrix)
  83. {
  84. Console.WriteLine($"{string.Join("", row)}");
  85. }
  86. return;
  87. }
  88.  
  89. public static bool IsInMatrix(char[][] matrix, int row, int col)
  90. {
  91. if (row >= 0 && row < matrix.Length && col >= 0 && col < matrix[row].Length)
  92. {
  93. return true;
  94. }
  95. return false;
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement