Advertisement
MeliDragon

Untitled

Apr 14th, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. namespace _001.FirstProblem
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int lives = int.Parse(Console.ReadLine());
  11. int size = int.Parse(Console.ReadLine());
  12. char[,] feald = new char[size, size];
  13. int rowM = 0;
  14. int colM = 0;
  15.  
  16. for (int row = 0; row < size; row++)
  17. {
  18. char[] data = Console.ReadLine().ToCharArray();
  19.  
  20. for (int col = 0; col < size; col++)
  21. {
  22. if (data[col]=='M')
  23. {
  24. rowM = row;
  25. colM = col;
  26. }
  27.  
  28. feald[row, col] = data[col];
  29. }
  30. }
  31.  
  32. while (lives>0)
  33. {
  34. feald[rowM, colM] = '-';
  35. string[] commands = Console.ReadLine().Split();
  36. string command = commands[0];
  37. int bowerRow = int.Parse(commands[1]);
  38. int bowerCol = int.Parse(commands[2]);
  39.  
  40. feald[bowerRow, bowerCol] = 'B';
  41.  
  42. int currCol = MoveCol(colM, command);
  43. int currRow = MoveRow(rowM, command);
  44. lives--;
  45.  
  46. if (!InBowndery(currRow, currCol, size))
  47. {
  48. continue;
  49. }
  50. else
  51. {
  52. rowM = currRow;
  53. colM = currCol;
  54.  
  55. if (feald[rowM, colM]=='B')
  56. {
  57. lives-=2;
  58. if (lives <= 0)
  59. {
  60. feald[rowM, colM] = 'X';
  61. Console.WriteLine($"Mario died at {rowM};{colM}.");
  62. break;
  63. }
  64. else
  65. {
  66. feald[rowM, colM] = 'M';
  67. continue;
  68. }
  69. }
  70. else if (feald[rowM, colM] == 'P')
  71. {
  72. Console.WriteLine($"Mario has successfully saved the princess! Lives left: {lives}");
  73. feald[rowM, colM] = '-';
  74. break;
  75. }
  76. }
  77. }
  78.  
  79. for (int row = 0; row < size; row++)
  80. {
  81. for (int col = 0; col < size; col++)
  82. {
  83. Console.Write(feald[row, col]);
  84. }
  85. Console.WriteLine();
  86. }
  87. }
  88.  
  89. static bool InBowndery(int row, int col, int size)
  90. {
  91. return row >= 0 && row < size && col >= 0 && col < size;
  92. }
  93.  
  94. private static int MoveCol(int col, string command)
  95. {
  96. if (command == "A")
  97. {
  98. return col - 1;
  99. }
  100. else if (command == "D")
  101. {
  102. return col + 1;
  103. }
  104.  
  105. return col;
  106. }
  107.  
  108. private static int MoveRow(int row, String command)
  109. {
  110. if (command == "W")
  111. {
  112. return row - 1;
  113. }
  114. else if (command == "S")
  115. {
  116. return row + 1;
  117. }
  118.  
  119. return row;
  120. }
  121. }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement