-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.java
164 lines (122 loc) · 2.85 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.awt.Point;
import java.util.LinkedList;
public class Snake {
public enum DIR {UP, DOWN, RIGHT, LEFT};
private Model _model;
private DIR _heading;
private LinkedList<DIR> _newHeadings;
private LinkedList<Cell> _body;
private Point _headLocation;
private int food;
private int length;
public Snake(Model model)
{
_model = model;
_heading = GameManager.STARTING_HEADING;
_headLocation = new Point(GameManager.ARENA_WIDTH/2, GameManager.ARENA_HEIGHT-1);
length = 0;
food = 0;
_body = new LinkedList<Cell>();
_newHeadings = new LinkedList<DIR>();
try {
spawnCell((int)_headLocation.getX(), (int) _headLocation.getY());
} catch (SnakeCrashException e) {}
}
private void spawnCell(int x, int y) throws SnakeCrashException
{
x = x < 0 ? GameManager.ARENA_WIDTH - 1 : x;
y = y < 0 ? GameManager.ARENA_HEIGHT - 1 : y;
x = x % GameManager.ARENA_WIDTH;
y = y % GameManager.ARENA_HEIGHT;
Cell c;
try {
c = _model.getCell(x, y);
} catch (ArrayIndexOutOfBoundsException e) { throw new SnakeCrashException(); }
if (c.isAlive())
{ throw new SnakeCrashException(); }
if(c.isSeed())
{
food = GameManager.FOOD_VALUE;
length = food;
_model.plantSeed();
}
_body.addFirst(c);
_headLocation = new Point(x, y);
c.spawn();
}
public void requestChangeHeading(DIR newHeading)
{
if(_newHeadings.size() < 3)
_newHeadings.addLast(newHeading);
}
public void slither() throws SnakeCrashException
{
moveHead();
if(food < 1)
{
moveTail();
}
else
{
food--;
}
}
private void moveHead() throws SnakeCrashException
{
updateHeading();
switch(_heading){
case UP:
spawnCell((int)_headLocation.getX(),(int) _headLocation.getY() - 1);
break;
case DOWN:
spawnCell((int)_headLocation.getX(),(int) _headLocation.getY() 1);
break;
case RIGHT:
spawnCell((int)_headLocation.getX() 1,(int) _headLocation.getY());
break;
case LEFT:
spawnCell((int)_headLocation.getX() - 1,(int) _headLocation.getY());
break;
}
}
private void updateHeading()
{
while(true)
{
if(!_newHeadings.isEmpty())
{
DIR newHeading = _newHeadings.getFirst();
_newHeadings.removeFirst();
if(headingIsValid(newHeading))
{
_heading = newHeading;
break;
}
}
else
break;
}
}
private boolean headingIsValid(DIR newHeading)
{
if(newHeading == DIR.RIGHT && _heading == DIR.LEFT)
return false;
if(newHeading == DIR.LEFT && _heading == DIR.RIGHT)
return false;
if(newHeading == DIR.UP && _heading == DIR.DOWN)
return false;
if(newHeading == DIR.DOWN && _heading == DIR.UP)
return false;
return true;
}
private void moveTail()
{
Cell tail = _body.getLast();
tail.kill();
_body.remove(tail);
}
public int getLength()
{
return length;
}
}