Anonymous user
Snake/Java: Difference between revisions
m
let's just make x and y fields rather than an xy array
m (→Code: makes sure there's at least one treat at startup) |
m (let's just make x and y fields rather than an xy array) |
||
Line 13:
Dir(int x, int y) {
}
final int
}
static final Random rand = new Random();
Line 163:
boolean hitsWall() {
Point head = snake.get(0);
int nextCol = head.x + dir.
int nextRow = head.y + dir.
return grid[nextRow][nextCol] == WALL;
}
Line 170:
boolean hitsSnake() {
Point head = snake.get(0);
int nextCol = head.x + dir.
int nextRow = head.y + dir.
for (Point p : snake)
if (p.x == nextCol && p.y == nextRow)
Line 180:
boolean eatsTreat() {
Point head = snake.get(0);
int nextCol = head.x + dir.
int nextRow = head.y + dir.
for (Point p : treats)
if (p.x == nextCol && p.y == nextRow) {
Line 202:
}
Point head = snake.get(0);
head.x += dir.
head.y += dir.
}
void growSnake() {
Point tail = snake.get(snake.size() - 1);
int x = tail.x + dir.
int y = tail.y + dir.
snake.add(new Point(x, y));
}
|