Advertisement
Guest User

Untitled

a guest
Oct 24th, 2020
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. using System;
  2.  
  3. namespace examPrep
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int n = int.Parse(Console.ReadLine());
  10.  
  11. char[,] matrix = new char[n, n];
  12.  
  13. int snakeRow = 0;
  14. int snakeCol = 0;
  15. int food = 0;
  16.  
  17. for (int rows = 0; rows < n; rows++)
  18. {
  19. string input = Console.ReadLine();
  20.  
  21. for (int cols = 0; cols < n; cols++)
  22. {
  23. matrix[rows, cols] = input[cols];
  24.  
  25. if (matrix[rows, cols] == 'S')
  26. {
  27. snakeRow = rows;
  28. snakeCol = cols;
  29. }
  30. }
  31. }
  32.  
  33. while (true)
  34. {
  35. string command = Console.ReadLine();
  36.  
  37. matrix[snakeRow, snakeCol] = '.';
  38.  
  39. if (command == "up")
  40. {
  41. snakeRow--;
  42.  
  43. if (snakeRow >= 0)
  44. {
  45. if (matrix[snakeRow, snakeCol] == '*')
  46. {
  47. food++;
  48. }
  49. else if (matrix[snakeRow, snakeCol] == 'B')
  50. {
  51. matrix[snakeRow, snakeCol] = '.';
  52. for (int rows = 0; rows < n; rows++)
  53. {
  54. for (int cols = 0; cols < n; cols++)
  55. {
  56. if (matrix[rows, cols] == 'B')
  57. {
  58. snakeRow = rows;
  59. snakeCol = cols;
  60. }
  61. }
  62. }
  63. }
  64. }
  65. else
  66. {
  67. Console.WriteLine("Game over!");
  68. break;
  69. }
  70. }
  71. else if (command == "down")
  72. {
  73. snakeRow++;
  74.  
  75. if (snakeRow < n)
  76. {
  77. if (matrix[snakeRow, snakeCol] == '*')
  78. {
  79. food++;
  80. }
  81. else if (matrix[snakeRow, snakeCol] == 'B')
  82. {
  83. matrix[snakeRow, snakeCol] = '.';
  84. for (int rows = 0; rows < n; rows++)
  85. {
  86. for (int cols = 0; cols < n; cols++)
  87. {
  88. if (matrix[rows, cols] == 'B')
  89. {
  90. snakeRow = rows;
  91. snakeCol = cols;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. else
  98. {
  99. Console.WriteLine("Game over!");
  100. break;
  101. }
  102. }
  103. else if (command == "left")
  104. {
  105. snakeCol--;
  106.  
  107. if (snakeCol >= 0)
  108. {
  109. if (matrix[snakeRow, snakeCol] == '*')
  110. {
  111. food++;
  112. }
  113. else if (matrix[snakeRow, snakeCol] == 'B')
  114. {
  115. matrix[snakeRow, snakeCol] = '.';
  116. for (int rows = 0; rows < n; rows++)
  117. {
  118. for (int cols = 0; cols < n; cols++)
  119. {
  120. if (matrix[rows, cols] == 'B')
  121. {
  122. snakeRow = rows;
  123. snakeCol = cols;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. else
  130. {
  131. Console.WriteLine("Game over!");
  132. break;
  133. }
  134. }
  135. else if (command == "right")
  136. {
  137. snakeCol++;
  138.  
  139. if (snakeCol < n)
  140. {
  141. if (matrix[snakeRow, snakeCol] == '*')
  142. {
  143. food++;
  144. }
  145. else if (matrix[snakeRow, snakeCol] == 'B')
  146. {
  147. matrix[snakeRow, snakeCol] = '.';
  148. for (int rows = 0; rows < n; rows++)
  149. {
  150. for (int cols = 0; cols < n; cols++)
  151. {
  152. if (matrix[rows, cols] == 'B')
  153. {
  154. snakeRow = rows;
  155. snakeCol = cols;
  156. }
  157. }
  158. }
  159. }
  160. }
  161. else
  162. {
  163. Console.WriteLine("Game over!");
  164. break;
  165. }
  166. }
  167.  
  168. if (food == 10)
  169. {
  170. Console.WriteLine("You won! You fed the snake.");
  171. matrix[snakeRow, snakeCol] = 'S';
  172. break;
  173. }
  174.  
  175. matrix[snakeRow, snakeCol] = 'S';
  176. }
  177.  
  178. Console.WriteLine($"Food eaten: {food}");
  179.  
  180. for (int rows = 0; rows < n; rows++)
  181. {
  182. for (int cols = 0; cols < n; cols++)
  183. {
  184. Console.Write($"{matrix[rows, cols]}");
  185. }
  186.  
  187. Console.WriteLine();
  188. }
  189. }
  190. public static void PrintMatrix(char[,] matrix)
  191. {
  192. for (int rows = 0; rows < matrix.GetLength(0); rows++)
  193. {
  194. for (int cols = 0; cols < matrix.GetLength(1); cols++)
  195. {
  196. Console.Write($"{matrix[rows, cols]}");
  197. }
  198.  
  199. Console.WriteLine();
  200. }
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement