Advertisement
osman1997

Untitled

Apr 14th, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. using System;
  2.  
  3. namespace matrixretakeexamadvanced
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int marioLive = int.Parse(Console.ReadLine());
  10. int n = int.Parse(Console.ReadLine());
  11.  
  12. char[,] matrix = new char[n, n];
  13.  
  14. int marioOldRow = 0;
  15. int marioOldCol = 0;
  16.  
  17. int marioNewRow = 0;
  18. int marioNewCol = 0;
  19.  
  20. bool isWon = false;
  21.  
  22.  
  23. for (int rows = 0; rows < n; rows++)
  24. {
  25. var command = Console.ReadLine().ToCharArray();
  26.  
  27. for (int cols = 0; cols < n; cols++)
  28. {
  29. matrix[rows, cols] = command[cols];
  30.  
  31. if(matrix[rows, cols] == 'M')
  32. {
  33. marioNewRow = rows;
  34. marioNewCol = cols;
  35. }
  36. }
  37. }
  38.  
  39.  
  40. //down=(row++, col), up=(row--, col), right=(row, col++), left=(row, col--)
  41.  
  42. matrix[marioNewRow, marioNewCol] = '-';
  43.  
  44.  
  45. while (true)
  46. {
  47. string[] command = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  48.  
  49. marioOldRow = marioNewRow;
  50. marioOldCol = marioNewCol;
  51.  
  52. string direction = command[0];
  53. int bowserRow = int.Parse(command[1]);
  54. int bowserCol = int.Parse(command[2]);
  55.  
  56. matrix[bowserRow, bowserCol] = 'B';
  57.  
  58. matrix[marioOldRow, marioOldCol] = '-';
  59.  
  60. if(direction == "W")
  61. {
  62. marioNewRow -= 1;
  63. }
  64. else if(direction == "S")
  65. {
  66. marioNewRow += 1;
  67. }
  68. else if(direction == "A")
  69. {
  70. marioNewCol -= 1;
  71. }
  72. else if(direction == "D")
  73. {
  74. marioNewCol += 1;
  75. }
  76. marioLive -= 1;
  77.  
  78. if(!IsValidCell(marioNewRow, marioNewCol, n))
  79. {
  80. switch (direction)
  81. {
  82. case "D":
  83. marioNewCol -= 1;
  84. break;
  85. case "W":
  86. marioNewRow += 1;
  87. break;
  88. case "S":
  89. marioNewRow -= 1;
  90. break;
  91. case "A":
  92. marioNewCol += 1;
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98.  
  99. if(matrix[marioNewRow, marioNewCol] == 'B')
  100. {
  101. marioLive -= 2;
  102. }
  103. if (matrix[marioNewRow, marioNewCol] == 'P')
  104. {
  105. matrix[marioNewRow, marioNewCol] = '-';
  106. isWon = true;
  107. break;
  108. }
  109.  
  110. if (marioLive <= 0)
  111. {
  112. matrix[marioNewRow, marioNewCol] = 'X';
  113. break;
  114. }
  115.  
  116. matrix[marioNewRow, marioNewCol] = 'M';
  117. }
  118.  
  119.  
  120. if (isWon)
  121. {
  122. Console.WriteLine($"Mario has successfully saved the princess! Lives left: {marioLive}");
  123. }
  124. else
  125. {
  126. Console.WriteLine($"Mario died at {marioNewRow};{marioNewCol}.");
  127. }
  128.  
  129. for (int rows = 0; rows < n; rows++)
  130. {
  131. for (int cols = 0; cols < n; cols++)
  132. {
  133. Console.Write(matrix[rows, cols]);
  134. }
  135. Console.WriteLine();
  136. }
  137. }
  138.  
  139. private static bool IsValidCell(int row, int col, int n)
  140. {
  141. return row >= 0 && row < n && col >= 0 && col < n;
  142.  
  143. }
  144. }
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement