Advertisement
Guest User

Untitled

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