-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.java
More file actions
277 lines (244 loc) · 9.02 KB
/
Snake.java
File metadata and controls
277 lines (244 loc) · 9.02 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import java.util.LinkedList;
import java.util.HashSet;
import java.util.Random;
public class Snake {
//List of constants to represent limiting
//Design factors for the Game such as
//The Key Codes and the board Bounds
private static final double BOARD_RAD = .015;
private static final int SIZE = 500;
private static final int UP_KEY = 38;
private static final int RIGHT_KEY = 39;
private static final int LEFT_KEY = 37;
private static final int DOWN_KEY = 40;
private static final int DELAY = 20;
private static final int SNAKE_RAD = 10;
private static final int BOUNDS = 40;
private void snake () {}
private static final Random RNG = new Random();
private static int gameCounter = 0;
private static final int RIGHT = 0;
private static final int UP = 2;
private static final int DOWN = 4;
private static final int LEFT = 1;
private static int direction = UP;
private static HashSet<Point> myBody = new HashSet<>();
static class Board {
private static final int RADIUS = SNAKE_RAD;
boolean[][] table;
Board (final int x, final int y) {
table = new boolean[x][y];
for (int i = 0; i < x; i++) {
table[i][RADIUS] = true;
}
for (int i = 0; i < x; i++) {
table[i][y - RADIUS] = true;
}
for (int i = 0; i < y; i++) {
table[RADIUS][i] = true;
}
for (int i = 0; i < y; i++) {
table[x - RADIUS][i] = true;
}
}
}
// static class obstacle {
// //To do//
// }
static class BodyPart {
private static final int SNAKE_RADUIS = 5;
Point center;
double radius = SNAKE_RADUIS;
BodyPart (final int x1, final int y1) {
center = new Point (x1, y1);
}
}
static class Point {
int x, y;
Point (final int a, final int b) {
x = a;
y = b;
}
public void moveLeft () {
x--;
}
public void moveRight () {
x++;
}
public void moveUp () {
y++;
}
public void moveDown () {
y--;
}
@Override
public String toString () {
return String.format("%d:%d", x, y);
}
@Override
public boolean equals (final Object obj) {
final Point other = (Point) obj;
return (x == other.x) && (y == other.y);
}
@Override
public int hashCode () {
return x + y;
}
}
static class SnakeBody {
private static final int RADUIS = SNAKE_RAD;
LinkedList<BodyPart> body;
Point tail;
Point head;
SnakeBody (final int size) {
body = new LinkedList<>();
body.addFirst((new BodyPart(size / 2, size / 2)));
body.addFirst(new BodyPart(size / 2, size / 2 + RADUIS));
tail = body.peekLast().center;
head = body.peekFirst().center;
}
//Removes the last bodyPart from the list
//And hides the part
public void cutTail (final Board board) {
board.table[body.peekLast().center.x][body.peekLast().center.y] = false;
myBody.remove(tail);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledCircle(body.peekLast().center.x,
body.peekLast().center.y, body.peekLast().radius + 1);
body.removeLast();
tail = body.peekLast().center;
}
//Increases the body of the snake in
//the direction it is going
public boolean increaseBody (final Board board) {
final BodyPart next;
final int increase = SNAKE_RAD;
if (direction == RIGHT) {
next = new BodyPart(head.x + increase, head.y);
} else if (direction == LEFT) {
next = new BodyPart(head.x - increase, head.y);
} else if (direction == UP) {
next = new BodyPart(head.x, head.y + increase);
} else {
next = new BodyPart(head.x, head.y - increase);
}
if (myBody.contains(next.center)) {
return true;
}
body.addFirst(next);
head = body.peekFirst().center;
board.table[head.x][head.y] = true;
myBody.add(head);
return false;
}
}
//Special Point that increases the length of the snake
static class Fruit {
private static final int COVER_UP_FRUIT = 3;
Point location;
Fruit (final int x, final int y) {
location = new Point(x, y);
}
public void draw (final Board board) {
StdDraw.setPenColor(StdDraw.MAGENTA);
if (!board.table[location.x][location.y]) {
StdDraw.filledSquare(location.x, location.y, 2);
}
}
public void erase (final Board board) {
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledSquare(location.x, location.y, COVER_UP_FRUIT);
}
}
//Initially Draws the board boards to accurately
//Represent the boarder
public static void drawBoard (final int size) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(BOARD_RAD);
StdDraw.setScale(0, size);
StdDraw.line(0, 0, 0, size);
StdDraw.line(0, 0, size, 0);
StdDraw.line(size, 0, size, size);
StdDraw.line(0, size, size, size);
}
//Draws the current instance of snake
public static int drawSnake (final SnakeBody snake) {
StdDraw.setPenColor(StdDraw.GREEN);
StdDraw.setScale(0, SIZE);
for (final BodyPart piece : snake.body) {
StdDraw.filledCircle(piece.center.x, piece.center.y, piece.radius);
}
return 0;
}
public static void main (final String[] args) {
//Initialize the game with a board size of 500 and creates
//Lists to track fruit and snake body
final int size = 500;
final Board board = new Board(size, size);
final SnakeBody snake = new SnakeBody(size);
final LinkedList<Fruit> fruit = new LinkedList<Fruit>();
drawBoard(size);
drawSnake(snake);
StdDraw.enableDoubleBuffering();
int isFruit = 0;
int fruitX = -1;
int fruitY = -1;
//Plays Game
while (true) {
//Listens for a specific key press and changes
//direction based off of that press
keyPressed();
//Calls SnakeBody cutTail Method
snake.cutTail(board);
//Calls SnakeBody increaseBody Method
try {
if (snake.increaseBody(board)) {
break;
}
} catch (final ArrayIndexOutOfBoundsException ex) {
return;
}
//Checks if there is a fruit in the list
//if so draw and check to see if the snake has eaten
if (!fruit.isEmpty()) {
if (((snake.head.x >= fruitX - SNAKE_RAD)
&& (snake.head.x <= fruitX + SNAKE_RAD))
&& ((snake.head.y >= fruitY - SNAKE_RAD)
&& (snake.head.y <= fruitY + SNAKE_RAD))) {
//if the snake eats the fruit increase its body
//erase the fruit from the board
//and remove the fruit from the list
snake.increaseBody(board);
fruit.peek().erase(board);
fruit.remove();
isFruit = 0;
} else if (gameCounter % SNAKE_RAD == 0 && isFruit == 0) {
fruit.peek().draw(board);
fruitX = fruit.peek().location.x;
fruitY = fruit.peek().location.y;
isFruit = 1;
}
} else {
fruit.add(new Fruit(RNG.nextInt(size - BOUNDS) + SNAKE_RAD,
RNG.nextInt(size - BOUNDS) + SNAKE_RAD));
}
drawSnake(snake);
StdDraw.show();
StdDraw.pause(DELAY);
gameCounter++;
}
}
public static void keyPressed () {
//Listens for a specific key press and changes
//direction based off of that press
if (StdDraw.isKeyPressed(DOWN_KEY) && direction != UP) {
direction = DOWN;
} else if (StdDraw.isKeyPressed(LEFT_KEY) && direction != RIGHT) {
direction = LEFT;
} else if (StdDraw.isKeyPressed(RIGHT_KEY) && direction != LEFT) {
direction = RIGHT;
} else if (StdDraw.isKeyPressed(UP_KEY) && direction != DOWN) {
direction = UP;
}
}
}