Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ExamProblems;
- import java.util.Scanner;
- public class ReVolt {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- int commandsCount = Integer.parseInt(scanner.nextLine());
- char[][] matrix = new char[n][n];
- for (int row = 0; row < n; row++) {
- matrix[row]=scanner.nextLine().toCharArray();
- }
- int playerRow=0;
- int playerCol =0;
- boolean hasWon = false;
- for (int row = 0; row < n; row++) {
- for (int col = 0; col < n; col++) {
- if (matrix[row][col]=='f'){
- playerRow=row;
- playerCol=col;
- }
- }
- }
- for (int i = 0; i <commandsCount ; i++) {
- String command = scanner.nextLine();
- if (matrix[playerRow][playerCol] != 'B'&&matrix[playerRow][playerCol] != 'T'&& matrix[playerRow][playerCol] != 'F'){
- matrix[playerRow][playerCol]='-';
- }
- switch (command){
- case "up":
- playerRow--;
- if (playerRow<0){
- playerRow=n-1;
- }
- if (matrix[playerRow][playerCol]=='B'){
- playerRow--;
- if (playerRow<0){
- playerRow=n-1;
- }
- } else if (matrix[playerRow][playerCol]=='T'){
- playerRow++;
- if (playerRow>n-1){
- playerRow=0;
- }
- } else if (matrix[playerRow][playerCol]=='F'){
- hasWon= true;
- matrix[playerRow][playerCol]='f';
- System.out.println("Player won!");
- printMatrix(matrix,n);
- return;
- }else {
- matrix[playerRow][playerCol]= 'f';
- }
- break;
- case "down":
- playerRow++;
- if (playerRow>n-1){
- playerRow=0;
- }
- if (matrix[playerRow][playerCol]=='B'){
- playerRow++;
- if (playerRow>n-1){
- playerRow=0;
- }
- } else if (matrix[playerRow][playerCol]=='T'){
- playerRow--;
- if (playerRow<0){
- playerRow=n-1;
- }
- } else if (matrix[playerRow][playerCol]=='F'){
- hasWon= true;
- matrix[playerRow][playerCol]='f';
- System.out.println("Player won!");
- printMatrix(matrix,n);
- return;
- } else {
- matrix[playerRow][playerCol]= 'f';
- }
- break;
- case "left":
- playerCol--;
- if (playerCol<0){
- playerCol=n-1;
- }
- if (matrix[playerRow][playerCol]=='B'){
- playerCol--;
- if (playerCol<0){
- playerCol=n-1;
- }
- } else if (matrix[playerRow][playerCol]=='T'){
- playerCol++;
- if (playerCol>n-1){
- playerCol=0;
- }
- } else if (matrix[playerRow][playerCol]=='F'){
- hasWon= true;
- matrix[playerRow][playerCol]='f';
- System.out.println("Player won!");
- printMatrix(matrix,n);
- return;
- }else {
- matrix[playerRow][playerCol]= 'f';
- }
- break;
- case "right":
- playerCol++;
- if (playerCol>n-1){
- playerCol=0;
- }
- if (matrix[playerRow][playerCol]=='B'){
- playerCol++;
- if (playerCol>n-1){
- playerCol=0;
- }
- } else if (matrix[playerRow][playerCol]=='T'){
- playerCol--;
- if (playerCol<0){
- playerCol=n-1;
- }
- } else if (matrix[playerRow][playerCol]=='F'){
- hasWon= true;
- matrix[playerRow][playerCol]='f';
- System.out.println("Player won!");
- printMatrix(matrix,n);
- return;
- } else {
- matrix[playerRow][playerCol]= 'f';
- }
- break;
- }
- }
- matrix[playerRow][playerCol]= 'f';
- System.out.println("Player lost!");
- printMatrix(matrix,n);
- }
- private static void printMatrix(char [][] matrix, int n) {
- for (int row = 0; row < n; row++) {
- for (int col = 0; col < n; col++) {
- System.out.print(matrix[row][col]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement