Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package advanced_izpiti.february_24;
- import java.util.Scanner;
- public class TronRacers {
- private static char[][] matrix;
- private static int firstX;
- private static int firstY;
- private static int secondX;
- private static int secondY;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- matrix = new char[n][n];
- firstX = -1;
- firstY = -1;
- secondX = -1;
- secondY = -1;
- int position = -1;
- for (int i = 0; i < n; i++) {
- String line = scanner.nextLine();
- position = line.indexOf('f');
- if ( position >= 0 && position < n){
- firstX = i;
- firstY = position;
- }
- position = line.indexOf('s');
- if ( position >= 0 && position < n){
- secondX = i;
- secondY = position;
- }
- matrix[i] = line.toCharArray();
- }
- boolean killed = false;
- while ( !killed ){
- String[] input = scanner.nextLine().split("\\s+");
- killed = checkMovement('f' , input[0], firstX, firstY, matrix );
- if( killed == true ){
- break;
- }
- killed = checkMovement('s' , input[1], secondX, secondY, matrix );
- // printMatrix(n, matrix);
- // System.out.println();
- }
- //System.out.println(firstX + " " + firstY);
- // System.out.println(secondX + " " + secondY );
- printMatrix(n , matrix);
- // System.out.println();
- }
- private static void printMatrix(int n, char[][] matrix) {
- for (int i = 0; i < n ; i++) {
- for (int j = 0; j < n; j++) {
- System.out.print(matrix[i][j]);
- }
- System.out.println();
- }
- }
- private static boolean checkMovement(char player, String command, int x, int y, char[][] matrix) {
- boolean killed = false;
- switch (command){
- case "down":
- if( x + 1 == matrix.length ){
- x = 0;
- }else{
- x = x + 1;
- }
- break;
- case "right":
- if( y + 1 == matrix.length ){
- y = 0;
- }else{
- y = y + 1;
- }
- break;
- case "left":
- if( y - 1 < 0 ){
- y = matrix.length - 1;
- }else{
- y = y - 1;
- }
- break;
- case "up":
- if( x - 1 < 0 ){
- x = matrix.length - 1;
- }else{
- x = x - 1;
- }
- break;
- }//switch
- if( matrix[x][y] == '*' || matrix[x][y] == player){
- matrix[x][y] = player;
- if( player == 'f'){
- firstX = x;
- firstY = y;
- }else{
- secondX = x;
- secondY = y;
- }
- }else{
- matrix[x][y] = 'x';
- killed = true;
- }
- return killed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement