Introduction
I recently built a Tic-Tac-Toe game using Rust, the Bevy game engine, and WebAssembly.
This project combines modern game development with artificial intelligence, running directly
in the browser thanks to WebAssembly. Let me walk you through how it works and what I learned.
Technical Stack
- ๐ฆ Rust: Systems programming language
- ๐ฎ Bevy: Data-driven game engine
- ๐ง Minimax AI: Advanced game algorithm
- ๐ WebAssembly: Browser-based deployment
- ๐จ HTML/CSS: Responsive game interface
Key Features
-
Interactive Gameplay
- Click-to-play interface
- Real-time move validation
- Visual feedback with colored squares
-
AI Opponent
- Minimax algorithm implementation
- Alpha-beta pruning optimization
- Strategic decision making
-
Modern Web Interface
- Responsive design
- Loading animations
- Clean, gradient-based UI
- Tech stack badges
-
Game State Management
- Win detection
- Draw conditions
- Easy restart functionality
Code Highlights
Gameboard Setup
// Key components of the board initialization
pub struct BoardState {
pub board: [[Option<Player>; 3]; 3],
pub current_player: Player,
pub game_over: bool,
pub winner: Option<Player>
}
Rust
AI Implementation
// Minimax algorithm with alpha-beta pruning
fn minimax(board: &[[Option<Player>; 3]; 3], depth: i32, maximizing: bool) -> i32 {
// ... algorithm implementation
}
Rust
Learning Points
-
Rust and ECS Architecture
- Component-based design
- System organization
- Resource management
-
WebAssembly Integration
- Rust-to-WASM compilation
- Browser performance optimization
- Web API interactions
-
Game AI Development
- Minimax algorithm implementation
- Game tree traversal
- Performance optimization
Deployment Process
-
Build Process:
- Rust compilation to WASM
- Asset bundling
- CSS/HTML optimization
-
Hosting:
- Static file deployment
- WebAssembly loading
- Browser compatibility
Docker Integration
Development Environment
One of the key aspects of this project is its containerized development environment using Docker. This ensures consistent builds across different platforms and simplifies the setup process for contributors.
Docker Setup
The project uses a multi-stage Docker build process:
- Base development image with Rust toolchain
- WebAssembly compilation environment
- Production-ready web server
Key Docker Components
project/
โโโ Dockerfile # Main container configuration
โโโ docker-compose.yml # Development orchestration
โโโ .dockerignore # Build optimization rules
Build Process
The Docker setup handles several crucial aspects:
- Development Environment
- Rust toolchain installation
- WASM target setup
- Development dependencies
- Build Tools
- trunk for asset bundling
- wasm-pack for WebAssembly compilation
- System libraries for Bevy
- Development Workflow
- Hot reloading support
- Volume mounting for live coding
- Port forwarding for local testing
Running the Project
To start development:
# Build and start the development server
docker-compose up --build
# Access the game at http://localhost:8080
# Stop the server
docker-compose down
Benefits
- Consistency
- Same environment for all developers
- Reproducible builds
- Simplified dependency management
- Cross-Platform
- Works on Windows, macOS, and Linux
- No local Rust installation required
- Consistent WebAssembly compilation
- Production Ready
- Optimized builds for deployment
- Small container footprint
- Easy cloud deployment
This Docker integration makes the project more accessible to contributors and ensures reliable builds across different platforms and environments.
Future Improvements
- Multiplayer Support
- Difficulty Levels
- Move Animation
- Game History
- Score Tracking
Repository Sections
bevy-tic-tac-toe/
โโโ src/
โ โโโ components/
โ โโโ systems/
โ โโโ resources/
โ โโโ main.rs
โโโ dist/
โ โโโ index.html
โโโ Cargo.toml
โโโ README.md
Conclusion
This project demonstrates how modern web games can be built using Rust and WebAssembly,
providing a smooth, native-like experience in the browser. The combination of Bevy's ECS
architecture with Rust's safety guarantees made it possible to create a robust,
performant game with minimal code.
Top comments (0)