A noughts-and-crosses (tic-tac-toe) game with vector database integration and multiple bot modes.
- ASCII Terminal Interface: Beautiful terminal-based game interface using Rich
- Multiple Bot Modes:
- Random: Makes random valid moves
- Algorithm: Uses minimax algorithm with alpha-beta pruning
- Vector: Uses vector database similarity search
- Configurable Board Size: Support for different board sizes (3x3, 4x4, etc.)
- Vector Database Integration: ChromaDB for storing and querying game states
- Poetry Environment Management: Modern Python dependency management
-
Clone the repository:
git clone <repository-url> cd vector-ox
-
Setup with Poetry:
# Install Poetry if you haven't already curl -sSL https://install.python-poetry.org | python3 - # Install dependencies poetry install
-
Initial Setup:
# Setup with default 3x3 board make setup # Or setup with custom board size make setup BOARD_SIZE=4
# Play with default settings (3x3 board, random bot)
make play
# Or use poetry directly
poetry run python -m vector_ox.game
# With custom options
poetry run python -m vector_ox.game --board-size 4 --bot-type algorithm
random
: Makes random valid movesalgorithm
: Uses minimax algorithm (optimal play)vector
: Uses vector database similarity search
# Show all available commands
make help
# Setup project with custom board size
make setup BOARD_SIZE=4
# Install dependencies
make install
# Install development dependencies
make dev
# Run tests
make test
# Run linting
make lint
# Format code
make format
# Clean up generated files
make clean
# Generate training data
make generate-data
# Build vector database
make build-vectors
# Test bot performance
make play-test
# Run extensive bot tournament
make play-test-extensive
# Test bots and save results
make play-test-save
vector-ox/
├── pyproject.toml # Poetry configuration
├── Makefile # Build and utility commands
├── README.md # This file
├── vector_ox/ # Main package
│ ├── __init__.py
│ ├── game.py # Main game interface
│ ├── board.py # Board logic and state management
│ ├── bots.py # Bot implementations
│ ├── data_generator.py # Training data generation
│ ├── vector_builder.py # Vector database builder
│ └── setup.py # Project setup
├── vector_db/ # Vector database storage (created during setup)
└── training_data.txt # Generated training data
graph TB
subgraph "Data Generation"
A[Random Bot] --> D[Game Generator]
B[Algorithm Bot] --> D
D --> E[Training Data]
end
subgraph "Vector Database"
E --> F[Vector Builder]
F --> G[ChromaDB]
G --> H[Vector Embeddings]
end
subgraph "Game Engine"
I[Board State] --> J[Move Selection]
J --> K[Random Bot]
J --> L[Algorithm Bot]
J --> M[Vector Bot]
M --> N[Similarity Search]
N --> G
end
subgraph "Testing & Analysis"
O[Bot Tester] --> P[Performance Charts]
O --> Q[Win Rate Analysis]
O --> R[Matchup Results]
end
- Data Generation: The system generates thousands of games using random and algorithm bots
- Vector Representation: Each board state is converted to a numerical vector
- Storage: Board states and moves are stored in ChromaDB with vector embeddings
- Similarity Search: During gameplay, the vector bot finds similar board states and uses their moves
flowchart TD
A[Start] --> B[Get available moves]
B --> C[Select random move]
C --> D[Return move]
D --> E[End]
Steps:
- Get Available Moves: Identifies all empty positions on the board
- Random Selection: Uses Python's
random.choice()
to pick any available move - Return Move: Returns the randomly selected position
flowchart TD
A[Start] --> B[Get available moves]
B --> C[For each move]
C --> D[Make move on board]
D --> E[Call minimax with alpha-beta]
E --> F[Undo move]
F --> G[Update best score]
G --> H{More moves?}
H -->|Yes| C
H -->|No| I[Return best move]
I --> J[End]
subgraph "Minimax Function"
K[Check if game over]
K --> L{Game over?}
L -->|Yes| M[Return score]
L -->|No| N{Maximizing player?}
N -->|Yes| O[Find max score]
N -->|No| P[Find min score]
O --> Q[Alpha-beta pruning]
P --> Q
Q --> R[Return best score]
end
Steps:
- Move Generation: Creates list of all possible moves
- Move Evaluation: For each move:
- Temporarily makes the move
- Calls minimax algorithm to evaluate resulting position
- Undoes the move
- Minimax Algorithm:
- Terminal Check: If game is over, return score (win=+10, lose=-10, tie=0)
- Recursive Search: Explore all possible future moves
- Score Calculation: Maximize score for current player, minimize for opponent
- Alpha-Beta Pruning: Skip evaluating branches that won't affect final decision
- Best Move Selection: Return move with highest score
flowchart TD
A[Start] --> B[Convert board to vector]
B --> C[Query vector database]
C --> D{Database available?}
D -->|No| E[Fallback to random]
D -->|Yes| F[Search similar states]
F --> G{Found similar states?}
G -->|No| E
G -->|Yes| H[Extract moves from results]
H --> I[Count move frequencies]
I --> J{Best move found?}
J -->|Yes| K[Return most common move]
J -->|No| E
K --> L[End]
E --> L
Steps:
- State Vectorization: Convert current board state to numerical vector (X=1, O=-1, Empty=0)
- Database Query: Search vector database for similar board states
- Similarity Matching: Find board states with similar vector representations
- Move Extraction: Extract moves made in similar situations
- Frequency Analysis: Count how often each move appears in similar states
- Confidence Calculation: Calculate confidence based on frequency of moves
- Move Selection: Return most frequently chosen move from similar states
- Fallback Handling: If no similar states found, use random move
graph LR
A[Random Bot] --> B[15-20% Win Rate]
C[Algorithm Bot] --> D[24-25% Win Rate]
E[Vector Bot] --> F[25-26% Win Rate]
G[Perfect Play] --> H[Always Ties vs Perfect Opponent]
I[Learning] --> J[Improves with More Training Data]
sequenceDiagram
participant P as Player
participant G as Game Engine
participant B as Bot
participant V as Vector DB
P->>G: Start Game
G->>G: Initialize Board
loop Game Loop
G->>P: Display Board
P->>G: Make Move
G->>G: Update Board
alt Game Not Over
G->>B: Bot's Turn
alt Vector Bot
B->>V: Query Similar States
V->>B: Return Similar Moves
B->>G: Make Move with Explanation
else Algorithm Bot
B->>B: Calculate Minimax
B->>G: Make Move with Strategy
else Random Bot
B->>G: Make Random Move
end
end
end
G->>P: Display Result
The project includes comprehensive bot testing capabilities:
# Quick test (50 games per matchup)
make play-test
# Extensive test (200 games per matchup)
make play-test-extensive
# Test and save results to file
make play-test-save
The testing system:
- Pits all bots against each other (random vs vector, random vs algorithm, vector vs algorithm)
- Tracks win rates, losses, and ties
- Generates ASCII bar charts showing performance
- Provides detailed matchup analysis
- Saves results to JSON for further analysis
make test
make format
make lint
The game supports different board sizes. For 3x3 boards, you can use numbers 1-9 for moves. For larger boards, use the row,col
format.
The vector database is stored in the vector_db/
directory and contains:
- Board state vectors
- Associated moves
- Game outcomes
If the vector bot falls back to random moves, it means:
- The vector database is empty or corrupted
- No similar board states were found
- The vector database connection failed
To rebuild the vector database:
make generate-data
make build-vectors
If you encounter Poetry-related issues:
# Clear Poetry cache
poetry cache clear --all
# Reinstall dependencies
poetry install --sync
This project is open source and available under the MIT License.