Snake/Java: Difference between revisions

→‎{{header|Java}}: cleaned up code a bit
m (→‎Code: small changes)
(→‎{{header|Java}}: cleaned up code a bit)
Line 9:
 
public class Snake extends JPanel implements Runnable {
enum Dir {
up, right, down, left
};
 
final Random rand = new Random();
final int[][] dirs = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
final int WALL = -1;
final int MAX_ENERGY = 1500;
 
volatile boolean gameOver = true;
Line 19 ⟶ 24:
int nRows = 44;
int nCols = 64;
intDir dir;
int energy;
 
Line 53 ⟶ 58:
 
case KeyEvent.VK_UP:
if (dir != 2Dir.down)
dir = 0Dir.up;
break;
 
case KeyEvent.VK_LEFT:
if (dir != 1Dir.right)
dir = 3Dir.left;
break;
 
case KeyEvent.VK_RIGHT:
if (dir != 3Dir.left)
dir = 1Dir.right;
break;
 
case KeyEvent.VK_DOWN:
if (dir != 0Dir.up)
dir = 2Dir.down;
break;
}
Line 82 ⟶ 87:
stop();
initGrid();
treats = new LinkedList<>();
 
dir = 3Dir.left;
energy = 1500MAX_ENERGY;
 
if (score > hiScore)
Line 90 ⟶ 96:
score = 0;
 
treats = new LinkedList<>();
snake = new ArrayList<>();
for (int x = 0; x < 7; x++)
Line 127 ⟶ 132:
}
 
energyif -=(energyUsed() 10;|| hitsWall() || hitsSnake()) {
if (energy <= 0)
gameOver();
} else {
 
else if (hitsWalleatsTreat()) {
gameOver() score++;
energy = MAX_ENERGY;
 
else if growSnake(hitsSnake());
gameOver();}
 
else if (eatsTreat()) {
score++;
energy = 1500;
growSnake();
}
 
if (!gameOver) {
moveSnake();
addTreat();
Line 149 ⟶ 145:
repaint();
}
}
 
boolean energyUsed() {
energy -= 10;
return energy <= 0;
}
 
boolean hitsWall() {
Point head = snake.get(0);
int nextCol = head.x + dirs[dir.ordinal()][0];
int nextRow = head.y + dirs[dir.ordinal()][1];
return grid[nextRow][nextCol] == WALL;
}
 
boolean hitsSnake() {
Point head = snake.get(0);
int nextCol = head.x + dirs[dir.ordinal()][0];
int nextRow = head.y + dirs[dir.ordinal()][1];
for (Point p : snake)
if (p.x == nextCol && p.y == nextRow)
return true;
return false;
}
 
boolean eatsTreat() {
Point head = snake.get(0);
int nextCol = head.x + dirs[dir.ordinal()][0];
int nextRow = head.y + dirs[dir.ordinal()][1];
for (Point p : treats)
if (p.x == nextCol && p.y == nextRow) {
return treats.remove(p);
}
return false;
}
 
Line 154 ⟶ 183:
gameOver = true;
stop();
}
 
void moveSnake() {
for (int i = snake.size() - 1; i > 0; i--) {
Point p1 = snake.get(i - 1);
Point p2 = snake.get(i);
p2.x = p1.x;
p2.y = p1.y;
}
Point head = snake.get(0);
head.x += dirs[dir.ordinal()][0];
head.y += dirs[dir.ordinal()][1];
}
 
void growSnake() {
Point tail = snake.get(snake.size() - 1);
int x = tail.x + dirs[dir.ordinal()][0];
int y = tail.y + dirs[dir.ordinal()][1];
snake.add(new Point(x, y));
}
 
void addTreat() {
if (treats.size() < 3) {
 
if (rand.nextInt(10) == 0) { // 1 in 10
 
if (rand.nextInt(4) != 0) { // 3 in 4
int x, y;
while (true) {
 
x = rand.nextInt(nCols);
y = rand.nextInt(nRows);
if (grid[y][x] != 0)
continue;
 
Point p = new Point(x, y);
if (snake.contains(p) || treats.contains(p))
continue;
 
treats.add(p);
break;
}
} else if (treats.size() > 1)
treats.remove(0);
}
}
}
 
Line 216 ⟶ 291:
drawScore(g);
}
}
 
void moveSnake() {
for (int i = snake.size() - 1; i > 0; i--) {
Point p1 = snake.get(i - 1);
Point p2 = snake.get(i);
p2.x = p1.x;
p2.y = p1.y;
}
Point head = snake.get(0);
head.x += dirs[dir][0];
head.y += dirs[dir][1];
}
 
void growSnake() {
Point tail = snake.get(snake.size() - 1);
int x = tail.x + dirs[dir][0];
int y = tail.y + dirs[dir][1];
snake.add(new Point(x, y));
}
 
void addTreat() {
if (treats.size() < 3) {
 
if (rand.nextInt(10) == 0) { // 1 in 10
 
if (rand.nextInt(4) != 0) { // 3 in 4
int x, y;
while (true) {
 
x = rand.nextInt(nCols);
y = rand.nextInt(nRows);
if (grid[y][x] != 0)
continue;
 
Point p = new Point(x, y);
if (snake.contains(p) || treats.contains(p))
continue;
 
treats.add(p);
break;
}
} else if (treats.size() > 1)
treats.remove(0);
}
}
}
 
boolean hitsWall() {
Point head = snake.get(0);
int nextCol = head.x + dirs[dir][0];
int nextRow = head.y + dirs[dir][1];
return grid[nextRow][nextCol] == WALL;
}
 
boolean hitsSnake() {
Point head = snake.get(0);
int nextCol = head.x + dirs[dir][0];
int nextRow = head.y + dirs[dir][1];
for (Point p : snake)
if (p.x == nextCol && p.y == nextRow)
return true;
return false;
}
 
boolean eatsTreat() {
Point head = snake.get(0);
int nextCol = head.x + dirs[dir][0];
int nextRow = head.y + dirs[dir][1];
for (Point p : treats)
if (p.x == nextCol && p.y == nextRow) {
treats.remove(p);
return true;
}
return false;
}
 
Anonymous user