Advertisement
Mangovv

Untitled

Feb 17th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. package advanced_izpiti.february_24;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class TronRacers {
  6. private static char[][] matrix;
  7. private static int firstX;
  8. private static int firstY;
  9. private static int secondX;
  10. private static int secondY;
  11.  
  12.  
  13. public static void main(String[] args) {
  14. Scanner scanner = new Scanner(System.in);
  15. int n = Integer.parseInt(scanner.nextLine());
  16. matrix = new char[n][n];
  17. firstX = -1;
  18. firstY = -1;
  19. secondX = -1;
  20. secondY = -1;
  21. int position = -1;
  22. for (int i = 0; i < n; i++) {
  23. String line = scanner.nextLine();
  24. position = line.indexOf('f');
  25. if ( position >= 0 && position < n){
  26. firstX = i;
  27. firstY = position;
  28. }
  29. position = line.indexOf('s');
  30. if ( position >= 0 && position < n){
  31. secondX = i;
  32. secondY = position;
  33. }
  34. matrix[i] = line.toCharArray();
  35. }
  36. boolean killed = false;
  37. while ( !killed ){
  38. String[] input = scanner.nextLine().split("\\s+");
  39. killed = checkMovement('f' , input[0], firstX, firstY, matrix );
  40. if( killed == true ){
  41. break;
  42. }
  43. killed = checkMovement('s' , input[1], secondX, secondY, matrix );
  44.  
  45. // printMatrix(n, matrix);
  46. // System.out.println();
  47. }
  48.  
  49.  
  50. //System.out.println(firstX + " " + firstY);
  51. // System.out.println(secondX + " " + secondY );
  52. printMatrix(n , matrix);
  53.  
  54. // System.out.println();
  55. }
  56.  
  57. private static void printMatrix(int n, char[][] matrix) {
  58. for (int i = 0; i < n ; i++) {
  59. for (int j = 0; j < n; j++) {
  60. System.out.print(matrix[i][j]);
  61. }
  62. System.out.println();
  63. }
  64. }
  65.  
  66. private static boolean checkMovement(char player, String command, int x, int y, char[][] matrix) {
  67. boolean killed = false;
  68. switch (command){
  69. case "down":
  70. if( x + 1 == matrix.length ){
  71. x = 0;
  72. }else{
  73. x = x + 1;
  74. }
  75. break;
  76. case "right":
  77. if( y + 1 == matrix.length ){
  78. y = 0;
  79. }else{
  80. y = y + 1;
  81. }
  82. break;
  83. case "left":
  84. if( y - 1 < 0 ){
  85. y = matrix.length - 1;
  86. }else{
  87. y = y - 1;
  88. }
  89. break;
  90. case "up":
  91. if( x - 1 < 0 ){
  92. x = matrix.length - 1;
  93. }else{
  94. x = x - 1;
  95. }
  96. break;
  97. }//switch
  98. if( matrix[x][y] == '*' || matrix[x][y] == player){
  99. matrix[x][y] = player;
  100. if( player == 'f'){
  101. firstX = x;
  102. firstY = y;
  103. }else{
  104. secondX = x;
  105. secondY = y;
  106. }
  107.  
  108. }else{
  109. matrix[x][y] = 'x';
  110. killed = true;
  111. }
  112.  
  113.  
  114. return killed;
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement