A command-line implementation of Tic Tac Toe using object-oriented programming principles in Rust.
- Multiplayer support (2-4 players)
- Human vs Human, Human vs AI, or AI vs AI gameplay
- Adjustable board size (3-10)
- Multiple AI difficulty levels (Easy, Medium, Hard)
- Unique emoji symbols for each player
- Clean command-line interface with aligned grid
- Object-oriented design with modular components
- Make sure you have Rust and Cargo installed. If not, install from rustup.rs
- Clone this repository:
git clone https://github.com/vikyw89/rust-tic-tac-toe.git
cd rust-tic-tac-toe- Build the project:
cargo build --releaseRun the game:
cargo runThe game will prompt you for:
- Board size (3-10, default: 3)
- Number of players (2-4, default: 2)
- For each player:
- Type (Human or AI)
- Name (for human players)
- Difficulty level (for AI players)
When prompted for a move, enter the row and column numbers (0-based) separated by a space. For example:
Enter your move (row col): 1 1 # This will place your symbol in the center
The project follows a flat directory structure with modular design, separating the library components from executable recipes.
Core game logic and components are organized as a library:
lib.rs- Library root and public exportsgame.rs- Game flow and state managementgame_board.rs- Board implementation and move validationplayer.rs- Player traits and implementationsai.rs- AI player logic and difficulty levelsui.rs- User interface componentstypes.rs- Shared types and enums
Executable examples and implementations:
tic_tac_toe.rs- Main game executable with CLI interface
# Run the main game
cargo run
# Run with release optimizations
cargo run --releasepub struct Symbol(char); // Holds emoji character
pub enum GameStatus { InProgress, Win(String), Draw }
pub enum GameResult { Win, Loss, Draw }
pub enum Difficulty { Easy, Medium, Hard }pub enum GameError {
InvalidMove,
OutOfTurn,
GameOver,
MaxPlayersReached
}
pub enum PlayerError {
NotFound,
InvalidData
}rand = "0.8.5": For randomizing first player, AI moves, and player symbols
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
MIT License