package com.javarush.games.snake;
import java.util.List;
import java.util.ArrayList;
import com.javarush.engine.cell.*;
//Создаем змейку
public class Snake {
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);
}
private List<GameObject> snakeParts = new ArrayList<>();
private static final String HEAD_SIGN = "\uD83D\uDC7E";
private static final String BODY_SIGN = "\u26AB";
public boolean isAlive = true;
/*public void move(){
GameObject newHead = createNewHead();
if ((newHead.x < 0) || (newHead.x >= SnakeGame.WIDTH) || (newHead.y < 0) || (newHead.y >= SnakeGame.HEIGHT)) {
this.isAlive = false;
} else {
snakeParts.add(0, newHead);
removeTail();
}
}
*/
public void move(Apple apple) {
GameObject newHead = createNewHead();
if (newHead.x < 0 || newHead.x >= SnakeGame.WIDTH
|| newHead.y < 0 || newHead.y >= SnakeGame.HEIGHT || newHead == apple) {
isAlive = false;
}
if (checkCollision(newHead)) {
isAlive = false;
}
else if(newHead.x == apple.x && newHead.y == apple.y){
apple.isAlive = false;
snakeParts.add(0, newHead);
}
else {
snakeParts.add(0, newHead);
removeTail();
}
}
public boolean checkCollision(GameObject object) {
boolean check = false;
for (int i = 0; i < snakeParts.size(); i++)
if ((object.x == snakeParts.get(i).x) && (object.y == snakeParts.get(i).y))
{
check = true;
}
return check;
}
//Создание новой головы
public GameObject createNewHead (){
GameObject gameObject = new GameObject(0,0);
if (direction==Direction.UP){
gameObject = new GameObject(snakeParts.get(0).x, snakeParts.get(0).y-1);
}else if (direction==Direction.DOWN){
gameObject = new GameObject(snakeParts.get(0).x, snakeParts.get(0).y+1);
}else if (direction==Direction.LEFT){
gameObject = new GameObject(snakeParts.get(0).x-1, snakeParts.get(0).y);
}else if (direction==Direction.RIGHT){
gameObject = new GameObject(snakeParts.get(0).x+1, snakeParts.get(0).y);
}
return gameObject;
}
//Удаление последнего элемента
public void removeTail(){
snakeParts.remove(snakeParts.size()-1);
}
public int getLength() {
return snakeParts.size();
}
private Direction direction = Direction.LEFT;
//Выбираем сторону движения
public void setDirection(Direction direction) {
if (this.direction == Direction.LEFT && (direction == Direction.RIGHT || snakeParts.get(0).x == snakeParts.get(1).x))
return;
if (this.direction == Direction.RIGHT && (direction == Direction.LEFT || snakeParts.get(0).x == snakeParts.get(1).x))
return;
if (this.direction == Direction.UP && (direction == Direction.DOWN || snakeParts.get(0).y == snakeParts.get(1).y))
return;
if (this.direction == Direction.DOWN && (direction == Direction.UP || snakeParts.get(0).y == snakeParts.get(1).y))
return;
this.direction = direction;
}
/* public void setDirection(Direction direction){
if (direction == Direction.RIGHT && this.direction == Direction.LEFT)
return;
if (direction == Direction.LEFT && this.direction == Direction.RIGHT)
return;
if (direction == Direction.DOWN && this.direction == Direction.UP)
return;
if (direction == Direction.UP && this.direction == Direction.DOWN)
return;
this.direction = direction;
}
*/
/* game.setCellValue(snakeParts.get(0).x, snakeParts.get(0).y, HEAD_SIGN);
for (int i = 1; i < snakeParts.size(); i++) {
game.setCellValue(snakeParts.get(i).x, snakeParts.get(i).y, BODY_SIGN);
*/
//Отрисовка змейки
public void draw(Game game) {
Color cellColor;
if (isAlive) {
cellColor = Color.BLACK;
}
else {
cellColor = Color.RED;
}
game.setCellValueEx(snakeParts.get(0).x, snakeParts.get(0).y, Color.NONE, HEAD_SIGN, cellColor,75);
game.setCellValueEx(snakeParts.get(1).x, snakeParts.get(1).y, Color.NONE, BODY_SIGN, cellColor,75);
game.setCellValueEx(snakeParts.get(2).x, snakeParts.get(2).y, Color.NONE, BODY_SIGN, cellColor,75);
}
}
snakeParts.add(first);
snakeParts.add(second);
snakeParts.add(third);
}
private List<GameObject> snakeParts = new ArrayList<>();
private static final String HEAD_SIGN = "\uD83D\uDC7E";
private static final String BODY_SIGN = "\u26AB";
public boolean isAlive = true;
/*public void move(){
GameObject newHead = createNewHead();
if ((newHead.x < 0) || (newHead.x >= SnakeGame.WIDTH) || (newHead.y < 0) || (newHead.y >= SnakeGame.HEIGHT)) {
this.isAlive = false;
} else {
snakeParts.add(0, newHead);
removeTail();
}
}
*/
public void move(Apple apple) {
GameObject newHead = createNewHead();
if (newHead.x < 0 || newHead.x >= SnakeGame.WIDTH
|| newHead.y < 0 || newHead.y >= SnakeGame.HEIGHT || newHead == apple) {
isAlive = false;
}
if (checkCollision(newHead)) {
isAlive = false;
}
else if(newHead.x == apple.x && newHead.y == apple.y){
apple.isAlive = false;
snakeParts.add(0, newHead);
}
else {
snakeParts.add(0, newHead);
removeTail();
}
}
public boolean checkCollision(GameObject object) {
boolean check = false;
for (int i = 0; i < snakeParts.size(); i++)
if ((object.x == snakeParts.get(i).x) && (object.y == snakeParts.get(i).y))
{
check = true;
}
return check;
}
//Создание новой головы
public GameObject createNewHead (){
GameObject gameObject = new GameObject(0,0);
if (direction==Direction.UP){
gameObject = new GameObject(snakeParts.get(0).x, snakeParts.get(0).y-1);
}else if (direction==Direction.DOWN){
gameObject = new GameObject(snakeParts.get(0).x, snakeParts.get(0).y+1);
}else if (direction==Direction.LEFT){
gameObject = new GameObject(snakeParts.get(0).x-1, snakeParts.get(0).y);
}else if (direction==Direction.RIGHT){
gameObject = new GameObject(snakeParts.get(0).x+1, snakeParts.get(0).y);
}
return gameObject;
}
//Удаление последнего элемента
public void removeTail(){
snakeParts.remove(snakeParts.size()-1);
}
public int getLength() {
return snakeParts.size();
}
private Direction direction = Direction.LEFT;
//Выбираем сторону движения
public void setDirection(Direction direction) {
if (this.direction == Direction.LEFT && (direction == Direction.RIGHT || snakeParts.get(0).x == snakeParts.get(1).x))
return;
if (this.direction == Direction.RIGHT && (direction == Direction.LEFT || snakeParts.get(0).x == snakeParts.get(1).x))
return;
if (this.direction == Direction.UP && (direction == Direction.DOWN || snakeParts.get(0).y == snakeParts.get(1).y))
return;
if (this.direction == Direction.DOWN && (direction == Direction.UP || snakeParts.get(0).y == snakeParts.get(1).y))
return;
this.direction = direction;
}
/* public void setDirection(Direction direction){
if (direction == Direction.RIGHT && this.direction == Direction.LEFT)
return;
if (direction == Direction.LEFT && this.direction == Direction.RIGHT)
return;
if (direction == Direction.DOWN && this.direction == Direction.UP)
return;
if (direction == Direction.UP && this.direction == Direction.DOWN)
return;
this.direction = direction;
}
*/
/* game.setCellValue(snakeParts.get(0).x, snakeParts.get(0).y, HEAD_SIGN);
for (int i = 1; i < snakeParts.size(); i++) {
game.setCellValue(snakeParts.get(i).x, snakeParts.get(i).y, BODY_SIGN);
*/
//Отрисовка змейки
public void draw(Game game) {
Color cellColor;
if (isAlive) {
cellColor = Color.BLACK;
}
else {
cellColor = Color.RED;
}
game.setCellValueEx(snakeParts.get(0).x, snakeParts.get(0).y, Color.NONE, HEAD_SIGN, cellColor,75);
game.setCellValueEx(snakeParts.get(1).x, snakeParts.get(1).y, Color.NONE, BODY_SIGN, cellColor,75);
game.setCellValueEx(snakeParts.get(2).x, snakeParts.get(2).y, Color.NONE, BODY_SIGN, cellColor,75);
}
}