Advertisement
Guest User

Re-Volt

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