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;
- import java.util.Scanner;
- public class Task_2 {
- private static int[] snakePosition;
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int size = Integer.parseInt(reader.readLine());
- snakePosition = new int[2];
- char[][] matrix = new char[size][size];
- int[] burrowFirst = new int[2];
- int[] burrowSecond = new int[2];
- boolean hasBurrow = false;
- int countBurrow = 0;
- for (int row = 0; row < matrix.length; row++) {
- char[] innerArray = reader.readLine().toCharArray();
- for (int col = 0; col < innerArray.length; col++) {
- matrix[row][col] = innerArray[col];
- if (innerArray[col] == 'S') {
- snakePosition[0] = row;
- snakePosition[1] = col;
- }
- if (innerArray[col] == 'B') {
- hasBurrow = true;
- countBurrow++;
- if (countBurrow == 1) {
- burrowFirst[0] = row;
- burrowFirst[1] = col;
- } else if (countBurrow == 2) {
- burrowSecond[0] = row;
- burrowSecond[1] = col;
- }
- }
- }
- }
- int foodQuantity = 0;
- String line;
- while (!(line = reader.readLine()).equals("end")) {
- int moveToRow = snakePosition[0];
- int moveToCol = snakePosition[1];
- matrix[moveToRow][moveToCol] = '.';
- switch (line) {
- case "up":
- moveToRow--;
- break;
- case "down":
- moveToRow++;
- break;
- case "left":
- moveToCol--;
- break;
- case "right":
- moveToCol++;
- break;
- }
- if (!isSafe(moveToRow, moveToCol, matrix)) {
- System.out.println("Game over!");
- break;
- }
- char current = matrix[moveToRow][moveToCol];
- if (current == '*') {
- foodQuantity++;
- snakePosition[0] = moveToRow;
- snakePosition[1] = moveToCol;
- matrix[moveToRow][moveToCol] = 'S';
- }
- if (current == '-') {
- snakePosition[0] = moveToRow;
- snakePosition[1] = moveToCol;
- matrix[moveToRow][moveToCol] = 'S';
- }
- if (current == 'B') {
- if (moveToRow == burrowFirst[0] && moveToCol == burrowFirst[1]) {
- matrix[moveToRow][moveToCol] = '.';
- snakePosition[0] = burrowSecond[0];
- snakePosition[1] = burrowSecond[1];
- matrix[burrowSecond[0]][burrowSecond[1]] = 'S';
- } else if (moveToRow == burrowSecond[0] && moveToCol == burrowSecond[1]) {
- matrix[moveToRow][moveToCol] = '.';
- snakePosition[0] = burrowFirst[0];
- snakePosition[1] = burrowFirst[1];
- matrix[burrowFirst[0]][burrowFirst[1]] = 'S';
- }
- }
- if (foodQuantity >= 10) {
- System.out.println("You won! You fed the snake.");
- break;
- }
- }
- System.out.println("Food eaten: " + foodQuantity);
- printCharMatrix(matrix);
- }
- private static void printCharMatrix(char[][] finalMatrix) {
- for (char[] matrix : finalMatrix) {
- for (char symbol : matrix) {
- System.out.print(symbol);
- }
- System.out.println();
- }
- }
- private static boolean isSafe(int rowToMove, int colToMove, char[][] matrix) {
- return rowToMove >= 0 && rowToMove < matrix.length && colToMove >= 0 && colToMove < matrix[rowToMove].length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement