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.
- 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
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
- CMake 3.16 or higher
- SDL3 library
- C11 compiler (GCC, Clang, etc.)
- Math library (libm, typically included with standard C library)
mkdir build
cd build
cmake ..
cmake --build .The executables will be built in build/bin/:
snake_person- Human-playable modesnake_machine- AI mode
For person mode:
gcc src/person/snake_ui.c include/circular_queue.c -Wall -Wextra -pedantic -lSDL3 -lm -Iinclude -o snake_personFor machine mode:
gcc src/machine/snake_ui.c include/circular_queue.c -Wall -Wextra -pedantic -lSDL3 -lm -Iinclude -o snake_machine./build/bin/snake_personControls:
- Arrow Keys or WASD:
↑/w- Move up←/a- Move left↓/s- Move down→/d- Move right
./build/bin/snake_machineWatch the AI navigate using the longest-path algorithm to maximize survival.
Edit include/const.h to customize:
ROWSandCOLUMNS- Grid dimensions (default: 30x30)WIDTHandHEIGHT- 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 buildThe machine mode uses a longest-path algorithm to determine snake movement. The algorithm:
- Finds paths between consecutive points using DFS/backtracking
- Iteratively extends path segments to maximize length
- Avoids creating paths that would trap the snake
See algorithm.md for detailed implementation notes.
- 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
If you encounter build errors:
- Ensure SDL3 is properly installed and findable by CMake
- Verify you have a C11-compatible compiler
- Clean the build directory:
rm -rf build && mkdir build
If the game doesn't launch:
- Check SDL3 runtime libraries are in your library path
- Verify the executable has proper permissions:
chmod +x build/bin/snake_person
This project is provided as-is for educational purposes.