Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.51 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class TronRacers {
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  8.  
  9.         int size = Integer.parseInt(reader.readLine());
  10.         String[][] matrix = new String[size][size];
  11.  
  12.         int firstRow = 0, firstCol = 0;
  13.         int secondRow = 0, secondCol = 0;
  14.         boolean foundFirst = false;
  15.         boolean foundSecond = false;
  16.         for (int i = 0; i < matrix.length; i++) {
  17.             String[] input = reader.readLine().split("");
  18.             matrix[i] = input;
  19.             if (!foundFirst || !foundSecond) {
  20.                 for (int j = 0; j < matrix[i].length; j++) {
  21.                     if (matrix[i][j].equals("f")) {
  22.                         firstRow = i;
  23.                         firstCol = j;
  24.                         foundFirst = true;
  25.                         break;
  26.                     } else if (matrix[i][j].equals("s")) {
  27.                         secondRow = i;
  28.                         secondCol = j;
  29.                         foundSecond = true;
  30.                         break;
  31.                     }
  32.                 }
  33.             }
  34.         }
  35.  
  36.         while (true) {
  37.             String[] commands = reader.readLine().split("\\s+");
  38.             String firstPlayerCommand = commands[0];
  39.             String secondPlayerCommand = commands[1];
  40.  
  41.             switch (firstPlayerCommand) {
  42.                 case "up":
  43.                     if (firstRow > 0) {
  44.                         firstRow--;
  45.                         if (matrix[firstRow][firstCol].equals("s")) {
  46.                             matrix[firstRow][firstCol] = "x";
  47.                             printMatrix(matrix);
  48.                             return;
  49.                         } else {
  50.                             matrix[firstRow][firstCol] = "f";
  51.                         }
  52.                     } else {
  53.                         firstRow = matrix.length - 1;
  54.                         if (matrix[firstRow][firstCol].equals("s")) {
  55.                             matrix[firstRow][firstCol] = "x";
  56.                             printMatrix(matrix);
  57.                             return;
  58.                         } else {
  59.                             matrix[firstRow][firstCol] = "f";
  60.                         }
  61.                     }
  62.                     break;
  63.                 case "down":
  64.                     if (firstRow < matrix.length - 1) {
  65.                         firstRow++;
  66.                         if (matrix[firstRow][firstCol].equals("s")) {
  67.                             matrix[firstRow][firstCol] = "x";
  68.                             printMatrix(matrix);
  69.                             return;
  70.                         } else {
  71.                             matrix[firstRow][firstCol] = "f";
  72.                         }
  73.                     } else if (firstRow == matrix.length - 1) {
  74.                         firstRow = 0;
  75.                         if (matrix[firstRow][firstCol].equals("s")) {
  76.                             matrix[firstRow][firstCol] = "x";
  77.                             printMatrix(matrix);
  78.                             return;
  79.                         } else {
  80.                             matrix[firstRow][firstCol] = "f";
  81.                         }
  82.                     }
  83.                     break;
  84.                 case "left":
  85.                     if (firstCol > 0) {
  86.                         firstCol--;
  87.                         if (matrix[firstRow][firstCol].equals("s")) {
  88.                             matrix[firstRow][firstCol] = "x";
  89.                             printMatrix(matrix);
  90.                             return;
  91.                         } else {
  92.                             matrix[firstRow][firstCol] = "f";
  93.                         }
  94.                     } else {
  95.                         firstCol = matrix[firstRow].length - 1;
  96.                         if (matrix[firstRow][firstCol].equals("s")) {
  97.                             matrix[firstRow][firstCol] = "x";
  98.                             printMatrix(matrix);
  99.                             return;
  100.                         } else {
  101.                             matrix[firstRow][firstCol] = "f";
  102.                         }
  103.                     }
  104.                     break;
  105.                 case "right":
  106.                     if (firstCol < matrix[firstRow].length - 1) {
  107.                         firstCol++;
  108.                         if (matrix[firstRow][firstCol].equals("s")) {
  109.                             matrix[firstRow][firstCol] = "x";
  110.                             printMatrix(matrix);
  111.                             return;
  112.                         } else {
  113.                             matrix[firstRow][firstCol] = "f";
  114.                         }
  115.                     } else if (firstCol == matrix[firstRow].length - 1) {
  116.                         firstCol = 0;
  117.                         if (matrix[firstRow][firstCol].equals("s")) {
  118.                             matrix[firstRow][firstCol] = "x";
  119.                             printMatrix(matrix);
  120.                             return;
  121.                         } else {
  122.                             matrix[firstRow][firstCol] = "f";
  123.                         }
  124.                     }
  125.                     break;
  126.                 default:
  127.                     break;
  128.             }
  129.  
  130.             switch (secondPlayerCommand) {
  131.                 case "up":
  132.                     if (secondRow > 0) {
  133.                         secondRow--;
  134.                         if (matrix[secondRow][secondCol].equals("f")) {
  135.                             matrix[secondRow][secondCol] = "x";
  136.                             printMatrix(matrix);
  137.                             return;
  138.                         } else {
  139.                             matrix[secondRow][secondCol] = "s";
  140.                         }
  141.                     } else {
  142.                         secondRow = matrix.length - 1;
  143.                         if (matrix[secondRow][secondCol].equals("f")) {
  144.                             matrix[secondRow][secondCol] = "x";
  145.                             printMatrix(matrix);
  146.                             return;
  147.                         } else {
  148.                             matrix[secondRow][secondCol] = "s";
  149.                         }
  150.                     }
  151.                     break;
  152.                 case "down":
  153.                     if (secondRow < matrix.length - 1) {
  154.                         secondRow++;
  155.                         if (matrix[secondRow][secondCol].equals("f")) {
  156.                             matrix[secondRow][secondCol] = "x";
  157.                             printMatrix(matrix);
  158.                             return;
  159.                         } else {
  160.                             matrix[secondRow][secondCol] = "s";
  161.                         }
  162.                     } else if (secondRow >= matrix.length - 1) {
  163.                         secondRow = 0;
  164.                         if (matrix[secondRow][secondCol].equals("f")) {
  165.                             matrix[secondRow][secondCol] = "x";
  166.                             printMatrix(matrix);
  167.                             return;
  168.                         } else {
  169.                             matrix[secondRow][secondCol] = "s";
  170.                         }
  171.                     }
  172.                     break;
  173.                 case "left":
  174.                     if (secondCol > 0) {
  175.                         secondCol--;
  176.                         if (matrix[secondRow][secondCol].equals("f")) {
  177.                             matrix[secondRow][secondCol] = "x";
  178.                             printMatrix(matrix);
  179.                             return;
  180.                         } else {
  181.                             matrix[secondRow][secondCol] = "s";
  182.                         }
  183.                     } else {
  184.                         secondCol = matrix[secondRow].length - 1;
  185.                         if (matrix[secondRow][secondCol].equals("f")) {
  186.                             matrix[secondRow][secondCol] = "x";
  187.                             printMatrix(matrix);
  188.                             return;
  189.                         } else {
  190.                             matrix[secondRow][secondCol] = "s";
  191.                         }
  192.                     }
  193.                     break;
  194.                 case "right":
  195.                     if (secondCol < matrix[secondRow].length - 1) {
  196.                         secondCol++;
  197.                         if (matrix[secondRow][secondCol].equals("f")) {
  198.                             matrix[secondRow][secondCol] = "x";
  199.                             printMatrix(matrix);
  200.                             return;
  201.                         } else {
  202.                             matrix[secondRow][secondCol] = "s";
  203.                         }
  204.                     } else if (secondCol == matrix[secondRow].length - 1) {
  205.                         secondCol = 0;
  206.                         if (matrix[secondRow][secondCol].equals("f")) {
  207.                             matrix[secondRow][secondCol] = "x";
  208.                             printMatrix(matrix);
  209.                             return;
  210.                         } else {
  211.                             matrix[secondRow][secondCol] = "s";
  212.                         }
  213.                     }
  214.                     break;
  215.                 default:
  216.                     break;
  217.             }
  218.         }
  219.     }
  220.  
  221.     private static void printMatrix(String[][] matrix) {
  222.         for (String[] strings : matrix) {
  223.             for (String string : strings) {
  224.                 System.out.print(string);
  225.             }
  226.             System.out.println();
  227.         }
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement