Advertisement
cankocanko

ReVolt0

Aug 11th, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9.  
  10. int size = Integer.parseInt(sc.nextLine());
  11. int commands = Integer.parseInt(sc.nextLine());
  12.  
  13. String[][] matrix = new String[size][size];
  14.  
  15. for (int rows = 0; rows < matrix.length; rows++) {
  16. String[] arr = sc.nextLine().split("");
  17. matrix[rows] = arr;
  18. }
  19. int playerRow = 0;
  20. int playerCol = 0;
  21.  
  22. boolean win = false;
  23.  
  24. int previousField = 0;
  25. for (int i = 0; i < commands; i++) {
  26. if (win){
  27. break;
  28. }
  29. String command = sc.nextLine();
  30.  
  31.  
  32. for (int k = 0; k < matrix.length; k++) {
  33. for (int o = 0; o < matrix[k].length; o++) {
  34. if (matrix[k][o].equals("f")) {
  35. playerRow = k;
  36. playerCol = o;
  37. }
  38. }
  39. }
  40. int nextField = 0;
  41.  
  42. switch (command) {
  43. case "up":
  44. nextField = moveUp(matrix, playerRow);
  45. if (matrix[nextField][playerCol].equals("T")){
  46. break;
  47. }
  48. previousField = playerRow;
  49. playerRow = nextField;
  50.  
  51. switch (matrix[playerRow][playerCol]) {
  52.  
  53. case "-":
  54. matrix[previousField][playerCol] = "-";
  55. matrix[playerRow][playerCol] = "f";
  56. break;
  57. case "B":
  58. matrix[previousField][playerCol] = "-";
  59. nextField = moveUp(matrix, playerRow);
  60.  
  61. playerRow = nextField;
  62. matrix[playerRow][playerCol] = "f";
  63. break;
  64. case "F":
  65. matrix[previousField][playerCol] = "-";
  66. matrix[playerRow][playerCol] = "f";
  67. win = true;
  68. continue;
  69. }
  70. break;
  71. case "down":
  72. nextField = moveDown(matrix, playerRow);
  73. if (matrix[nextField][playerCol].equals("T")){
  74. break;
  75. }
  76. previousField = playerRow;
  77. playerRow = nextField;
  78.  
  79. switch (matrix[playerRow][playerCol]) {
  80.  
  81. case "-":
  82. matrix[previousField][playerCol] = "-";
  83. matrix[playerRow][playerCol] = "f";
  84. break;
  85. case "B":
  86. matrix[previousField][playerCol] = "-";
  87. nextField = moveDown(matrix, playerRow);
  88.  
  89. playerRow = nextField;
  90. matrix[playerRow][playerCol] = "f";
  91. break;
  92. case "F":
  93. matrix[previousField][playerCol] = "-";
  94. matrix[playerRow][playerCol] = "f";
  95. win = true;
  96. continue;
  97. }
  98. break;
  99. case "left":
  100. nextField = moveLeft(matrix, playerCol);
  101. if (matrix[playerRow][nextField].equals("T")){
  102. break;
  103. }
  104. previousField = playerCol;
  105. playerCol = nextField;
  106.  
  107. switch (matrix[playerRow][playerCol]) {
  108.  
  109. case "-":
  110. matrix[playerRow][previousField] = "-";
  111. matrix[playerRow][playerCol] = "f";
  112. break;
  113. case "B":
  114. matrix[playerRow][previousField] = "-";
  115. nextField = moveLeft(matrix, playerCol);
  116.  
  117. playerCol = nextField;
  118. matrix[playerRow][playerCol] = "f";
  119. break;
  120. case "F":
  121. matrix[playerRow][previousField] = "-";
  122. matrix[playerRow][playerCol] = "f";
  123. win = true;
  124. continue;
  125. }
  126. break;
  127. case "right":
  128. nextField = moveRight(matrix, playerCol);
  129. if (matrix[playerRow][nextField].equals("T")){
  130. break;
  131. }
  132. previousField = playerCol; // prev
  133. playerCol = nextField; // row = next
  134.  
  135. switch (matrix[playerRow][playerCol]) {
  136.  
  137. case "-":
  138. matrix[playerRow][previousField] = "-";
  139. matrix[playerRow][playerCol] = "f";
  140. break;
  141. case "B":
  142. matrix[playerRow][previousField] = "-";
  143. nextField = moveRight(matrix, playerRow);
  144.  
  145. playerCol = nextField;
  146. matrix[playerRow][playerCol] = "f";
  147. break;
  148. case "F":
  149. matrix[playerRow][previousField] = "-";
  150. matrix[playerRow][playerCol] = "f";
  151. win = true;
  152. continue;
  153. }
  154.  
  155. break;
  156. }
  157. }
  158. if (win) {
  159. System.out.println("Player won!");
  160. printMatrix(matrix);
  161. }
  162. if (!win) {
  163. System.out.println("Player lost!");
  164. printMatrix(matrix);
  165. }
  166. }
  167.  
  168. public static int moveUp(String[][] matrix, int playerRow) {
  169. if (playerRow > 0) {
  170. return playerRow - 1;
  171. } else {
  172. return matrix.length - 1;
  173. }
  174. }
  175.  
  176. public static int moveDown(String[][] matrix, int playerRow) {
  177. if (playerRow < matrix.length - 1) {
  178. return playerRow + 1;
  179. } else {
  180. return 0;
  181. }
  182. }
  183.  
  184. public static int moveLeft(String[][] matrix, int playerCol) {
  185. if (playerCol > 0) {
  186. return playerCol - 1;
  187. } else {
  188. return matrix.length - 1;
  189. }
  190. }
  191.  
  192. public static int moveRight(String[][] matrix, int playerCol) {
  193. if (playerCol < matrix.length - 1) {
  194. return playerCol + 1;
  195. } else {
  196. return 0;
  197. }
  198. }
  199.  
  200. public static void printMatrix(String[][] matrix) {
  201. for (int rows = 0; rows < matrix.length; rows++) {
  202. for (int cols = 0; cols < matrix[rows].length; cols++) {
  203. System.out.print(matrix[rows][cols]);
  204. }
  205. System.out.println();
  206. }
  207. }
  208. }
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement