Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int size = Integer.parseInt(sc.nextLine());
- String[][] matrix = new String[size][size];
- for (int row = 0; row < size; row++) {
- String[] arr = sc.nextLine().split("");
- matrix[row] = arr;
- }
- int food = 0;
- String command = sc.nextLine();
- int snakeRow = 0;
- int snakeCol = 0;
- while (food < 10) {
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- if (matrix[rows][cols].equals("S")) {
- snakeRow = rows;
- snakeCol = cols;
- }
- }
- }
- switch (command) {
- case "up":
- if (snakeRow > 0) {
- //check if food
- if (matrix[snakeRow - 1][snakeCol].equals("*")) {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow - 1][snakeCol] = "S";
- snakeRow -= 1;
- food += 1;
- break;
- }
- if (matrix[snakeRow - 1][snakeCol].equals("B")) {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow - 1][snakeCol] = ".";
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- if (matrix[rows][cols].equals("B")) {
- matrix[rows][cols] = "S";
- snakeRow = rows;
- snakeCol = cols;
- }
- }
- }
- } else {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow - 1][snakeCol] = "S";
- snakeRow -= 1;
- }
- } else {
- System.out.println("Game over!");
- System.out.println(String.format("Food eaten: %d", food));
- matrix[snakeRow][snakeCol] = ".";
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- System.out.print(matrix[rows][cols]);
- }
- System.out.println();
- }
- }
- break;
- case "down":
- if (snakeRow < matrix.length - 1) {
- //check if food
- if (matrix[snakeRow + 1][snakeCol].equals("*")) {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow + 1][snakeCol] = "S";
- snakeRow += 1;
- food += 1;
- break;
- }
- if (matrix[snakeRow + 1][snakeCol].equals("B")) {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow + 1][snakeCol] = ".";
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- if (matrix[rows][cols].equals("B")) {
- matrix[rows][cols] = "S";
- snakeRow = rows;
- snakeCol = cols;
- }
- }
- }
- } else {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow + 1][snakeCol] = "S";
- snakeRow += 1;
- }
- } else {
- System.out.println("Game over!");
- System.out.println(String.format("Food eaten: %d", food));
- matrix[snakeRow][snakeCol] = ".";
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- System.out.print(matrix[rows][cols]);
- }
- System.out.println();
- }
- }
- break;
- case "left":
- if (snakeCol > 0) {
- //check if food
- if (matrix[snakeRow][snakeCol - 1].equals("*")) {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow][snakeCol - 1] = "S";
- snakeCol -= 1;
- food += 1;
- break;
- }
- if (matrix[snakeRow][snakeCol - 1].equals("B")) {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow][snakeCol - 1] = ".";
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- if (matrix[rows][cols].equals("B")) {
- matrix[rows][cols] = "S";
- snakeRow = rows;
- snakeCol = cols;
- }
- }
- }
- } else {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow][snakeCol - 1] = "S";
- snakeCol -= 1;
- }
- } else {
- System.out.println("Game over!");
- System.out.println(String.format("Food eaten: %d", food));
- matrix[snakeRow][snakeCol] = ".";
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- System.out.print(matrix[rows][cols]);
- }
- System.out.println();
- }
- }
- break;
- case "right":
- if (snakeCol < matrix.length - 1) {
- //check if food
- if (matrix[snakeRow][snakeCol + 1].equals("*")) {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow][snakeCol + 1] = "S";
- snakeCol += 1;
- food += 1;
- break;
- }
- if (matrix[snakeRow][snakeCol + 1].equals("B")) {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow][snakeCol + 1] = ".";
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- if (matrix[rows][cols].equals("B")) {
- matrix[rows][cols] = "S";
- snakeRow = rows;
- snakeCol = cols;
- }
- }
- }
- } else {
- matrix[snakeRow][snakeCol] = ".";
- matrix[snakeRow += 1][snakeCol] = "S";
- snakeCol += 1;
- }
- } else {
- System.out.println("Game over!");
- System.out.println(String.format("Food eaten: %d", food));
- matrix[snakeRow][snakeCol] = ".";
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- System.out.print(matrix[rows][cols]);
- }
- System.out.println();
- }
- }
- break;
- }
- if (food >= 10){
- System.out.println("You won! You fed the snake.");
- System.out.println(String.format("Food eaten: %d",food));
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- System.out.print(matrix[rows][cols]);
- }
- System.out.println();
- }
- break;
- }
- command = sc.nextLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement