Advertisement
Guest User

Untitled

a guest
Apr 25th, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _10_Radio_Active_Mutant_Vampire_Bunnies
  6. {
  7. class Program
  8. {
  9. static char[,] matrix;
  10. static void Main(string[] args)
  11. {
  12. var input = Console.ReadLine()
  13. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  14. .Select(int.Parse)
  15. .ToArray();
  16.  
  17. matrix = new char[input[0], input[1]];
  18.  
  19. (int rowCheck, int colCheck) = MatrixLoading();
  20.  
  21. string moves = Console.ReadLine();
  22.  
  23. bool IsSuccessMove = false;
  24. bool IsLive = false;
  25. bool playerDead = false;
  26.  
  27. for (int i = 0; i < moves.Length; i++)
  28. {
  29. if (moves[i] == 'R')
  30. {
  31. if (IsValid(rowCheck, colCheck + 1) == true)
  32. {
  33. if (matrix[rowCheck, colCheck + 1] == '.')
  34. {
  35. matrix[rowCheck, colCheck] = '.';
  36. IsSuccessMove = true;
  37. }
  38. else if (matrix[rowCheck, colCheck + 1] == 'B')
  39. {
  40. playerDead = true;
  41. }
  42. colCheck = colCheck + 1;
  43. }
  44. else
  45. {
  46. matrix[rowCheck, colCheck] = '.';
  47. BonneyMoves();
  48. MatrixPrint();
  49. Console.WriteLine($"won: {rowCheck} {colCheck}");
  50. return;
  51. }
  52. }
  53. else if (moves[i] == 'L')
  54. {
  55. if (IsValid(rowCheck, colCheck - 1) == true)
  56. {
  57. if (matrix[rowCheck, colCheck - 1] == '.')
  58. {
  59. matrix[rowCheck, colCheck] = '.';
  60. IsSuccessMove = true;
  61. }
  62.  
  63. else if (matrix[rowCheck, colCheck - 1] == 'B')
  64. {
  65. playerDead = true;
  66. }
  67. colCheck = colCheck - 1;
  68. }
  69. else
  70. {
  71. matrix[rowCheck, colCheck] = '.'; matrix[rowCheck, colCheck] = '.';
  72. BonneyMoves();
  73. MatrixPrint();
  74. Console.WriteLine($"won: {rowCheck} {colCheck}");
  75. return;
  76. }
  77. }
  78. else if (moves[i] == 'U')
  79. {
  80. if (IsValid(rowCheck - 1, colCheck) == true)
  81. {
  82. if (matrix[rowCheck - 1, colCheck] == '.')
  83. {
  84. matrix[rowCheck, colCheck] = '.';
  85. IsSuccessMove = true;
  86. }
  87. else if (matrix[rowCheck - 1, colCheck] == 'B')
  88. {
  89. playerDead = true;
  90. }
  91. rowCheck = rowCheck - 1;
  92. }
  93. else
  94. {
  95. matrix[rowCheck, colCheck] = '.';
  96. BonneyMoves();
  97. MatrixPrint();
  98. Console.WriteLine($"won: {rowCheck} {colCheck}");
  99. return;
  100. }
  101. }
  102. else if (moves[i] == 'D')
  103. {
  104. if (IsValid(rowCheck + 1, colCheck) == true)
  105. {
  106. if (matrix[rowCheck + 1, colCheck] == '.')
  107. {
  108. matrix[rowCheck, colCheck] = '.';
  109. IsSuccessMove = true;
  110. }
  111. else if (matrix[rowCheck + 1, colCheck] == 'B')
  112. {
  113. playerDead = true;
  114. }
  115. rowCheck = rowCheck + 1;
  116. }
  117. else
  118. {
  119. matrix[rowCheck, colCheck] = '.';
  120. BonneyMoves();
  121. MatrixPrint();
  122. Console.WriteLine($"won: {rowCheck} {colCheck}");
  123. return;
  124. }
  125. }
  126.  
  127. if (playerDead)
  128. {
  129. BonneyMoves();
  130. MatrixPrint();
  131. Console.WriteLine($"dead: {rowCheck} {colCheck}");
  132. return;
  133. }
  134.  
  135. else if (IsSuccessMove)
  136. {
  137. matrix[rowCheck, colCheck] = 'P';
  138. BonneyMoves();
  139.  
  140. foreach (var items in matrix)
  141. {
  142. if (items == 'P')
  143. {
  144. IsLive = true;
  145. }
  146.  
  147. }
  148.  
  149. if (IsLive == false)
  150. {
  151. MatrixPrint();
  152. Console.WriteLine($"dead: {rowCheck} {colCheck}");
  153. return;
  154. }
  155. }
  156. IsSuccessMove = false;
  157. }
  158. }
  159.  
  160. private static (int, int) MatrixLoading()
  161. {
  162. int rowCheck = 0;
  163. int colCheck = 0;
  164.  
  165. for (int row = 0; row < matrix.GetLength(0); row++)
  166. {
  167. string items = Console.ReadLine();
  168. for (int col = 0; col < matrix.GetLength(1); col++)
  169. {
  170. matrix[row, col] = items[col];
  171.  
  172. if (matrix[row, col] == 'P')
  173. {
  174. rowCheck = row;
  175. colCheck = col;
  176. }
  177. }
  178. }
  179.  
  180. return (rowCheck, colCheck);
  181. }
  182. private static bool IsValid(int rowCheck, int colCheck)
  183. {
  184. if ((rowCheck >= 0 && rowCheck < matrix.GetLength(0)) && (colCheck >= 0 && colCheck < matrix.GetLength(1)))
  185. {
  186. return true;
  187. }
  188. else
  189. {
  190. return false;
  191. }
  192. }
  193.  
  194. private static void MatrixPrint()
  195. {
  196. for (int row = 0; row < matrix.GetLength(0); row++)
  197. {
  198.  
  199. for (int col = 0; col < matrix.GetLength(1); col++)
  200. {
  201. Console.Write(matrix[row, col]);
  202. }
  203. Console.WriteLine();
  204. }
  205. }
  206. private static void BonneyMoves()
  207. {
  208. var indexBunnies = new Dictionary<int, List<int>>();
  209. int count = 0;
  210.  
  211. for (int row = 0; row < matrix.GetLength(0); row++)
  212. {
  213. for (int col = 0; col < matrix.GetLength(1); col++)
  214. {
  215. if (matrix[row, col] == 'B')
  216. {
  217. indexBunnies.Add(count, new List<int>());
  218. indexBunnies[count].Add(row);
  219. indexBunnies[count].Add(col);
  220. count++;
  221. }
  222. }
  223. }
  224.  
  225. for (int i =0;i<indexBunnies.Count;i++)
  226. {
  227. int row = indexBunnies[i][0];
  228. int col = indexBunnies[i][1];
  229.  
  230.  
  231. if (IsValid(row - 1, col) == true && matrix[row - 1, col] != 'B')
  232. {
  233. matrix[row - 1, col] = 'B';
  234. }
  235. if (IsValid(row + 1, col) == true && matrix[row + 1, col] != 'B')
  236. {
  237. matrix[row + 1, col] = 'B';
  238. }
  239. if (IsValid(row, col - 1) == true && matrix[row, col - 1] != 'B')
  240. {
  241. matrix[row, col - 1] = 'B';
  242. }
  243. if (IsValid(row, col + 1) == true && matrix[row, col + 1] != 'B')
  244. {
  245. matrix[row, col + 1] = 'B';
  246. }
  247. }
  248. indexBunnies.Clear();
  249. }
  250. }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement