Skip to content

Raquezin/Snake_algorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake Game

Warning

This represents a concept that I am actively developing.

A classic Snake game implementation in C with SDL3, featuring both human-playable and AI-controlled modes.

Features

  • Person Mode: Play the game yourself using keyboard controls
  • Machine Mode: Watch the AI play using a longest-path algorithm
  • SDL3-based graphical interface
  • Customizable grid size (30x30 by default)
  • Circular queue-based snake body management

Project Structure

snake/
├── include/
│   ├── circular_queue.h     # Circular queue header (types and function declarations)
│   ├── circular_queue.c     # Circular queue implementation for snake body
│   └── const.h              # Game constants (grid size, window dimensions, etc.)
├── src/
│   ├── person/
│   │   ├── snake.c          # Console-based game logic (legacy)
│   │   └── snake_ui.c       # SDL3 UI implementation for person mode
│   └── machine/
│       ├── snake.c          # Game logic with AI pathfinding (legacy)
│       └── snake_ui.c       # SDL3 UI implementation for machine mode
├── CMakeLists.txt           # Build configuration
└── algorithm.md             # Documentation of the longest-path algorithm

Requirements

  • CMake 3.16 or higher
  • SDL3 library
  • C11 compiler (GCC, Clang, etc.)
  • Math library (libm, typically included with standard C library)

Building

CMake Build (Recommended)

mkdir build
cd build
cmake ..
cmake --build .

The executables will be built in build/bin/:

  • snake_person - Human-playable mode
  • snake_machine - AI mode

Manual Build (Alternative)

For person mode:

gcc src/person/snake_ui.c include/circular_queue.c -Wall -Wextra -pedantic -lSDL3 -lm -Iinclude -o snake_person

For machine mode:

gcc src/machine/snake_ui.c include/circular_queue.c -Wall -Wextra -pedantic -lSDL3 -lm -Iinclude -o snake_machine

Running

Person Mode

./build/bin/snake_person

Controls:

  • Arrow Keys or WASD:
    • / w - Move up
    • / a - Move left
    • / s - Move down
    • / d - Move right

Machine Mode

./build/bin/snake_machine

Watch the AI navigate using the longest-path algorithm to maximize survival.

Configuration

Edit include/const.h to customize:

  • ROWS and COLUMNS - Grid dimensions (default: 30x30)
  • WIDTH and HEIGHT - Window size (default: 1000x1000)
  • TIMER - Game speed in milliseconds (default: 75ms)
  • PADDING - Visual padding around grid (default: 40px)

After changing constants, rebuild the project:

cmake --build build

Algorithm

The machine mode uses a longest-path algorithm to determine snake movement. The algorithm:

  1. Finds paths between consecutive points using DFS/backtracking
  2. Iteratively extends path segments to maximize length
  3. Avoids creating paths that would trap the snake

See algorithm.md for detailed implementation notes.

Implementation Details

  • Circular Queue: The snake body is managed using a circular queue data structure for efficient head/tail operations
  • Collision Detection: Checks for wall collisions and self-collisions each frame
  • Smooth Rendering: Uses SDL3 textures and incremental updates for optimized rendering
  • Input Handling: Prevents opposite direction changes and implements turn buffering

Troubleshooting

If you encounter build errors:

  1. Ensure SDL3 is properly installed and findable by CMake
  2. Verify you have a C11-compatible compiler
  3. Clean the build directory: rm -rf build && mkdir build

If the game doesn't launch:

  1. Check SDL3 runtime libraries are in your library path
  2. Verify the executable has proper permissions: chmod +x build/bin/snake_person

License

This project is provided as-is for educational purposes.

About

Snake in C, with a greedy algorithm

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published