Advertisement
Guest User

Untitled

a guest
May 21st, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace _2nd_attempt
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int[] dimensions = Console.ReadLine()
  13. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  14. .Select(int.Parse)
  15. .ToArray();
  16.  
  17. char[,] matrix = new char[dimensions[0], dimensions[1]];
  18.  
  19. int playerRow = 0;
  20. int playerCol = 0;
  21.  
  22. for (int row = 0; row < matrix.GetLength(0); row++)
  23. {
  24. char[] rowValues = Console.ReadLine().ToCharArray();
  25.  
  26. for (int col = 0; col < matrix.GetLength(1); col++)
  27. {
  28. matrix[row, col] = rowValues[col];
  29.  
  30. if (matrix[row,col]=='P')
  31. {
  32. playerRow = row;
  33. playerCol = col;
  34. }
  35. }
  36. }
  37.  
  38. char[] directions = Console.ReadLine().ToCharArray();
  39. bool isWon = false;
  40. bool isDead = false;
  41.  
  42. foreach (char direction in directions)
  43. {
  44. int playerNewRow = playerRow;
  45. int playerNewCol = playerCol;
  46.  
  47. switch (direction)
  48. {
  49. case 'U':
  50. playerNewRow--;
  51. break;
  52.  
  53. case 'D':
  54. playerNewRow++;
  55. break;
  56.  
  57. case 'L':
  58. playerNewCol--;
  59. break;
  60.  
  61. case 'R':
  62. playerNewRow++;
  63. break;
  64. }
  65.  
  66. isWon = Won(matrix,playerNewRow,playerNewCol);
  67.  
  68.  
  69. if (!isWon)
  70. {
  71. isDead = Death(matrix,'B',playerNewRow,playerNewCol);
  72.  
  73.  
  74. if (!isDead)
  75. {
  76. matrix[playerRow, playerCol] = 'P';
  77. }
  78.  
  79. matrix[playerRow, playerCol] = '.';
  80. playerRow = playerNewRow;
  81. playerCol = playerNewCol;
  82. }
  83. else
  84. {
  85. matrix[playerRow, playerCol] = '.';
  86. }
  87.  
  88. List<int> coordinatesBunnies = new List<int>();
  89.  
  90. for (int row = 0; row < matrix.GetLength(0); row++)
  91. {
  92. for (int col = 0; col < matrix.GetLength(1); col++)
  93. {
  94. if (matrix[row, col] == 'B')
  95. {
  96. coordinatesBunnies.Add(row);
  97. coordinatesBunnies.Add(col);
  98. }
  99. }
  100. }
  101.  
  102.  
  103.  
  104. for (int i = 0; i < coordinatesBunnies.Count; i+=2)
  105. {
  106. int rowBunny = coordinatesBunnies[i];
  107. int colBunny = coordinatesBunnies[i + 1];
  108.  
  109. SpreadBunny(matrix,rowBunny,colBunny);
  110. }
  111.  
  112. isDead = Death(matrix, 'B', playerRow, playerCol);
  113.  
  114. if (isDead||isWon)
  115. {
  116. break;
  117. }
  118.  
  119. }
  120.  
  121. PrintMatrix(matrix);
  122.  
  123. if (isDead)
  124. {
  125. Console.WriteLine($"dead: {playerRow} {playerCol}");
  126. }
  127.  
  128. else if (isWon)
  129. {
  130. Console.WriteLine($"won: {playerRow} {playerCol}");
  131. }
  132. }
  133.  
  134. private static bool Death(char[,] matrix, char symbol, int playerNewRow, int playerNewCol)
  135. {
  136. bool isDead = false;
  137.  
  138. if (matrix[playerNewRow,playerNewCol]==symbol)
  139. {
  140. isDead = true;
  141. }
  142.  
  143. return isDead;
  144. }
  145.  
  146. private static bool Won(char[,] matrix, int playerNewRow, int playerNewCol)
  147. {
  148. bool isWon = true;
  149.  
  150. if ((playerNewRow>=0&&playerNewRow<matrix.GetLength(0))&&(playerNewCol>=0&&playerNewCol<matrix.GetLength(1)))
  151. {
  152. isWon = false;
  153. }
  154.  
  155. return isWon;
  156. }
  157.  
  158.  
  159. private static void PrintMatrix(char[,] matrix)
  160. {
  161. for (int row = 0; row < matrix.GetLength(0); row++)
  162. {
  163. for (int col = 0; col < matrix.GetLength(1); col++)
  164. {
  165. Console.Write(matrix[row,col]);
  166. }
  167.  
  168. Console.WriteLine();
  169.  
  170. }
  171.  
  172. }
  173.  
  174. private static void SpreadBunny(char[,] matrix, int rowBunny, int colBunny)
  175. {
  176. if (rowBunny + 1 < matrix.GetLength(0))
  177. {
  178.  
  179. matrix[rowBunny + 1, colBunny] = 'B';
  180. }
  181.  
  182. if (rowBunny - 1 >= 0)
  183. {
  184.  
  185. matrix[rowBunny - 1, colBunny] = 'B';
  186. }
  187.  
  188. if (colBunny + 1 < matrix.GetLength(1))
  189. {
  190.  
  191. matrix[rowBunny, colBunny + 1] = 'B';
  192. }
  193.  
  194. if (colBunny - 1 >= 0)
  195. {
  196.  
  197. matrix[rowBunny, colBunny - 1] = 'B';
  198. }
  199.  
  200. }
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement