Advertisement
Kent_St_1

FotrmulaJavaAdvancedRetakeExam18August2021

Oct 5th, 2021
1,298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class FormulaOne_02 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int size = Integer.parseInt(scanner.nextLine()); //брой на редове и брой на колони
  7.         int countCommands = Integer.parseInt(scanner.nextLine());
  8.         char[][] matrix = new char[size][size];
  9.  
  10.         fillMatrix(scanner, size, matrix);
  11.         //P -> позиция на играча
  12.         //F -> финал
  13.         //B -> бонус
  14.         //T -> капан
  15.  
  16.         //първоначална позиция на играча
  17.         int rowPlayer = 0;
  18.         int colPlayer = 0;
  19.         boolean isFindPollPosition = false;
  20.         for (int row = 0; row < size; row++) {
  21.             for (int col = 0; col < size; col++) {
  22.                 char currentElement = matrix[row][col];
  23.                 if (currentElement == 'P') {
  24.                     rowPlayer = row;
  25.                     colPlayer = col;
  26.                     isFindPollPosition = true;
  27.                     break;
  28.                 }
  29.  
  30.             }
  31.             if(isFindPollPosition) {
  32.                 break;
  33.             }
  34.         }
  35.  
  36.         boolean hasWon = false;
  37.         for (int commandCount = 1; commandCount <= countCommands; commandCount++) {
  38.             String direction = scanner.nextLine();
  39.  
  40.             //up, down, left or right
  41.             int newRow = 0; //редът след преместване
  42.             int newCol = 0; //колоната след преместване
  43.             switch (direction) {
  44.                 case "up":
  45.                     //row - 1
  46.                     newRow = checkIsOutside(rowPlayer - 1, size); //TODO: ПРОВЕРКА ДАЛИ РЕДЪТ В МАТРИЦАТА
  47.                     newCol = checkIsOutside(colPlayer, size); //TODO: ПРОВЕРКА ДАЛИ колоната В МАТРИЦАТА
  48.                     if (matrix[newRow][newCol] == 'B') {
  49.                         newRow = checkIsOutside(rowPlayer - 2, size);
  50.                         newCol = checkIsOutside(colPlayer, size);
  51.                     }
  52.                     break;
  53.                 case "down":
  54.                     //row + 1
  55.                     newRow = checkIsOutside(rowPlayer + 1, size);
  56.                     newCol = checkIsOutside(colPlayer, size);
  57.                     if (matrix[newRow][newCol] == 'B') {
  58.                         newRow = checkIsOutside(rowPlayer + 2, size);
  59.                         newCol = checkIsOutside(colPlayer, size);
  60.                     }
  61.                     break;
  62.                 case "left":
  63.                     //col - 1
  64.                     newRow = checkIsOutside(rowPlayer, size);
  65.                     newCol = checkIsOutside(colPlayer - 1, size);
  66.                     if (matrix[newRow][newCol] == 'B') {
  67.                         newRow = checkIsOutside(rowPlayer, size);
  68.                         newCol = checkIsOutside(colPlayer - 2, size);
  69.                     }
  70.                     break;
  71.                 case "right":
  72.                     //col + 1
  73.                     newRow = checkIsOutside(rowPlayer, size);
  74.                     newCol = checkIsOutside(colPlayer + 1, size);
  75.                     if (matrix[newRow][newCol] == 'B') {
  76.                         newRow = checkIsOutside(rowPlayer, size);
  77.                         newCol = checkIsOutside(colPlayer + 2, size);
  78.                     }
  79.                     break;
  80.             }
  81.             //проверка: на новата позиция имаме ли B, T, F
  82.             if (matrix[newRow][newCol] == 'T') {
  83.                 //нямаме преместване
  84.                 newRow = rowPlayer;
  85.                 newCol = colPlayer;
  86.             } else {
  87.                 //има преместаване
  88.                 if (matrix[newRow][newCol] == 'F') {
  89.                     hasWon = true;
  90.                 }
  91.                 matrix[rowPlayer][colPlayer] = '.';
  92.                 matrix[newRow][newCol] = 'P';
  93.                 rowPlayer = newRow;
  94.                 colPlayer = newCol;
  95.  
  96.             }
  97.  
  98.  
  99.         }
  100.  
  101.  
  102.         if (hasWon) {
  103.             System.out.println("Well done, the player won first place!");
  104.         } else {
  105.             System.out.println("Oh no, the player got lost on the track!");
  106.         }
  107.  
  108.         printMatrix(matrix);
  109.  
  110.  
  111.     }
  112.  
  113.     private static int checkIsOutside(int value, int size) {
  114.         //ако е вътрев матрицата ред или колона -> връщам стойността
  115.         //ако е извън -> връщаме новата стойност
  116.         if (value < 0) {
  117.             value = size - 1;
  118.         } else if (value >= size ) {
  119.             value = 0;
  120.         }
  121.         return value;
  122.     }
  123.  
  124.     private static void printMatrix(char[][] matrix) {
  125.         for (int row = 0; row < matrix.length; row++) {
  126.             for (int col = 0; col < matrix.length; col++) {
  127.                 System.out.print(matrix[row][col]);
  128.             }
  129.             System.out.println();
  130.         }
  131.     }
  132.  
  133.     private static void fillMatrix(Scanner scanner, int size, char[][] matrix) {
  134.         //fillMatrix
  135.         for (int row = 0; row < size; row++) {
  136.             String line = scanner.nextLine();
  137.             matrix[row] = line.toCharArray();
  138.         }
  139.     }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement