Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class TronRacers {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int size = Integer.parseInt(reader.readLine());
- String[][] matrix = new String[size][size];
- int firstRow = 0, firstCol = 0;
- int secondRow = 0, secondCol = 0;
- boolean foundFirst = false;
- boolean foundSecond = false;
- for (int i = 0; i < matrix.length; i++) {
- String[] input = reader.readLine().split("");
- matrix[i] = input;
- if (!foundFirst || !foundSecond) {
- for (int j = 0; j < matrix[i].length; j++) {
- if (matrix[i][j].equals("f")) {
- firstRow = i;
- firstCol = j;
- foundFirst = true;
- break;
- } else if (matrix[i][j].equals("s")) {
- secondRow = i;
- secondCol = j;
- foundSecond = true;
- break;
- }
- }
- }
- }
- while (true) {
- String[] commands = reader.readLine().split("\\s+");
- String firstPlayerCommand = commands[0];
- String secondPlayerCommand = commands[1];
- switch (firstPlayerCommand) {
- case "up":
- if (firstRow > 0) {
- firstRow--;
- if (matrix[firstRow][firstCol].equals("s")) {
- matrix[firstRow][firstCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[firstRow][firstCol] = "f";
- }
- } else {
- firstRow = matrix.length - 1;
- if (matrix[firstRow][firstCol].equals("s")) {
- matrix[firstRow][firstCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[firstRow][firstCol] = "f";
- }
- }
- break;
- case "down":
- if (firstRow < matrix.length - 1) {
- firstRow++;
- if (matrix[firstRow][firstCol].equals("s")) {
- matrix[firstRow][firstCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[firstRow][firstCol] = "f";
- }
- } else if (firstRow == matrix.length - 1) {
- firstRow = 0;
- if (matrix[firstRow][firstCol].equals("s")) {
- matrix[firstRow][firstCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[firstRow][firstCol] = "f";
- }
- }
- break;
- case "left":
- if (firstCol > 0) {
- firstCol--;
- if (matrix[firstRow][firstCol].equals("s")) {
- matrix[firstRow][firstCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[firstRow][firstCol] = "f";
- }
- } else {
- firstCol = matrix[firstRow].length - 1;
- if (matrix[firstRow][firstCol].equals("s")) {
- matrix[firstRow][firstCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[firstRow][firstCol] = "f";
- }
- }
- break;
- case "right":
- if (firstCol < matrix[firstRow].length - 1) {
- firstCol++;
- if (matrix[firstRow][firstCol].equals("s")) {
- matrix[firstRow][firstCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[firstRow][firstCol] = "f";
- }
- } else if (firstCol == matrix[firstRow].length - 1) {
- firstCol = 0;
- if (matrix[firstRow][firstCol].equals("s")) {
- matrix[firstRow][firstCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[firstRow][firstCol] = "f";
- }
- }
- break;
- default:
- break;
- }
- switch (secondPlayerCommand) {
- case "up":
- if (secondRow > 0) {
- secondRow--;
- if (matrix[secondRow][secondCol].equals("f")) {
- matrix[secondRow][secondCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[secondRow][secondCol] = "s";
- }
- } else {
- secondRow = matrix.length - 1;
- if (matrix[secondRow][secondCol].equals("f")) {
- matrix[secondRow][secondCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[secondRow][secondCol] = "s";
- }
- }
- break;
- case "down":
- if (secondRow < matrix.length - 1) {
- secondRow++;
- if (matrix[secondRow][secondCol].equals("f")) {
- matrix[secondRow][secondCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[secondRow][secondCol] = "s";
- }
- } else if (secondRow >= matrix.length - 1) {
- secondRow = 0;
- if (matrix[secondRow][secondCol].equals("f")) {
- matrix[secondRow][secondCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[secondRow][secondCol] = "s";
- }
- }
- break;
- case "left":
- if (secondCol > 0) {
- secondCol--;
- if (matrix[secondRow][secondCol].equals("f")) {
- matrix[secondRow][secondCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[secondRow][secondCol] = "s";
- }
- } else {
- secondCol = matrix[secondRow].length - 1;
- if (matrix[secondRow][secondCol].equals("f")) {
- matrix[secondRow][secondCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[secondRow][secondCol] = "s";
- }
- }
- break;
- case "right":
- if (secondCol < matrix[secondRow].length - 1) {
- secondCol++;
- if (matrix[secondRow][secondCol].equals("f")) {
- matrix[secondRow][secondCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[secondRow][secondCol] = "s";
- }
- } else if (secondCol == matrix[secondRow].length - 1) {
- secondCol = 0;
- if (matrix[secondRow][secondCol].equals("f")) {
- matrix[secondRow][secondCol] = "x";
- printMatrix(matrix);
- return;
- } else {
- matrix[secondRow][secondCol] = "s";
- }
- }
- break;
- default:
- break;
- }
- }
- }
- private static void printMatrix(String[][] matrix) {
- for (String[] strings : matrix) {
- for (String string : strings) {
- System.out.print(string);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement