Design Snake game


  • 4
    N

    Design a Snake game on a device that has screen of width x height.

    Initially the snake is at position (0,0) with length = 1 unit.

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

    Each food appears one after another. For example, the second food will not appear until the first food was eaten by the snake. Same with others.

    class Snake {
        public Snake(int width, int height, List<int[]> food) {
    
        }
    
        /**
         * @param direction U = Up, L = Left, R = Right, D = Down
         * @return The game's score after the move. Return -1 if game over.
         */
        public int move(char direction) {
    
        }
    }
    

  • 0

    @nixed Thanks for posting! What happens when the snake go beyond the screen? For example when the snake is at (width-1,0) and move in the direction R, does it wrap around the screen and reappear at (0,0)?


  • 2
    N

    @1337c0d3r No, you are not allowed to go past the screen. This move results in game over as the snake collide with the screen.


  • 0

    @nixed How about the following?

    H = Head of snake
    T = Tail of snake

    T###H

    Is moving to the left a valid move? If so, will this result in game over?


  • 0
    N

    @1337c0d3r You may assume this is an invalid move and all moves given are valid.


  • 0

    @nixed When does appear the new list of food? Can we accept that each food list is new level in the game?
    Snake constructor is called with List of arrays of food positions. What does it mean that snake coudn't move to the left - left outside the screen, or snake head only move to the right,up and down?


  • 0
    N

    @elmirap The list contains all food's positions. Initially the food appears at position food.get(0), only one food may appear at the screen at once. After the snake eats the first food, the second food will appear at position food.get(1), and the same for the third food and so on...


  • 0
    N

    @1337c0d3r Actually to make things simple, you may assume that if the snake is currently T###H moving to the left is a valid move and results in game over.

    Each move controls the snake head's direction. So if moving the snake head in that direction causes the head to touch the snake's body, it results in game over.


  • 0

    @nixed , yes, i see but they use two dimensional array for the food, so I assume that food is situated on different cells on the grid. Is it right?
    In your example food.get(0) returns two dimensional array ,probably this is x,y coordinates of the food or I don't get something


  • 0
    N

    @elmirap Oops, my bad. It should be List<int[2]>. Each food is represent by an array of size 2, ary[0] = row, ary[1] = col.


  • 0

    @nixed thanks. About describtion T###H.
    I think that the following result in game over:

    1. move down
      /###H
      /###T
    2. move up
      /###T
      /###H
    3. move right
      / ####
      / ##HT
    4. move left
      /####
      /TH##
    5. cross with screen borders

  • 0

    The food position is given at first? What if the position of the food to appear is occupied by the snake body?
    I think it would be better that food is generated randomly after the snake eating one.


  • 0
    N

    @elmirap said in Design Snake game:

    move down
    ###H
    ###T

    No this is a valid move and does not result in game over. This move results in the following:

    ####
    ##TH
    

  • 0
    N

    @xidui Yes the food is given in the constructor. If the position of the food is occupied by the snake body then according to the rules, the snake's length and the game score immediately increase by one.

    In real game you would generate randomly the next food's position, but I think you want a deterministic score after each move so it is easier to test.


  • 0

    @nixed after some correction is it right this?

    valid move down
    /###H
    /###T
    becomes
    /####
    /##TH
    valid move up
    /###T
    /###H
    becomes
    /##TH
    /####
    valid move right
    / ####
    / ##HT
    becomes
    / ###T
    / ###H
    valid move left
    /####
    /TH##
    becomes
    / T###
    / H###
    invalid cross with screen borders


  • 0
    N

    @elmirap Yes that's right.


  • 0

    @nixed and the snake bites its tail when there is no space for the tail to move


  • 0

    @elmirap It is not possible for the snake to bite its tail. The only exception is right after the snake eats a food, the next food appears at one of the block already occupied by the snake.


  • 0

    @1337c0d3r what about 2x2 game ,following game use case :

    1. /H,T #
      /# F
    2. /# T
      /# H
    3. /# T
      /F H
    4. /# T
      /H #
    5. /F T
      /H #
    6. /H T
      /# #
    7. All game map is covered by snake body, game end? With your words next food will always appears in blocks occupied by sname (can't eat anymore) and the game is over.
      I see now, snake always is moving , thank you very much @1337c0d3r,@nixed

  • 0

    @elmirap Not necessarily. The next food may not appear as the given list of food is finite. :)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.