Tic Tac Toe Multiplayer in Zig
A multiplayer Tic Tac Toe game built to learn the Zig programming language.
Educational Purpose
This project was created for educational purposes to learn:
- Zig programming language fundamentals
- TCP socket networking
- State management
- Modularity
What we have
- Local Play: Two players on the same computer
- Network Multiplayer: Host or join games over TCP
- Real-time Synchronization: Instant move updates between players
- Cross-platform: Works on macOS, Linux, and Windows
Technologies Used
- Zig - A general-purpose programming language and toolchain
- Raylib - A simple and easy-to-use library for game development
- raylib-zig - Zig bindings for Raylib
Prerequisites
Getting Started
- Clone the repository:
git clone https://github.com/yourusername/learn-zig.git
cd learn-zig
- Build the project:
zig build
- Run the game:
zig build run
How to Play
Local Play
- Launch the game and select "Local Play"
- Players take turns clicking on the grid
- First player to get 3 in a row wins
Network Multiplayer
To host a game:
- Select "Host Game" from the main menu
- Note the port number (default: 5555)
- Share your IP address with the other player
- Wait for them to connect
To join a game:
- Select "Join Game" from the main menu
- Enter the host's IP address
- Enter the port number (default: 5555)
- Click "Connect"
Game Rules:
- The host plays as X and goes first
- The client plays as O
- Players take turns clicking empty squares
- First to get 3 in a row wins
Project Structure
src/
├── main.zig # Entry point and game loop
├── game.zig # Game logic and state management
├── renderer.zig # All drawing and UI rendering
├── network.zig # TCP networking implementation
├── ui_state.zig # UI state definitions
├── input.zig # Input handling utilities
└── constants.zig # Shared constants and configuration
Building from Source
The project uses Zig's built-in build system. The build.zig
file configures:
- Executable compilation
- Raylib dependency linking
- Build options and flags
To build in release mode for better performance:
zig build -Doptimize=ReleaseFast
Network Protocol
The multiplayer mode uses a simple TCP protocol with message types:
CONNECT
- Initial handshake
MOVE
- Transmit player moves
RESET
- Restart the game
DISCONNECT
- Clean disconnection
Messages are serialized with a 4-byte header indicating message length.
Learning Resources
For those learning Zig:
For Raylib:
Acknowledgments
- The Zig community for excellent documentation
- Raylib
- raylib-zig for the Zig bindings of the Raylib