Advertisement
Guest User

asd

a guest
Feb 24th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.62 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace MultidimensionalArrays
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int mSize = int.Parse(Console.ReadLine());
  11. int commandsCount = int.Parse(Console.ReadLine());
  12.  
  13. char[,] matrix = new char[mSize, mSize];
  14. int playerRow = 0;
  15. int playerCol = 0;
  16.  
  17. bool isWinner = false;
  18. MatrixInitialize(mSize, matrix, ref playerRow, ref playerCol);
  19.  
  20. while (commandsCount >= 0)
  21. {
  22. commandsCount--;
  23. string command = Console.ReadLine();
  24.  
  25. if (command == "up")
  26. {
  27. if (playerRow - 1 >= 0 && matrix[playerRow - 1, playerCol] == 'F')
  28. {
  29. matrix[playerRow, playerCol] = '-';
  30. playerRow--;
  31. matrix[playerRow, playerCol] = 'f';
  32. isWinner = true;
  33. break;
  34. }
  35. else if (playerRow - 1 >= 0 && matrix[playerRow - 1, playerCol] == '-')
  36. {
  37. matrix[playerRow, playerCol] = '-';
  38. playerRow--;
  39. matrix[playerRow, playerCol] = 'f';
  40. }
  41. else if (playerRow - 1 >= 0 && matrix[playerRow + 1, playerCol] == 'B')
  42. {
  43. if (playerRow - 2 >= 0 )
  44. {
  45. matrix[playerRow, playerCol] = '-';
  46. playerRow--;
  47. playerRow--;
  48. matrix[playerRow, playerCol] = 'f';
  49. }
  50. else
  51. {
  52. matrix[playerRow, playerCol] = '-';
  53. playerRow = mSize - 1;
  54. matrix[playerRow, playerCol] = 'f';
  55. }
  56. }
  57. else if(playerRow - 1 < 0)
  58. {
  59. if (matrix[mSize-1, playerCol] == 'F')
  60. {
  61. matrix[playerRow, playerCol] = '-';
  62. playerRow = mSize - 1;
  63. matrix[playerRow, playerCol] = 'f';
  64. isWinner = true;
  65. break;
  66. }
  67. else if (matrix[mSize - 1, playerCol] == '-')
  68. {
  69. matrix[playerRow, playerCol] = '-';
  70. playerRow = mSize - 1;
  71. matrix[playerRow, playerCol] = 'f';
  72. }
  73.  
  74. }
  75. }
  76. else if (command == "down")
  77. {
  78. if (playerRow + 1 < mSize && matrix[playerRow + 1, playerCol] == 'F')
  79. {
  80. matrix[playerRow, playerCol] = '-';
  81. playerRow++;
  82. matrix[playerRow, playerCol] = 'f';
  83. isWinner = true;
  84. break;
  85. }
  86. else if (playerRow + 1 < mSize && matrix[playerRow + 1, playerCol] == '-')
  87. {;
  88. matrix[playerRow, playerCol] = '-';
  89. playerRow++;
  90. matrix[playerRow, playerCol] = 'f';
  91. }
  92. else if (playerRow + 1 < mSize && matrix[playerRow + 1,playerCol] == 'B')
  93. {
  94. if (playerRow + 2 < mSize)
  95. {
  96. matrix[playerRow, playerCol] = '-';
  97. playerRow++;
  98. playerRow++;
  99. matrix[playerRow, playerCol] = 'f';
  100. }
  101. else
  102. {
  103. matrix[playerRow, playerCol] = '-';
  104. playerRow = 0;
  105. matrix[playerRow, playerCol] = 'f';
  106. }
  107. }
  108. else if (playerRow + 1 == mSize)
  109. {
  110. if (matrix[0, playerCol] == 'F')
  111. {
  112. matrix[playerRow, playerCol] = '-';
  113. playerRow = 0;
  114. matrix[playerRow, playerCol] = 'f';
  115. isWinner = true;
  116. break;
  117. }
  118. else if (matrix[0, playerCol] == '-')
  119. {
  120. matrix[playerRow, playerCol] = '-';
  121. playerRow = 0;
  122. matrix[playerRow, playerCol] = 'f';
  123. }
  124. else if (matrix[0, playerCol] == 'B')
  125. {
  126. matrix[playerRow, playerCol] = '-';
  127. playerRow = 1;
  128. matrix[playerRow, playerCol] = 'f';
  129. }
  130.  
  131. }
  132. }
  133. else if (command == "left")
  134. {
  135. if (playerCol - 1 >= 0 && matrix[playerRow, playerCol - 1] == 'F')
  136. {
  137. matrix[playerRow, playerCol] = '-';
  138. playerCol--;
  139. matrix[playerRow, playerCol] = 'f';
  140. isWinner = true;
  141. break;
  142. }
  143. else if (playerCol - 1 >= 0 && matrix[playerRow,playerCol - 1] == '-')
  144. {
  145. matrix[playerRow, playerCol] = '-';
  146. playerCol--;
  147. matrix[playerRow, playerCol] = 'f';
  148. }
  149. else if (playerCol - 1 >= 0 && matrix[playerRow,playerCol - 1] == 'B')
  150. {
  151. if (playerCol - 2 >= 0)
  152. {
  153. matrix[playerRow, playerCol] = '-';
  154. playerCol--;
  155. playerCol--;
  156. matrix[playerRow, playerCol] = 'f';
  157. }
  158. else
  159. {
  160. matrix[playerRow, playerCol] = '-';
  161. playerCol = mSize - 1;
  162. matrix[playerRow, playerCol] = 'f';
  163. }
  164. }
  165. else if (playerCol - 1 < 0)
  166. {
  167. if (matrix[playerRow , mSize - 1] == 'F')
  168. {
  169. matrix[playerRow, playerCol] = '-';
  170. playerCol = mSize - 1;
  171. matrix[playerRow, playerRow] = 'f';
  172. isWinner = true;
  173. break;
  174. }
  175. else if (matrix[playerRow, mSize - 1] == '-')
  176. {
  177. matrix[playerRow, playerCol] = '-';
  178. playerRow = mSize - 1;
  179. matrix[playerRow, playerCol] = 'f';
  180. }
  181. else if (matrix[playerRow, mSize - 1] == 'B')
  182. {
  183. matrix[playerRow, playerCol] = '-';
  184. playerRow = mSize - 2;
  185. matrix[playerRow, playerCol] = 'f';
  186. }
  187.  
  188. }
  189.  
  190. }
  191. else if (command == "right")
  192. {
  193. if (playerCol + 1 < mSize && matrix[playerRow, playerCol + 1] == 'F')
  194. {
  195. matrix[playerRow, playerCol] = '-';
  196. playerCol++;
  197. matrix[playerRow, playerCol] = 'f';
  198. isWinner = true;
  199. break;
  200. }
  201. else if (playerCol + 1 < mSize && matrix[playerRow,playerCol + 1] == '-')
  202. {
  203. matrix[playerRow, playerCol] = '-';
  204. playerCol++;
  205. matrix[playerRow, playerCol] = 'f';
  206. }
  207. else if (playerCol + 1 < mSize && matrix[playerRow,playerCol + 1] == 'B')
  208. {
  209. if (playerCol + 2 < mSize)
  210. {
  211. matrix[playerRow, playerCol] = '-';
  212. playerCol++;
  213. playerCol++;
  214. matrix[playerRow, playerCol] = 'f';
  215. }
  216. else
  217. {
  218. matrix[playerRow, playerCol] = '-';
  219. playerCol = 0;
  220. matrix[playerRow, playerCol] = 'f';
  221. }
  222. }
  223. else if (playerCol + 1 == mSize)
  224. {
  225. if (matrix[playerRow, 0] == 'F')
  226. {
  227. matrix[playerRow, playerCol] = '-';
  228. playerCol = 0;
  229. matrix[playerRow, playerCol] = 'f';
  230. isWinner = true;
  231. break;
  232. }
  233. else if (matrix[playerRow, 0] == '-')
  234. {
  235. matrix[playerRow, playerCol] = '-';
  236. playerCol = 0;
  237. matrix[playerRow, playerCol] = 'f';
  238. }
  239. else if (matrix[playerRow, 0] == 'B')
  240. {
  241. matrix[playerRow, playerCol] = '-';
  242. playerCol = 1;
  243. matrix[playerRow, playerCol] = 'f';
  244. }
  245.  
  246. }
  247.  
  248. }
  249.  
  250. } // while
  251. if (isWinner)
  252. {
  253. Console.WriteLine("Player won!");
  254. }
  255. else
  256. {
  257. Console.WriteLine("Player lost!");
  258. }
  259. for (int row = 0; row < mSize; row++)
  260. {
  261. for (int col = 0; col < mSize; col++)
  262. {
  263. Console.Write(matrix[row,col]);
  264. }
  265. Console.WriteLine();
  266. }
  267. }
  268.  
  269. private static void MatrixInitialize(int mSize, char[,] matrix, ref int playerRow, ref int playerCol)
  270. {
  271. for (int rows = 0; rows < mSize; rows++)
  272. {
  273. var consoleInput = Console.ReadLine().ToCharArray();
  274. for (int cols = 0; cols < mSize; cols++)
  275. {
  276. if (consoleInput[cols] != '-')
  277. {
  278. if (consoleInput[cols] == 'f')
  279. {
  280. playerRow = rows;
  281. playerCol = cols;
  282. }
  283. }
  284. matrix[rows, cols] = consoleInput[cols];
  285. }
  286. }
  287. }
  288. }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement