Untitled

 avatar
unknown
plain_text
10 months ago
3.9 kB
8
Indexable
import java.util.Random;
import java.util.Scanner;

public class SnekGame {

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

    public static void main(String[] args) {
        initializeGame();

        Scanner scanner = new Scanner(System.in);
        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};

        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';
        }
        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): ");
        String input = scanner.nextLine().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 == 9 || headY == 0 || headY == 9) {
            return true;
        }

        for (int i = 1; i < snekLength; i++) {
            if (headX == snek[i][0] && headY == snek[i][1]) {
                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 < 10; i++) {
            for (int j = 0; j < 10; j++) {
                grid[i][j] = ' ';
            }
        }
    }
}
Editor is loading...
Leave a Comment