Untitled

 avatar
unknown
plain_text
2 months ago
3.1 kB
4
Indexable
package org.example;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Random;

public class SnakeGame {
    public enum Direction {
        UP, DOWN, LEFT, RIGHT;
    }

    private static final int WIDTH = 3;
    private static final int HEIGHT = 3;

    private Deque<int[]> snake;

    private Direction direction;
    private int[] food;
    private boolean gameOver;

    public SnakeGame() {
        snake = new ArrayDeque<>();
        snake.addFirst(new int[]{0, 0});
        food = new int[2];
        direction = Direction.RIGHT;
        placeFood();
        gameOver = false;
    }

    private void placeFood() {
        Random random = new Random();
        food[0] = random.nextInt(WIDTH);
        food[1] = random.nextInt(HEIGHT);
    }

    public void move() {
        if(gameOver) {
            return;
        }
        int[] head = snake.peekFirst();
        int[] newHead = new int[]{head[0], head[1]};

        switch(direction) {
            case UP:
                newHead[0]--;
                break;
            case DOWN:
                newHead[0]++;
                break;
            case LEFT:
                newHead[1]--;
                break;
            case RIGHT:
                newHead[1]++;
                break;
        }

        if(newHead[0] < 0 || newHead[0] >= HEIGHT || newHead[1] < 0 || newHead[1] >= WIDTH) {
            gameOver = true;
            return;
        }

        for(int [] body: snake) {
            if(body[0] == newHead[0] && body[1] == newHead[1]) {
                gameOver = true;
                return;
            }
        }
        snake.push(newHead);
        if(newHead[0] == food[0] && newHead[1] == food[1]) {
            placeFood();
        } else {
            snake.removeLast();
        }
    }

    public void changeDirection(Direction newDirection) {
        if((direction == Direction.UP && newDirection != Direction.DOWN)
                || (direction == Direction.DOWN && newDirection != Direction.UP)
                || (direction == Direction.LEFT && newDirection != Direction.RIGHT)
                || (direction == Direction.RIGHT && newDirection != Direction.LEFT)) {

            direction = newDirection;
        }
    }

    public void display() {
        for(int i = 0; i < HEIGHT; i++) {
            for(int j = 0; j < WIDTH; j++) {
                boolean printed = false;
                for(int[] body: snake) {
                    if(body[0] == i && body[1] == j) {
                        System.out.print("S ");
                        printed = true;
                        break;
                    }
                }
                if(!printed && food[0] == i && food[1] == j) {
                    System.out.print("F ");
                    printed = true;
                }

                if(!printed) {
                    System.out.print(". ");
                }
            }
            System.out.println();
        }
        System.out.println();
    }

    public boolean isGameOver() {
        return gameOver;
    }

}
Editor is loading...
Leave a Comment