iiitl / snake-game-js

0 stars 44 forks source link

Improved Food Generation to Avoid Overlapping with Snake Body #74

Closed shuklamaneesh23 closed 4 months ago

shuklamaneesh23 commented 4 months ago

Solved issue #9 ,please review it.

Pull Request Description:

This pull request introduces a significant enhancement to the Snake game codebase by implementing a hash table (snakeHash) to efficiently track the snake's position on the game board. The changes aim to improve collision detection, optimize gameplay performance, and enhance overall code clarity and maintainability.

Key Changes:

  1. Introduction of Snake Hash Table:

    • The PR adds a new data structure, snakeHash, to efficiently track the positions occupied by the snake on the game board.
    • The hash table is initialized as a two-dimensional array with dimensions 19x19, matching the size of the game board.
    • Each element in the hash table represents a cell on the game board, with an initial value of false indicating that the cell is unoccupied by the snake.
  2. Optimized Collision Detection:

    • The isCollide() function has been updated to utilize the snakeHash table for faster and more accurate collision detection.
    • By checking the status of the corresponding cell in the snakeHash table, the function efficiently determines whether the snake has collided with itself or with the game board boundaries.
  3. Improved Gameplay Performance:

    • The introduction of the snakeHash table enhances gameplay performance by providing a faster and more streamlined approach to collision detection.
    • With the optimized collision detection logic, the game can handle larger snake sizes and higher speeds without sacrificing performance.