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) {
xythis.x = new int[]{x,; this.y = y};
}
 
final int[] xyx, y;
};
 
static final Random rand = new Random();
Line 163:
boolean hitsWall() {
Point head = snake.get(0);
int nextCol = head.x + dir.xy[0]x;
int nextRow = head.y + dir.xy[1]y;
return grid[nextRow][nextCol] == WALL;
}
Line 170:
boolean hitsSnake() {
Point head = snake.get(0);
int nextCol = head.x + dir.xy[0]x;
int nextRow = head.y + dir.xy[1]y;
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.xy[0]x;
int nextRow = head.y + dir.xy[1]y;
for (Point p : treats)
if (p.x == nextCol && p.y == nextRow) {
Line 202:
}
Point head = snake.get(0);
head.x += dir.xy[0]x;
head.y += dir.xy[1]y;
}
 
void growSnake() {
Point tail = snake.get(snake.size() - 1);
int x = tail.x + dir.xy[0]x;
int y = tail.y + dir.xy[1]y;
snake.add(new Point(x, y));
}
Anonymous user