Untitled
unknown
plain_text
a year ago
1.3 kB
8
Indexable
public class SnekUtils {
public static void updateSnekPosition(int newX, int newY) {
for (int i = SnekGame.snekLength - 1; i > 0; i--) {
SnekGame.snek[i][0] = SnekGame.snek[i-1][0];
SnekGame.snek[i][1] = SnekGame.snek[i-1][1];
}
SnekGame.snek[0][0] = newX;
SnekGame.snek[0][1] = newY;
if (newX == SnekGame.foodX && newY == SnekGame.foodY) {
SnekGame.snekLength++;
SnekGame.generateFoodIfNeeded();
}
SnekGame.clearGrid();
for (int i = 0; i < SnekGame.snekLength; i++) {
int x = SnekGame.snek[i][0];
int y = SnekGame.snek[i][1];
SnekGame.grid[x][y] = (i == 0) ? 'S' : 's';
}
SnekGame.grid[SnekGame.foodX][SnekGame.foodY] = '@';
}
// private static int[] getNextBodyPosition(int[] snekHead, int[][] grid) {
// return snekHead;
// }
public static boolean isValidMove(int x, int y) {
if (x < 0 || x >= 10 || y < 0 || y >= 10) {
return false;
}
for (int i = 0; i < SnekGame.snekLength; i++) {
if (x == SnekGame.snek[i][0] && y == SnekGame.snek[i][1]) {
return false;
}
}
return true;
}
}
Editor is loading...
Leave a Comment