Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
4.9 kB
0
Indexable
Never
import java.util.Random;
import java.util.Scanner;

public class SnekGame {
    public static int gridSize;

//    public static char[][] grid = new char[gridSize][gridSize];
    public static char[][] grid;
    public static int snekLength = 3;
    public static int[][] snek = new int[100][2];
    public static int foodX;
    public static int foodY;
    public static int[][] obstacles = new int[3][2];


    public static void main(String[] args) {
        System.out.println("enter grid size: ");
        Scanner scanner = new Scanner(System.in);
        gridSize = scanner.nextInt();
        grid = new char[gridSize][gridSize];
        initializeGame();
        while (true) {
            displayGrid();

            char direction = getUserInput(scanner);

            moveSnek(direction);

            if (checkCollision()) {
                System.out.println("Game Over!");
                break;
            }
        }

        scanner.close();
    }

    public static void initializeGame(){
        snek[0] = new int[]{3, 6};
        snek[1] = new int[]{3, 7};
        snek[2] = new int[]{3, 8};
        obstacles[0] = new int[]{3, 3};
        obstacles[1] = new int[]{1, 1};
        obstacles[2] = new int[]{2, 2};

        generateFoodIfNeeded();

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                grid[i][j] = '.';
            }
        }

        for (int i = 0; i < snekLength; i++) {
            int x = snek[i][0];
            int y = snek[i][1];
            grid[x][y] = (i == 0) ? 'S' : 's';
        }
        for (int i = 0; i < obstacles.length; i++) {
            int x = obstacles[i][0];
            int y = obstacles[i][1];
            grid[x][y] = '#';
        }
        grid[foodX][foodY] = '@';
    }

    public static void displayGrid() {
        System.out.println("#".repeat(grid.length));
        for (int i = 1; i < grid.length - 1; i++) {
            System.out.print("#");
            for (int j = 1; j < grid[i].length - 1; j++) {
                if (grid[i][j] == ' '){
                    System.out.print(".");
                } else {
                    System.out.print(grid[i][j] + "");
                }
            }
            System.out.println("#");
        }
        System.out.println("#".repeat(grid.length));
    }

    public static char getUserInput(Scanner scanner) {
        System.out.print("Enter direction (w/a/s/d): ");
        Scanner scanner1 = new Scanner(System.in);
        String Stringinput = scanner1.nextLine();
        String input = Stringinput.toLowerCase();

        if (!input.isEmpty()) {
            return input.charAt(0);
        }
        return ' ';
    }

    public static void moveSnek(char direction) {
        int[] head = snek[0];
        int newHeadX = head[0];
        int newHeadY = head[1];

        switch (direction) {
            case 'w': newHeadX--; break;
            case 's': newHeadX++; break;
            case 'a': newHeadY--; break;
            case 'd': newHeadY++; break;
        }

        if (SnekUtils.isValidMove(newHeadX, newHeadY)) {
            SnekUtils.updateSnekPosition(newHeadX, newHeadY);
            if (newHeadX == foodX && newHeadY == foodY){
                generateFoodIfNeeded();
            }
        }
    }

    public static boolean checkCollision() {
        int headX = snek[0][0];
        int headY = snek[0][1];
        if (headX == 0 || headX == gridSize-1|| headY == 0 || headY ==  gridSize-1) {
            return true;
        }

        for (int i = 1; i < snekLength; i++) {
            if (headX == snek[i][0] && headY == snek[i][1]) {
                return true;
            }
        }

        for (int i=0; i<obstacles.length; i++){
            int obstaclesX = obstacles[i][0];
            int obstaclesY = obstacles[i][1];
            if (headX == obstaclesX && headY == obstaclesY){
                return true;
            }
        }

        return false;
    }

    public static void generateFoodIfNeeded() {

        int oldFoodX = foodX;
        int oldFoodY = foodY;

        Random random = new Random();
        boolean collision;
        do {
            foodX = random.nextInt(8) + 1; // So that coordinate (0, y) is not possible
            foodY = random.nextInt(8) + 1; // So that coordinate (x, 0) is not possible

            collision = false;
            for (int[] point : snek) {
                if (foodX == point[0] && foodY == point[1] && foodX == oldFoodX && foodY == oldFoodY) {
                    collision = true;
                    break;
                }
            }
        } while (collision);
    }

    public static void clearGrid() {
        for (int i = 0; i < gridSize; i++) {
            for (int j = 0; j < gridSize; j++) {
                grid[i][j] = ' ';
            }
        }
    }
}
Leave a Comment