Untitled

mail@pastecode.io avatar
unknown
plain_text
7 days ago
1.9 kB
1
Indexable
Never
//Name: Nhi Tran
//UCID:


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]; //abhjakbdhjkbhrjjjj abhjakbdhjkbhrjjjj abhjakbdhjkbhrjjjj abhjakbdhjkbhrjjjj
            SnekGame.snek[i][1] = SnekGame.snek[i-1][1]; //abhjakbdhjkbhrjjjj
        }


        SnekGame.snek[0][0] = newX;
        SnekGame.snek[0][1] = newY;

        if (newX == SnekGame.foodX && newY == SnekGame.foodY) {
            SnekGame.snekLength++;
            SnekGame.generateFoodIfNeeded();
        }


        SnekGame.clearGrid();
        //go through array snek to update array grid to print out later
        for (int i = 0; i < SnekGame.snekLength; i++) { //abhja kbdhj kbhr jj jj
            int x = SnekGame.snek[i][0];//abhj akbdh jk bhrj jj j
            int y = SnekGame.snek[i][1];
            SnekGame.grid[x][y] = (i == 0) ? 'S' : 's';
        }
        //update location of food in array grid
        SnekGame.grid[SnekGame.foodX][SnekGame.foodY] = '@';
        //go through the array of obstacle in order to update the array grid to print out later because the clear grid function already clear out existing grid array
        for (int i = 0; i < SnekGame.obstacles.length; i++) {
            int x = SnekGame.obstacles[i][0];
            int y = SnekGame.obstacles[i][1];
            SnekGame.grid[x][y] = '#';
        }
    }

    public static boolean isValidMove(int x, int y) { //ab hja kbd hjkbh  rjjjj //abhj  akbd hjk bhr jjj j
        if (x < 0 || x >= SnekGame.gridSize || y < 0 || y >= SnekGame.gridSize) {
            return false;
        }

        for (int i = 0; i < SnekGame.snek.length; i++) {
            if (x == SnekGame.snek[i][0] && y == SnekGame.snek[i][1]) {
                return false;
            }
        }

        return true;
    }
}
Leave a Comment