-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnake.java
62 lines (50 loc) · 1.07 KB
/
Snake.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// To represent a snake
import java.util.LinkedList;
public class Snake {
private LinkedList<Cell> snakePartList = new LinkedList<>();
private Cell head;
public Snake(Cell initPos)
{
head = initPos;
snakePartList.add(head);
}
public void grow()
{
snakePartList.add(head);
}
public void move(Cell nextCell)
{
System.out.println("Snake is moving to " +
nextCell.getRow() + " " + nextCell.getCol());
Cell tail = snakePartList.removeLast();
tail.setCellType(CellType.EMPTY);
head = nextCell;
snakePartList.addFirst(head);
}
public boolean checkCrash(Cell nextCell)
{
System.out.println("Going to check for Crash");
for (Cell cell : snakePartList) {
if (cell == nextCell) {
return true;
}
}
return false;
}
public LinkedList<Cell> getSnakePartList()
{
return snakePartList;
}
public void setSnakePartList(LinkedList<Cell> snakePartList)
{
this.snakePartList = snakePartList;
}
public Cell getHead()
{
return head;
}
public void setHead(Cell head)
{
this.head = head;
}
}