package com.codegym.games.snake; import com.codegym.engine.cell.Color; import com.codegym.engine.cell.Game; import java.util.ArrayList; import java.util.List; public class Snake { private List<GameObject> snakeParts = new ArrayList<GameObject>(); private static final String HEAD_SIGN = "\uD83D\uDC7E"; private static final String BODY_SIGN = "\u26AB"; public boolean isAlive= true; private Direction direction = Direction.LEFT; public void setDirection(Direction direction){ if(direction == Direction.LEFT && this.direction != Direction.RIGHT){ if(this.direction != Direction.LEFT && snakeParts.get(0).x != snakeParts.get(1).x) this.direction = direction; } else if(direction == Direction.RIGHT && this.direction != Direction.LEFT){ if(this.direction != Direction.RIGHT && snakeParts.get(0).x != snakeParts.get(1).x) this.direction = direction; } else if(direction == Direction.UP && this.direction != Direction.DOWN){ if(this.direction != Direction.UP && snakeParts.get(0).y != snakeParts.get(1).y) this.direction = direction; } else if(direction == Direction.DOWN && this.direction != Direction.UP) { if(this.direction != Direction.DOWN && snakeParts.get(0).y != snakeParts.get(1).y) this.direction = direction; } } public Snake(int x, int 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){ for(GameObject obj : snakeParts){ if (snakeParts.indexOf(obj)==0){ if(isAlive == false) game.setCellValueEx(obj.x, obj.y, Color.NONE, HEAD_SIGN, Color.RED, 75); else game.setCellValueEx(obj.x, obj.y, Color.NONE, HEAD_SIGN, Color.BLACK, 75); } else if(snakeParts.indexOf(obj)>0){ if (isAlive == false) game.setCellValueEx(obj.x, obj.y, Color.NONE, BODY_SIGN, Color.RED, 75); else game.setCellValueEx(obj.x, obj.y, Color.NONE, BODY_SIGN, Color.BLACK, 75); } } } public void move(Apple apple){ GameObject newHead = createNewHead(); if(checkCollision(newHead)) isAlive = false; else{ if(newHead.x < 0 || newHead.y < 0 || newHead.x >= SnakeGame.WIDTH || newHead.y >= SnakeGame.HEIGHT){ isAlive = false; }else{ snakeParts.add(0, newHead); if(newHead.x == apple.x && newHead.y == apple.y){ apple.isAlive = false; }else{ removeTail(); } }} } public boolean checkCollision(GameObject gameObject){ boolean collide = false; for (GameObject element: snakeParts) { if(element.x == gameObject.x && element.y == gameObject.y) { collide = true; } } return collide; } public int getLength(){ return snakeParts.size(); } public GameObject createNewHead(){ GameObject newHead = snakeParts.get(0); if (direction == Direction.LEFT) { return new GameObject(newHead.x - 1, newHead.y); } else if (direction == Direction.RIGHT) { return new GameObject(newHead.x + 1, newHead.y); } else if (direction == Direction.DOWN) { return new GameObject(newHead.x, newHead.y + 1); } else { return new GameObject(newHead.x, newHead.y-1); } } public void removeTail(){ snakeParts.remove(snakeParts.get(snakeParts.size()-1)); } }