Design Snake Game
Let us see how to design a basic Snake Game that provides the following functionalities:
- Snake can move in a given direction and when it eats the food, the length of snake increases.
- When the snake crosses itself, the game will be over.
- Food will be generated at a given interval.
Asked In: Amazon, Microsoft, and many more interviews.
This question is asked in interviews to Judge the Object-Oriented Design skill of a candidate. So, first of all, we should think about the classes.
The main classes will be:
The class Game represents the body of our program. It stores information about the snake and the board. The Cell class represents the one point of display/board. It contains the row no, column no and the information about it i.e. it is empty or there is food on it or is it a part of snake body?
Now, the Snake class, contains the body and head. We have used Linked List to store the body because we can add a cell in O(1).
Grow method will be called when it eats the food. Other methods are self-explanatory.
The Board class represents the display. It is a matrix of Cells. It has a method generate Food which generates the
the food at a random position.
The main class (Game) which keeps the instance of Snake and Board. It’s method “update” needs to be called at a fixed interval (with the help of user input).
- The code in this class represents a snake game.
- The Snake object stores the information about the snake and the Board object stores the information about the board.
- The direction variable keeps track of which direction the player is moving in (left, right, up, or down).
- The isGameOver() method checks to see if there is a game over condition.
- If there is a game over condition, then setGameOver() sets the gameOver flag to true so that it will stop playing when there is a game over.
- The getDirection() method returns an integer value that indicates which direction the player is currently moving in (0 for left, 1 for right, 2 for up, and -2 for down).
- The code is responsible for managing the game play of the Snake Game.
- The class has a number of properties and methods that are relevant to game play.
- The first property is the snake object which references the actual Snake Game character.
- The snake object has a number of properties including direction, board and gameOver.
- The next property is the Board object which references the playing surface on which the Snake Game takes place.
- The Board object also has a direction property which indicates where in space the player is located relative to the playing surface.
- The last two properties are used to keep track of whether or not the game is currently over.
- gameOver will be set to true if the player loses, whileisGameOver will be set to false if
- The code starts by printing out “Going to update the game.”
- This is a message that will be displayed every time the code executes.
- Next, the code checks to see if gameOver has been set to true.
- If it hasn’t, then the code sets direction to DIRECTION_NONE and sets gameOver to true.
- The next part of the code determines which cell in the snake’s head is being used as a reference point.
- The getNextCell() method uses row and col variables to determine this information.
- Then, it returns the Cell object for use in other parts of the program.
- The next section of code updates various aspects of the game based on user input from keyboard keys.
- First, it checks whether any key presses have been made.
- If so, it uses those key presses as inputs for moving or growing cells in the snake’s body.
- Finally, it updates various elements onscreen based on what was done with those cells (e.g., generating food).
- The code updates the game at regular intervals and accepts user input from the Keyboard.
- If the user inputs a direction other than DIRECTION_NONE, then the code sets the direction to that chosen input.
- If the user inputs a cell that is not on the snake’s path, then the code moves the snake to that cell and checks if it crashes into anything along its way.
- If it does, then set Direction to DIRECTION_NONE.
- Otherwise, if the cell is a food item, then the code will grow the snake and call generateFood() on board .
- The code starts by creating a new instance of the Snake class.
- This object will represent the player’s snake in the game.
- The initPos variable stores the location of this snake at any given time.
- Next, a new Board object is created and initialized with the size of the playing area (10×10).
- This board will be used to track where the snake has moved and what obstacles it has encountered along its way.
- A new Game object is then created, which contains information about the game itself as well as our snake instance.
- The gameOver property is set to false so that we can keep track of whether or not there is currently a game being played.
- The direction property is set to DIRECTION_RIGHT so that users can control their snake’s movement using their keyboard input.
- The code then periodically updates both the Game object and Board objects based on user input received from the Keyboard class.
- Whenever a keystroke is detected, an event handler for that particular key is called.
- In this case, we simply print out “Press left arrow to move left” onscreen whenever Left Arrow keystrokes are detected by our code.
- The code creates a new instance of the Snake game, initializes it to the given position (0, 0), and creates a new Board object.
- The code then creates a new Game object and sets its properties to match those of the Snake object.
- Next, the code declares two variables: initSnake and board.
- initSnake is an instance of the Snake class, while board is an instance of the Board class.
- The next line of code sets up a timer that will update the game at regular intervals.
- This will allow us to react to user input from the keyboard.
- Finally, the code declares two variables: gameOver and direction.
- gameOver is set to false so that the game can continue even if it reaches its end condition.
Please Login to comment...