Snake/Java: Difference between revisions

Content added Content deleted
m (→‎Code: small changes)
(→‎{{header|Java}}: cleaned up code a bit)
Line 9: Line 9:


public class Snake extends JPanel implements Runnable {
public class Snake extends JPanel implements Runnable {
enum Dir {
up, right, down, left
};

final Random rand = new Random();
final Random rand = new Random();
final int[][] dirs = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
final int[][] dirs = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
final int WALL = -1;
final int WALL = -1;
final int MAX_ENERGY = 1500;


volatile boolean gameOver = true;
volatile boolean gameOver = true;
Line 19: Line 24:
int nRows = 44;
int nRows = 44;
int nCols = 64;
int nCols = 64;
int dir;
Dir dir;
int energy;
int energy;


Line 53: Line 58:


case KeyEvent.VK_UP:
case KeyEvent.VK_UP:
if (dir != 2)
if (dir != Dir.down)
dir = 0;
dir = Dir.up;
break;
break;


case KeyEvent.VK_LEFT:
case KeyEvent.VK_LEFT:
if (dir != 1)
if (dir != Dir.right)
dir = 3;
dir = Dir.left;
break;
break;


case KeyEvent.VK_RIGHT:
case KeyEvent.VK_RIGHT:
if (dir != 3)
if (dir != Dir.left)
dir = 1;
dir = Dir.right;
break;
break;


case KeyEvent.VK_DOWN:
case KeyEvent.VK_DOWN:
if (dir != 0)
if (dir != Dir.up)
dir = 2;
dir = Dir.down;
break;
break;
}
}
Line 82: Line 87:
stop();
stop();
initGrid();
initGrid();
treats = new LinkedList<>();


dir = 3;
dir = Dir.left;
energy = 1500;
energy = MAX_ENERGY;


if (score > hiScore)
if (score > hiScore)
Line 90: Line 96:
score = 0;
score = 0;


treats = new LinkedList<>();
snake = new ArrayList<>();
snake = new ArrayList<>();
for (int x = 0; x < 7; x++)
for (int x = 0; x < 7; x++)
Line 127: Line 132:
}
}


energy -= 10;
if (energyUsed() || hitsWall() || hitsSnake()) {
if (energy <= 0)
gameOver();
gameOver();
} else {

else if (hitsWall())
if (eatsTreat()) {
gameOver();
score++;
energy = MAX_ENERGY;

else if (hitsSnake())
growSnake();
gameOver();
}

else if (eatsTreat()) {
score++;
energy = 1500;
growSnake();
}

if (!gameOver) {
moveSnake();
moveSnake();
addTreat();
addTreat();
Line 149: Line 145:
repaint();
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: Line 183:
gameOver = true;
gameOver = true;
stop();
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: Line 291:
drawScore(g);
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;
}
}