Design a Snake game that is played on a device with screen size =width x height.Play the game onlineif you are not familiar with the game.

The snake is initially positioned at the top left corner (0,0) with length = 1 unit.

You are given a list of food's positions in row-column order. When a snake eats the food, its length and the game's score both increase by 1.

Each food appears one by one on the screen. For example, the second food will not appear until the first food was eaten by the snake.

When a food does appear on the screen, it is guaranteed that it will not appear on a block occupied by the snake.

import java.util.ArrayList;
import java.util.List;

class SnakeGame {
    private char[][] board;
    private int[][] food;
    private int foodIndex;
    private int len;
    private int[] tail;
    private int[] head;
    private List<int[]> turningPoints;
    private int score;

    /** Initialize your data structure here.
     @param width - screen width
     @param height - screen height
     @param food - A list of food positions
     E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
    public SnakeGame(int width, int height, int[][] food) {
        this.board = new char[height][width];

        this.food = food;

        board[0][0] = 's';

        if(food.length > 0) {

        }
        foodIndex = 0;
        if(foodIndex < food.length && food[foodIndex][0] < height && food[foodIndex][1] < width) {

            board[food[foodIndex][0]][food[foodIndex][1]] = 'f';
            foodIndex++;
        }

        len = 1;

        head = new int[]{0, 0};
        tail = new int[]{0, 0};
        turningPoints = new ArrayList<>();

        score = 0;
    }

    /** Moves the snake.
     @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
     @return The game's score after the move. Return -1 if game over.
     Game over when snake crosses the screen boundary or bites its body. */
    public int move(String direction) {
        int m = board.length;
        int n = board[0].length;
        int x1 = head[0];
        int y1 = head[1];
        int x2 = head[0];
        int y2 = head[1];

        int dir = 0;

        if(direction.equals("U")) {
            x2--;
            dir = 0;
        } else if(direction.equals("L")){
            y2--;
            dir = 1;
        } else if(direction.equals("R")) {
            y2++;
            dir = 2;
        } else {
            x2++;
            dir = 3;
        }

        if(x2 < 0 || x2 >= m || y2 < 0 || y2 >= n)
            return -1;


        turningPoints.add(new int[]{x1, y1, dir});

        if(board[x2][y2] != 'f') {
            head = new int[]{x2, y2};

            board[tail[0]][tail[1]] = ' ';
            if(board[x2][y2] == 's')
                return -1;
            board[x2][y2] = 's';

            int[] p = turningPoints.get(0);

            if(tail[0] == p[0] && tail[1] == p[1])
                turningPoints.remove(0);

            if(p[2] == 0) {
                tail[0]--;
            } else if(p[2] == 1) {
                tail[1]--;
            } else if(p[2] == 2) {
                tail[1]++;
            } else {
                tail[0]++;
            }

        } else {
            len++;
            head = new int[]{x2, y2};
            if(board[x2][y2] == 's')
                return -1;
            board[x2][y2] = 's';
            if(foodIndex < food.length && food[foodIndex][0] < m && food[foodIndex][1] < n) {
                board[food[foodIndex][0]][food[foodIndex][1]] = 'f';
                foodIndex++;
            }
            score++;
        }

        return score;
    }

    public void print() {
        for(int i = 0; i < board.length; i++) {
            System.out.println();
            for(int j = 0; j < board[0].length; j++) {
                System.out.print(board[i][j]);
            }
        }
        System.out.println();
        System.out.println("-------------------------------------------------------");
    }

}

/**
 * Your SnakeGame object will be instantiated and called as such:
 * SnakeGame obj = new SnakeGame(width, height, food);
 * int param_1 = obj.move(direction);
 */

results matching ""

    No results matching ""