Advertisement
IvaAnd

ReVolt

Oct 24th, 2020 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. package Exam20200222;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ReVolt {
  6. public static int newRow = 0;
  7. public static int newCol = 0;
  8.  
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. int n = Integer.parseInt(scanner.nextLine());
  13. int countCommands = Integer.parseInt(scanner.nextLine());
  14.  
  15. char[][] matrix = new char[n][n];
  16. // f - player
  17. int rowPlayer = 0;
  18. int colPlayer = 0;
  19.  
  20. for (int i = 0; i < matrix.length; i++) {
  21. String input = scanner.nextLine();
  22. if (input.contains("f")) {
  23. rowPlayer = i;
  24. colPlayer = input.indexOf("f");
  25. }
  26. addCharArrayAsRowInMatrix(matrix, i, input);
  27. }
  28.  
  29. while (countCommands-- > 0) { //&& !isOutOfBounds(rowPlayer, colPlayer, matrix)) {
  30. String command = scanner.nextLine();
  31.  
  32. if (command.equals("up")) {
  33. //row--
  34. newRow = rowPlayer - 1;
  35. newCol = colPlayer;
  36. moveUp(matrix, rowPlayer);
  37.  
  38. } else if (command.equals("down")) {
  39. //row++
  40. newRow = rowPlayer + 1;
  41. newCol = colPlayer;
  42. moveDown(matrix, rowPlayer);
  43.  
  44. } else if (command.equals("left")) {
  45. //col--
  46. newRow = rowPlayer;
  47. newCol = colPlayer - 1;
  48. moveLeft(matrix, rowPlayer, colPlayer);
  49.  
  50. } else if (command.equals("right")) {
  51. //col++
  52. newRow = rowPlayer;
  53. newCol = colPlayer + 1;
  54. moveRight(matrix, colPlayer);
  55. }
  56.  
  57. // B - bonus
  58. // T - trap
  59. // F - finish
  60. if (matrix[newRow][newCol] == 'F') {
  61. countCommands = 0;
  62. System.out.println("Player won!");
  63. }
  64. if (countCommands == 0 && matrix[newRow][newCol] != 'F') {
  65. System.out.println("Player lost!");
  66. }
  67.  
  68. matrix[rowPlayer][colPlayer] = '-';
  69. matrix[newRow][newCol] = 'f';
  70. rowPlayer = newRow;
  71. colPlayer = newCol;
  72. }
  73.  
  74. for (int i = 0; i < matrix.length; i++) {
  75. System.out.println(String.valueOf(matrix[i]));
  76. }
  77.  
  78. }
  79.  
  80. public static void moveRight(char[][] matrix, int colPlayer) {
  81. if (isOutOfBounds(newRow, newCol, matrix)) {
  82. newCol = 0;
  83. }
  84. if (matrix[newRow][newCol] == 'B') {
  85. newCol = newCol + 1;
  86. if (isOutOfBounds(newRow, newCol, matrix)) {
  87. newCol = 0;
  88. }
  89. } else if (matrix[newRow][newCol] == 'T') {
  90. newCol = colPlayer;
  91. }
  92. }
  93.  
  94. public static void moveLeft(char[][] matrix, int rowPlayer, int colPlayer) {
  95. if (isOutOfBounds(newRow, newCol, matrix)) {
  96. newCol = matrix[rowPlayer].length - 1;
  97. }
  98. if (matrix[newRow][newCol] == 'B') {
  99. newCol = newCol - 1;
  100. if (isOutOfBounds(newRow, newCol, matrix)) {
  101. newCol = matrix[rowPlayer].length - 1;
  102. }
  103. } else if (matrix[newRow][newCol] == 'T') {
  104. newCol = colPlayer;
  105. }
  106. }
  107.  
  108. public static void moveUp(char[][] matrix, int rowPlayer) {
  109. if (newRow< 0){
  110. newRow = matrix.length - 1;
  111. }
  112. if (matrix[newRow][newCol] == 'B') {
  113. newRow = newRow - 1;
  114.  
  115. } else if (matrix[newRow][newCol] == 'T') {
  116. newRow = rowPlayer;
  117. }
  118. }
  119.  
  120. public static void moveDown(char[][] matrix, int rowPlayer) {
  121. if (newRow >= matrix.length) {
  122. newRow = 0;
  123. }
  124. if (matrix[newRow][newCol] == 'B') {
  125. newRow = newRow + 1;
  126. if (newRow >= matrix.length) {
  127. newRow = 0;
  128. }
  129. } else if (matrix[newRow][newCol] == 'T') {
  130. newRow = rowPlayer;
  131. }
  132. }
  133.  
  134. public static void addCharArrayAsRowInMatrix(char[][] matrix, int i, String input) {
  135. char[] matrixRow = input.toCharArray();
  136. matrix[i] = matrixRow;
  137. }
  138.  
  139. private static boolean isOutOfBounds(int row, int col, char[][] matrix) {
  140. return row < 0 || row >= matrix.length
  141. || col < 0 || col >= matrix[row].length;
  142. }
  143.  
  144.  
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement