Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TronRacers_v2 {
- private static int firstPlayerRow = 0;
- private static int firstPlayerCol = 0;
- private static int secondPlayerRow = 0;
- private static int secondlayerCol = 0;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int size = Integer.parseInt(scanner.nextLine());
- char[][] matrix = new char[size][size];
- for (int row = 0; row < matrix.length; row++) {
- char[] line = scanner.nextLine().toCharArray();
- for (int col = 0; col < matrix[row].length; col++) {
- char symbol = line[col];
- matrix[row][col] = symbol;
- if (symbol == 'f') {
- firstPlayerRow = row;
- firstPlayerCol = col;
- } else if (symbol == 's') {
- secondPlayerRow = row;
- secondlayerCol = col;
- }
- }
- }
- boolean isFirstPlayerAlive = true;
- boolean isSecondPlayerAlive = true;
- while (isFirstPlayerAlive && isSecondPlayerAlive) {
- String[] input = scanner.nextLine().split("\\s+");
- String firstPlayerPosition = input[0];
- String secondPlayerPosition = input[1];
- moveFirstPlayer(firstPlayerPosition, size);
- if (matrix[firstPlayerRow][firstPlayerCol] == 's') {
- matrix[firstPlayerRow][firstPlayerCol] = 'x';
- isFirstPlayerAlive = false;
- break;
- } else {
- matrix[firstPlayerRow][firstPlayerCol] = 'f';
- }
- moveSecondPlayer(secondPlayerPosition, size);
- if (matrix[secondPlayerRow][secondlayerCol] == 'f') {
- matrix[secondPlayerRow][secondlayerCol] = 'x';
- isSecondPlayerAlive = false;
- } else {
- matrix[secondPlayerRow][secondlayerCol] = 's';
- }
- }
- for (int r = 0; r <matrix.length ; r++) {
- for (int c = 0; c < matrix.length; c++) {
- System.out.print(matrix[r][c]);
- }
- System.out.println();
- }
- }
- private static void moveSecondPlayer(String secondPlayerPosition, int size) {
- switch (secondPlayerPosition) {
- case "down":
- if (secondPlayerRow == size - 1) {
- secondPlayerRow = 0;
- } else {
- secondPlayerRow++;
- }
- break;
- case "up":
- if (secondPlayerRow == 0) {
- secondPlayerRow = size - 1;
- } else {
- secondPlayerRow--;
- }
- break;
- case "right":
- if (secondlayerCol == size - 1) {
- secondlayerCol = 0;
- } else {
- secondlayerCol++;
- }
- break;
- case "left":
- if (secondlayerCol == 0) {
- secondlayerCol = size - 1;
- } else {
- secondlayerCol--;
- }
- break;
- }
- }
- private static void moveFirstPlayer(String firstPlayerPosition, int size) {
- switch (firstPlayerPosition) {
- case "down":
- if (firstPlayerRow == size - 1) {
- firstPlayerRow = 0;
- } else {
- firstPlayerRow++;
- }
- break;
- case "up":
- if (firstPlayerRow == 0) {
- firstPlayerRow = size - 1;
- } else {
- firstPlayerRow--;
- }
- break;
- case "right":
- if (firstPlayerCol == size - 1) {
- firstPlayerCol = 0;
- } else {
- firstPlayerCol++;
- }
- break;
- case "left":
- if (firstPlayerCol == 0) {
- firstPlayerCol = size - 1;
- } else {
- firstPlayerCol--;
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement