Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TronRacers_v2 {
  4.     private static int firstPlayerRow = 0;
  5.     private static int firstPlayerCol = 0;
  6.     private static int secondPlayerRow = 0;
  7.     private static int secondlayerCol = 0;
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         int size = Integer.parseInt(scanner.nextLine());
  13.         char[][] matrix = new char[size][size];
  14.  
  15.         for (int row = 0; row < matrix.length; row++) {
  16.             char[] line = scanner.nextLine().toCharArray();
  17.             for (int col = 0; col < matrix[row].length; col++) {
  18.                 char symbol = line[col];
  19.                 matrix[row][col] = symbol;
  20.                 if (symbol == 'f') {
  21.                     firstPlayerRow = row;
  22.                     firstPlayerCol = col;
  23.  
  24.                 } else if (symbol == 's') {
  25.                     secondPlayerRow = row;
  26.                     secondlayerCol = col;
  27.                 }
  28.  
  29.             }
  30.         }
  31.         boolean isFirstPlayerAlive = true;
  32.         boolean isSecondPlayerAlive = true;
  33.  
  34.         while (isFirstPlayerAlive && isSecondPlayerAlive) {
  35.             String[] input = scanner.nextLine().split("\\s+");
  36.  
  37.             String firstPlayerPosition = input[0];
  38.             String secondPlayerPosition = input[1];
  39.  
  40.             moveFirstPlayer(firstPlayerPosition, size);
  41.             if (matrix[firstPlayerRow][firstPlayerCol] == 's') {
  42.                 matrix[firstPlayerRow][firstPlayerCol] = 'x';
  43.                 isFirstPlayerAlive = false;
  44.                 break;
  45.             } else {
  46.                 matrix[firstPlayerRow][firstPlayerCol] = 'f';
  47.             }
  48.  
  49.             moveSecondPlayer(secondPlayerPosition, size);
  50.             if (matrix[secondPlayerRow][secondlayerCol] == 'f') {
  51.                 matrix[secondPlayerRow][secondlayerCol] = 'x';
  52.                 isSecondPlayerAlive = false;
  53.             } else {
  54.                 matrix[secondPlayerRow][secondlayerCol] = 's';
  55.             }
  56.  
  57.         }
  58.         for (int r = 0; r <matrix.length ; r++) {
  59.             for (int c = 0; c < matrix.length; c++) {
  60.                 System.out.print(matrix[r][c]);
  61.             }
  62.             System.out.println();
  63.         }
  64.  
  65.     }
  66.  
  67.     private static void moveSecondPlayer(String secondPlayerPosition, int size) {
  68.         switch (secondPlayerPosition) {
  69.             case "down":
  70.                 if (secondPlayerRow == size - 1) {
  71.                     secondPlayerRow = 0;
  72.                 } else {
  73.                     secondPlayerRow++;
  74.                 }
  75.                 break;
  76.             case "up":
  77.                 if (secondPlayerRow == 0) {
  78.                     secondPlayerRow = size - 1;
  79.                 } else {
  80.                     secondPlayerRow--;
  81.                 }
  82.                 break;
  83.             case "right":
  84.                 if (secondlayerCol == size - 1) {
  85.                     secondlayerCol = 0;
  86.                 } else {
  87.                     secondlayerCol++;
  88.                 }
  89.                 break;
  90.             case "left":
  91.                 if (secondlayerCol == 0) {
  92.                     secondlayerCol = size - 1;
  93.                 } else {
  94.                     secondlayerCol--;
  95.                 }
  96.                 break;
  97.         }
  98.     }
  99.  
  100.     private static void moveFirstPlayer(String firstPlayerPosition, int size) {
  101.         switch (firstPlayerPosition) {
  102.             case "down":
  103.                 if (firstPlayerRow == size - 1) {
  104.                     firstPlayerRow = 0;
  105.                 } else {
  106.                     firstPlayerRow++;
  107.                 }
  108.                 break;
  109.             case "up":
  110.                 if (firstPlayerRow == 0) {
  111.                     firstPlayerRow = size - 1;
  112.                 } else {
  113.                     firstPlayerRow--;
  114.                 }
  115.                 break;
  116.             case "right":
  117.                 if (firstPlayerCol == size - 1) {
  118.                     firstPlayerCol = 0;
  119.                 } else {
  120.                     firstPlayerCol++;
  121.                 }
  122.                 break;
  123.             case "left":
  124.                 if (firstPlayerCol == 0) {
  125.                     firstPlayerCol = size - 1;
  126.                 } else {
  127.                     firstPlayerCol--;
  128.                 }
  129.                 break;
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement