Home » leet code » LeetCode 353. Snake Game – Medium

LeetCode 353. Snake Game – Medium

Introduction

LeetCode 353. Snake Game :Are you ready to dive into the world of game design and programming? In this blog post, we’ll be exploring a fascinating journey of designing a classic snake game while tackling LeetCode 353. Get ready to embark on this adventure with us!


Understanding LeetCode 353 – Design Snake Game

Before we plunge into the deep waters of designing our snake game, let’s understand what LeetCode 353 is all about. It’s a problem that challenges your design skills by asking you to create a snake game. The task is to make the snake move, grow, and interact with food while avoiding collisions with itself or the wall. Sounds exciting, right?


The Snake Game in a Nutshell

Imagine you’re a snake in a confined space, and your mission is to eat as much food as possible without running into the walls or yourself. As you consume food, you grow longer and the game becomes progressively challenging. It’s a simple concept but can be quite addictive. Let’s break down the components of the snake game:

ComponentDescription
SnakeThe main character of the game – a moving snake.
FoodThe target for the snake to eat and grow.
WallsThe boundaries of the game space.
CollisionWhat happens when the snake meets itself or a wall.
ScoringHow you keep track of the player’s performance.

Designing the Snake Game

Now that we have a good grasp of what the snake game entails, let’s dive into designing it. We’ll break this down into smaller sections, making it easier to understand and implement.

1. Initializing the Game

The first step is to set up the game environment. This includes creating the game board, initializing the snake, and placing the first food item.

2. Moving the Snake

We’ll need to allow the player to control the snake’s movement. This typically involves using arrow keys to change the snake’s direction.

3. Eating Food

When the snake’s head collides with a food item, we need to make it grow and generate a new food item at a random location.

4. Handling Collisions

We should handle collisions with the walls and the snake’s own body. When a collision occurs, the game ends.

5. Scoring System

To keep track of the player’s performance, we can implement a scoring system that increases as the snake eats food.

6. Game Over

Lastly, we need to determine when the game is over, whether due to collisions or the player’s choice to quit.


The Coding Part

Now, let’s get our hands dirty and start coding. We’ll be using Python to design our snake game. Python is a versatile language, and it’s a great choice for implementing this game.

Here’s a simplified example of how you can initialize the game board and the snake using Python:

# Initialize the game board
game_board = [[0] * width for _ in range(height)]

# Initialize the snake
snake = [(0, 0)]

In this code, we create a game board as a two-dimensional array and initialize the snake with its starting position.

I am Building guessing game C# make a “youre close” answer


Moving the Snake – Step by Step

Moving the snake is a crucial part of the game. It’s what makes it interactive and fun. Let’s break it down into steps:

Step 1: Handling User Input

We need to listen for user input, typically arrow keys, and change the direction of the snake accordingly.

Step 2: Updating the Snake’s Position

Based on the input from the user, we need to update the position of the snake’s head and, subsequently, the rest of its body.

Step 3: Checking for Collisions

After each move, we must check if the snake collides with the walls, itself, or food.

Step 4: Growing the Snake

If the snake eats food, it should grow longer. This means adding a new segment to the snake’s body.

Step 5: Game Over Condition

We should determine when the game is over. This can happen if the snake collides with a wall or itself.


Coding the Snake’s Movement

Here’s a code snippet illustrating how to move the snake in Python:

# Step 1: Handling User Input
def handle_input():
    # Get user input (e.g., arrow key)
    # Update the snake's direction based on input

# Step 2: Updating the Snake's Position
def move_snake():
    # Update the position of the snake's head
    # Move the rest of the body segments

# Step 3: Checking for Collisions
def check_collisions():
    # Check if the snake hits the walls or itself
    # Check if the snake eats food

# Step 4: Growing the Snake
def grow_snake():
    # Add a new segment to the snake's body

# Step 5: Game Over Condition
def is_game_over():
    # Check if the game is over (e.g., collision with wall or itself)

This code structure outlines how you can implement the snake’s movement and interaction with the game environment.


Scoring and Game Over

Now, let’s delve into scoring and determining when the game is over.

Scoring System

A simple scoring system can be implemented to keep track of the player’s performance. You can increase the score each time the snake eats a piece of food.

score = 0

# Inside the code for eating food
score += 1

Game Over

To determine when the game is over, we can use the is_game_over function we defined earlier. If it returns True, the game ends.

if is_game_over():
    print("Game Over! Your score: " + str(score))
    # End the game or ask if the player wants to play again

Here is the SnakeGame code without comments in C++, Java, Python, and JavaScript:

C++: Snake Game

#include <iostream>
#include <vector>
#include <deque>
#include <unordered_set>

class SnakeGame {
public:
    SnakeGame(int width, int height, std::vector<std::vector<int>>& food) {
        this->width = width;
        this->height = height;
        this->food = food;
        set.insert(0);
        deque.push_back(0);
        score = 0;
        foodIndex = 0;
    }

    int move(std::string direction) {
        int rowHead = deque.front() / width;
        int colHead = deque.front() % width;

        int newRowHead, newColHead;
        if (direction == "U") {
            newRowHead = rowHead - 1;
            newColHead = colHead;
        } else if (direction == "D") {
            newRowHead = rowHead + 1;
            newColHead = colHead;
        } else if (direction == "L") {
            newRowHead = rowHead;
            newColHead = colHead - 1;
        } else {
            newRowHead = rowHead;
            newColHead = colHead + 1;
        }

        int newHead = newRowHead * width + newColHead;

        set.erase(deque.back());

        if (newRowHead < 0 || newRowHead == height || newColHead < 0 || newColHead == width || set.count(newHead)) {
            return -1;
        }

        set.insert(newHead);
        deque.push_front(newHead);

        if (foodIndex < food.size() && newRowHead == food[foodIndex][0] && newColHead == food[foodIndex][1]) {
            foodIndex++;
            score++;
            set.insert(deque.back());
            return score;
        }

        deque.pop_back();
        return score;
    }

private:
    std::unordered_set<int> set;
    std::deque<int> deque;
    int score;
    int foodIndex;
    int width;
    int height;
    std::vector<std::vector<int>> food;
};

Java: Leetcode 353

import java.util.*;

class SnakeGame {
    HashSet<Integer> set;
    Deque<Integer> deque;
    int score;
    int foodIndex;
    int width;
    int height;
    int[][] food;

    public SnakeGame(int width, int height, int[][] food) {
        this.width = width;
        this.height = height;
        this.food = food;
        set = new HashSet<>();
        deque = new LinkedList<>();
        score = 0;
        foodIndex = 0;
        set.add(0);
        deque.offerLast(0);
    }

    public int move(String direction) {
        int rowHead = deque.peekFirst() / width;
        int colHead = deque.peekFirst() % width;

        int newRowHead, newColHead;
        switch (direction) {
            case "U":
                newRowHead = rowHead - 1;
                newColHead = colHead;
                break;
            case "D":
                newRowHead = rowHead + 1;
                newColHead = colHead;
                break;
            case "L":
                newRowHead = rowHead;
                newColHead = colHead - 1;
                break;
            default:
                newRowHead = rowHead;
                newColHead = colHead + 1;
        }

        int newHead = newRowHead * width + newColHead;
        set.remove(deque.peekLast());

        if (newRowHead < 0 || newRowHead == height || newColHead < 0 || newColHead == width || set.contains(newHead)) {
            return -1;
        }

        set.add(newHead);
        deque.offerFirst(newHead);

        if (foodIndex < food.length && newRowHead == food[foodIndex][0] && newColHead == food[foodIndex][1]) {
            foodIndex++;
            score++;
            set.add(deque.peekLast());
            return score;
        }

        deque.pollLast();
        return score;
    }
}

Python:Snake Game

class SnakeGame:

    def __init__(self, width: int, height: int, food: List[List[int]]):
        self.width = width
        self.height = height
        self.food = food
        self.set = set()
        self.deque = collections.deque()
        self.score = 0
        self.foodIndex = 0
        self.set.add(0)
        self.deque.append(0)

    def move(self, direction: str) -> int:
        rowHead = self.deque[0] // self.width
        colHead = self.deque[0] % self.width

        if direction == "U":
            rowHead -= 1
        elif direction == "D":
            rowHead += 1
        elif direction == "L":
            colHead -= 1
        else:
            colHead += 1

        newHead = rowHead * self.width + colHead

        self.set.remove(self.deque[-1])

        if (rowHead < 0 or rowHead == self.height or colHead < 0 or colHead == self.width or newHead in self.set):
            return -1

        self.set.add(newHead)
        self.deque.appendleft(newHead)

        if self.foodIndex < len(self.food) and rowHead == self.food[self.foodIndex][0] and colHead == self.food[self.foodIndex][1]:
            self.foodIndex += 1
            self.score += 1
            self.set.add(self.deque[-1])
            return self.score

        self.deque.pop()
        return self.score

JavaScript: Snake Game Leetcode 353.

class SnakeGame {
    constructor(width, height, food) {
        this.width = width;
        this.height = height;
        this.food = food;
        this.set = new Set();
        this.deque = [];
        this.score = 0;
        this.foodIndex = 0;
        this.set.add(0);
        this.deque.push(0);
    }

    move(direction) {
        let rowHead = Math.floor(this.deque[0] / this.width);
        let colHead = this.deque[0] % this.width;

        let newRowHead, newColHead;
        switch (direction) {
            case "U":
                newRowHead = rowHead - 1;
                newColHead = colHead;
                break;
            case "D":
                newRowHead = rowHead + 1;
                newColHead = colHead;
                break;
            case "L":
                newRowHead = rowHead;
                newColHead = colHead - 1;
                break;
            default:
                newRowHead = rowHead;
                newColHead = colHead + 1;
        }

        let newHead = newRowHead * this.width + newColHead;

        this.set.delete(this.deque[this.deque.length - 1]);

        if (newRowHead < 0 || newRowHead === this.height || newColHead < 0 || newColHead === this.width || this.set.has(newHead)) {
            return -1;
        }

        this.set.add(newHead);
        this.deque.unshift(newHead

);

        if (this.foodIndex < this.food.length && newRowHead === this.food[this.foodIndex][0] && newColHead === this.food[this.foodIndex][1]) {
            this.foodIndex++;
            this.score++;
            this.set.add(this.deque[this.deque.length - 1]);
            return this.score;
        }

        this.deque.pop();
        return this.score;
    }
}

Conclusion

In this journey of designing a snake game, we’ve covered the basics of how to create a simple snake game, inspired by LeetCode 353. We’ve discussed the game’s components, the coding steps involved, and even touched on scoring and game over conditions. This is just the tip of the iceberg; you can expand and enhance the game with more features, levels, and challenges.

So, are you up for the challenge? Design your snake game, explore the world of game development, and have fun while doing it. The possibilities are endless, and the only limit is your creativity. Happy coding!

List of Some important Leet code Questions:

Leave a Reply