Advertisement
Guest User

Untitled

a guest
Apr 13th, 2020
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _3._3_Nikulden_s_meals
  7. {
  8. class Program
  9. {
  10. static void Main()
  11. {
  12. string input = Console.ReadLine();
  13.  
  14. int size = int.Parse(Console.ReadLine());
  15.  
  16. char[][] matrix = new char[size][];
  17.  
  18. int row = 0;
  19. int col = 0;
  20.  
  21. for (int i = 0; i < size; i++)
  22. {
  23. string data = Console.ReadLine();
  24.  
  25. matrix[i] = new char[data.Length];
  26.  
  27. for (int j = 0; j < data.Length; j++)
  28. {
  29. matrix[i][j] = data[j];
  30.  
  31. if (matrix[i][j] == 'P')
  32. {
  33. row = i;
  34. col = j;
  35. }
  36. }
  37. }
  38.  
  39. while (true)
  40. {
  41. string command = Console.ReadLine();
  42.  
  43. if (command == "end")
  44. {
  45. break;
  46. }
  47.  
  48. matrix[row][col] = '-';
  49.  
  50. if (command == "up")
  51. {
  52. row--;
  53.  
  54. if (row < 0)
  55. {
  56. row = 0;
  57. input = MyRemove(input);
  58. }
  59. else
  60. {
  61. input = CheckForLetter(input, matrix, row, col);
  62. }
  63.  
  64. }
  65. else if (command == "down")
  66. {
  67. row++;
  68.  
  69. if (row == matrix.Length)
  70. {
  71. row = matrix.Length - 1;
  72.  
  73. input = MyRemove(input);
  74. }
  75. else
  76. {
  77. input = CheckForLetter(input, matrix, row, col);
  78. }
  79. }
  80. else if (command == "left")
  81. {
  82. col--;
  83.  
  84. if (col < 0)
  85. {
  86. col = 0;
  87.  
  88. input = MyRemove(input);
  89. }
  90. else
  91. {
  92. input = CheckForLetter(input, matrix, row, col);
  93. }
  94. }
  95. else if (command == "right")
  96. {
  97. col++;
  98.  
  99. if (col == matrix.Length)
  100. {
  101. col = matrix.Length - 1;
  102.  
  103. input = MyRemove(input);
  104. }
  105. else
  106. {
  107. input = CheckForLetter(input, matrix, row, col);
  108. }
  109. }
  110.  
  111. matrix[row][col] = 'P';
  112. }
  113.  
  114. Console.WriteLine(input);
  115. Console.WriteLine(Print(matrix));
  116. }
  117.  
  118. private static string MyRemove(string input)
  119. {
  120. input = input.Remove(input.Length - 1, 1);
  121. return input;
  122. }
  123.  
  124. private static string CheckForLetter(string input, char[][] matrix, int row, int col)
  125. {
  126. if (char.IsLetter(matrix[row][col]))
  127. {
  128. input = input + matrix[row][col];
  129. }
  130.  
  131. return input;
  132. }
  133.  
  134. private static string Print(char[][] matrix)
  135. {
  136. StringBuilder sb = new StringBuilder();
  137.  
  138. for (int i = 0; i < matrix.Length; i++)
  139. {
  140. for (int j = 0; j < matrix[i].Length; j++)
  141. {
  142. sb.Append($"{matrix[i][j]}");
  143. }
  144.  
  145. sb.AppendLine();
  146. }
  147.  
  148. return sb.ToString().TrimEnd();
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement