Skip to content

vikyw89/rust-tic-tac-toe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tic Tac Toe in Rust

A feature-rich, command-line implementation of Tic Tac Toe (also known as Noughts and Crosses) built with Rust. Supports multiple players, AI opponents with varying difficulty levels, and customizable board sizes.

Features

  • Flexible Player Support: 2-4 players in a single game
  • Game Modes: Human vs Human, Human vs AI, or AI vs AI
  • Adjustable Board Size: Play on boards from 3x3 to 10x10
  • AI Opponents: Three difficulty levels:
    • Easy: Random moves
    • Medium: 70% smart moves, 30% random
    • Hard: Strategic play with win-seeking, blocking, and fork prevention
  • Unique Emoji Symbols: 12 distinct animal emoji for player identification
  • Clean CLI Interface: Formatted grid with row/column indicators
  • Modular Architecture: Well-organized library code suitable for extension

Installation

Prerequisites

Build

git clone https://github.com/vikyw89/rust-tic-tac-toe.git
cd rust-tic-tac-toe
cargo build --release

Run

cargo run
# or for release builds
cargo run --release

Usage

The game prompts for configuration:

  1. Board size (3-10, default: 3)
  2. Number of players (2-4, default: 2)
  3. For each player:
    • Type: Human (1) or AI (2)
    • Name (for humans) or auto-generated (for AI)
    • AI difficulty (if AI player)

Making Moves

Enter row and column as 0-based coordinates:

Enter your move (row col): 1 1

Example board output:

   0   1   2  
-------------
0 | X |   | O |
-------------
1 |   | X |   |
-------------
2 |   |   |   |
-------------

Project Structure

rust-tic-tac-toe/
├── src/
│   ├── lib.rs           # Library root and public exports
│   ├── game.rs          # Game state and flow management
│   ├── game_board.rs    # Board implementation and win detection
│   ├── player.rs        # Player struct and logic
│   ├── ai.rs            # AI decision-making logic
│   ├── ui.rs            # Command-line interface
│   ├── types.rs         # Shared types, enums, errors
│   └── score_board.rs   # Player statistics tracking
├── recipes/
│   └── tic_tac_toe.rs   # Main game executable
├── tests/
│   └── ai_performance.rs # AI performance benchmarks
├── Cargo.toml
└── README.md

Architecture

Core Types (types.rs)

Symbol(char)           // Emoji character wrapper
GameStatus             // InProgress, Win(winner), Draw
Difficulty             // Easy, Medium, Hard
GameError              // InvalidMove, MaxPlayersReached, GameOver, etc.
BoardError             // InvalidPosition, CellOccupied, OutOfBounds
PlayerError            // NotFound, InvalidData

Game Flow (game.rs)

  • Game::new() - Create game with board size and max players
  • add_player() - Register players with unique symbols
  • make_move() - Execute a move if valid
  • is_over() / winner() - Check game state
  • randomize_turn() - Random starting player

Board Logic (game_board.rs)

  • NxN grid with configurable size
  • Win detection for rows, columns, and diagonals
  • Move validation and undo capability
  • Position evaluation for AI

AI Strategy (ai.rs)

Difficulty Strategy
Easy Random position selection
Medium 70% strategic, 30% random
Hard Prioritizes: center > wins > blocks > forks > corners

Testing

cargo test

Run AI performance benchmarks:

cargo test test_three_player_ai_performance -- --nocapture

Dependencies

  • rand = "0.8.5" - Randomization for symbols, turns, and AI moves

License

MIT License - See LICENSE for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a Pull Request

Releases

No releases published

Packages

 
 
 

Contributors

Languages