package com.javarush.games.snake;
import com.javarush.engine.cell.Game;
import com.javarush.engine.cell.*;
public class SnakeGame extends Game
{
    private int score;
    private void win()
    {
        stopTurnTimer();
        isGameStopped = true;
        showMessageDialog(Color.BLACK, "Красава", Color.BLUE, 4);
    }
    private static final int GOAL = 28;
    private void gameOver()
    {
        stopTurnTimer();
        isGameStopped = true;
        showMessageDialog(Color.BLACK, "Капец", Color.BLUE, 4);
    }
    private boolean isGameStopped;
    private void createNewApple()
    {
        apple = new Apple(getRandomNumber(WIDTH),getRandomNumber(HEIGHT));
        while(snake.checkCollision(apple)) apple = new Apple(getRandomNumber(WIDTH), getRandomNumber(HEIGHT));
    }
    private Apple apple;
    public void onKeyPress(Key key)
    {
        switch(key)
        {
            case LEFT: snake.setDirection(Direction.LEFT); break;
            case RIGHT: snake.setDirection(Direction.RIGHT); break;
            case UP: snake.setDirection(Direction.UP); break;
            case DOWN: snake.setDirection(Direction.DOWN); break;
            case SPACE: if(isGameStopped) createGame();
            default: break;
        }
    }
    private int turnDelay;
    public void onTurn(int a1)
    {
        if(!apple.isAlive)
        {
            createNewApple();
            score += 5;
            setScore(score);
            turnDelay -= 10;
            setTurnTimer(turnDelay);
        }
        snake.move(apple);
        if(!snake.isAlive) gameOver();
        if(snake.getLength() > GOAL) win();
        drawScene();
    }
    public static final int WIDTH = 15;
    public static final int HEIGHT = 15;
    private Snake snake;
    @Override
    public void initialize() {
        setScreenSize(WIDTH, HEIGHT);
        createGame();
    }

    private void createGame(){
        score = 0;
        setScore(score);
        snake = new Snake(WIDTH / 2, HEIGHT / 2);
        createNewApple();
        isGameStopped = false;
        drawScene();
        turnDelay = 300;
        setTurnTimer(turnDelay);
    }

    private void drawScene(){
        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                setCellValueEx(i, j, Color.DARKSEAGREEN, "");
            }
        }
        snake.draw(this);
        apple.draw(this);
    }
}
package com.javarush.games.snake;

public class GameObject
{

    public int x;
    public int y;

    public GameObject(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
package com.javarush.games.snake;
import java.util.List;
import java.util.ArrayList;
import com.javarush.engine.cell.*;

public class Snake
{
    public int getLength()
    {
        return snakeParts.size();
    }
    public boolean checkCollision(GameObject a)
    {
        int i = 0;
        while(i < snakeParts.size())
        {
            if(a.x == snakeParts.get(i).x && a.y == snakeParts.get(i).y) return true;
            i++;
        }
        return false;
    }
    public GameObject createNewHead()
    {
        GameObject isntReadySnakeParts = null;
        if(direction == Direction.UP) isntReadySnakeParts = new GameObject(snakeParts.get(0).x, snakeParts.get(0).y - 1);
        else if(direction == Direction.LEFT) isntReadySnakeParts = new GameObject(snakeParts.get(0).x - 1, snakeParts.get(0).y);
        else if(direction == Direction.DOWN) isntReadySnakeParts = new GameObject(snakeParts.get(0).x, snakeParts.get(0).y + 1);
        else if(direction == Direction.RIGHT) isntReadySnakeParts = new GameObject(snakeParts.get(0).x + 1, snakeParts.get(0).y);
        GameObject DoesIt = isntReadySnakeParts;
        return  DoesIt;
    }
    public void removeTail()
    {
        snakeParts.remove(snakeParts.size() - 1);
    }
    public void move(Apple apple)
    {
        GameObject GO = createNewHead();
        boolean ShouldIt = true;
        if(GO.x == apple.x && GO.y == apple.y)
        {
            apple.isAlive = false;
            ShouldIt = false;
        }
        if(checkCollision(createNewHead())) isAlive = false;
        else if(GO.x > 14 || GO.x < 0) isAlive = false;
        else if(GO.y > 14 || GO.y < 0) isAlive = false;
        else
        {
            snakeParts.add(0, GO);
            if(ShouldIt) removeTail();
        }
    }
    public void setDirection(Direction direction)
    {
        boolean willIDo = true;
        if(this.direction == Direction.LEFT && snakeParts.get(0).x == snakeParts.get(1).x) willIDo = false;
        else if(this.direction == Direction.RIGHT && snakeParts.get(0).x == snakeParts.get(1).x) willIDo = false;
        else if(this.direction == Direction.UP && snakeParts.get(0).y == snakeParts.get(1).y) willIDo = false;
        else if(this.direction == Direction.DOWN && snakeParts.get(0).y == snakeParts.get(1).y) willIDo = false;
        else if(this.direction == direction.LEFT && direction != direction.RIGHT && willIDo) this.direction = direction;
        else if(this.direction == direction.RIGHT && direction != direction.LEFT) this.direction = direction;
        else if(this.direction == direction.UP && direction != direction.DOWN) this.direction = direction;
        else if(this.direction == direction.DOWN && direction != direction.UP) this.direction = direction;
    }
    public boolean isAlive = true;
    public void draw(Game game)
    {
        Color snakecolor;
        if(isAlive == true) snakecolor = Color.BLACK;
        else snakecolor = Color.RED;
        game.setCellValueEx
        (
        snakeParts.get(0).x,
        snakeParts.get(0).y,
        Color.NONE,
        HEAD_SIGN,
        snakecolor,
        75
        );
        int i = 1;
        while(i < snakeParts.size())
        {
            game.setCellValueEx
            (
            snakeParts.get(i).x,
            snakeParts.get(i).y,
            Color.NONE,
            BODY_SIGN,
            snakecolor,
            75
            );
            i++;
        }
    }
    private Direction direction = Direction.LEFT;
    private static final String HEAD_SIGN = "\uD83D\uDC7E";
    private static final String BODY_SIGN = "\u26AB";
    private List<GameObject> snakeParts = new ArrayList<>();
    public Snake(int x, int y)
    {
        for (int i = 0; i < 3; i++)
        {
            snakeParts.add(new GameObject(x + i, y));
        }
    }
}
package com.javarush.games.snake;
public enum Direction{UP, RIGHT, DOWN, LEFT};
package com.javarush.games.snake;

import com.javarush.engine.cell.*;

public class Apple extends GameObject
{
    public boolean isAlive = true;
    private static final String APPLE_SIGN = "\uD83C\uDF4E";

    public Apple(int x, int y)
    {
        super(x, y);
    }

    public void draw(Game game) {
        game.setCellValueEx(x, y, Color.NONE, APPLE_SIGN, Color.GREEN, 75);
    }
}