Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
1.3 kB
2
Indexable
Never
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;
    }
}
Leave a Comment