where is problem
package com.codegym.games.snake;
import com.codegym.engine.cell.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Snake extends GameObject {
private static final String HEAD_SIGN = "\uD83D\uDC66\uD83C\uDFFF";
private static List<String> bodies = Arrays.asList("👩","👱♀","👩🦰","👩🦱","👩🦳");
public boolean isAlive = true;
private Direction direction = Direction.LEFT;
public List<GameObject> snakeParts = new ArrayList<>();
public void setDirection(Direction direction) {
if (direction.equals(Direction.LEFT) && (snakeParts.get(0).x==snakeParts.get(1).x)) return;
else if (direction.equals(Direction.RIGHT) && (snakeParts.get(0).x==snakeParts.get(1).x)) return;
else if (direction.equals(Direction.UP) && (snakeParts.get(0).y==snakeParts.get(1).y)) return;
else if (direction.equals(Direction.DOWN) && (snakeParts.get(0).y==snakeParts.get(1).y)) return;
this.direction=direction;
/*What I think it's correct:
if (direction.equals(Direction.LEFT) && (snakeParts.get(0).y!=snakeParts.get(1).y)) this.direction = direction;
else if (direction.equals(Direction.RIGHT) && (snakeParts.get(0).y!=snakeParts.get(1).y)) this.direction = direction;
else if (direction.equals(Direction.UP) && (snakeParts.get(0).x!=snakeParts.get(1).x)) this.direction = direction;
else if (direction.equals(Direction.DOWN) && (snakeParts.get(0).x!=snakeParts.get(1).x)) this.direction = direction;
*/
//Neither of them checks.
}
public Direction getDirection() {
return direction;
}
public Snake(int x, int y) {
super(x, y);
GameObject first = new GameObject(x,y);
GameObject second = new GameObject(x+1,y);
GameObject third = new GameObject(x+2,y);
snakeParts.add(first);
snakeParts.add(second);
snakeParts.add(third);
}
public void draw(Game game){
Color snakeColor = isAlive ? Color.BLACK : Color.LIGHTGRAY;
for (int i = 0; i < snakeParts.size(); i++) {
if (i==0) game.setCellValueEx(snakeParts.get(i).x,snakeParts.get(i).y,Color.NONE,HEAD_SIGN,Color.SADDLEBROWN,75);
else game.setCellValueEx(snakeParts.get(i).x,snakeParts.get(i).y,Color.NONE,bodies.get((int)(Math.random()*5)),snakeColor,75);
}
}
public void move(Apple apple){
GameObject newHead = createNewHead();
if ((newHead.x<0) || (newHead.y<0) || (newHead.x==15) || (newHead.y==15)) isAlive=false;
else {
if (checkCollision(newHead)) isAlive=false;
else {
snakeParts.add(0, newHead);
if (apple.x == newHead.x && apple.y == newHead.y) apple.isAlive = false;
else removeTail();
}
}
}
public void removeTail(){
snakeParts.remove(snakeParts.size()-1);
}
public GameObject createNewHead(){
if (direction.equals(Direction.LEFT)) {
x--;
return new GameObject(x,y);
}
else if (direction.equals(Direction.RIGHT)) {
x++;
return new GameObject(x,y);
}
else if (direction.equals(Direction.UP)) {
y--;
return new GameObject(x,y);
}
else if (direction.equals(Direction.DOWN)) {
y++;
return new GameObject(x,y);
}
return new GameObject(x,y);
}
public boolean checkCollision(GameObject gameObject){
for (GameObject snakePart : snakeParts){
if (gameObject.x==snakePart.x && gameObject.y==snakePart.y) return true;
}
return false;
}
public int getLength(){
return snakeParts.size();
}
}