Skip to main content

A modern, well-tested Tic-Tac-Toe game implementation in Python with CLI interface and AI opponent

Project description

Tic-Tac-Toe Game

CI Coverage Code style: black

A well-tested Tic-Tac-Toe game implementation in Python with a CLI interface and AI opponent.


Features

  • Interactive command-line interface
  • AI opponent with multiple difficulty levels
  • Human vs Human gameplay
  • Smart AI using minimax algorithm
  • Comprehensive test suite with >95% coverage
  • Modern Python development setup with pre-commit hooks
  • Proper package structure and configuration

Installation

From PyPI

pip install tictactoe-vish

Development Installation

git clone https://github.com/v-s-v-i-s-h-w-a-s/tic-tac-toe.git
cd tic-tac-toe
pip install -e .[dev]

Usage

Command Line Interface

# Run the game
tictactoe

# Or if installed in development mode
python -m src.tictactoe.cli

As a Library

from tictactoe import Board, AIPlayer

# Create a game
board = Board()
ai = AIPlayer(difficulty="hard")

# Make moves
board.make_move(1, 1)  # Human move to center
ai_move = ai.get_best_move(board)  # AI calculates best move
board.make_move(ai_move[0], ai_move[1])  # AI makes move

# Check game state
if board.check_winner():
    print(f"Winner: {board.check_winner()}")
elif board.is_full():
    print("It's a tie!")

Game Rules

  • Players take turns placing X's and O's on a 3x3 grid
  • First player to get 3 in a row (horizontally, vertically, or diagonally) wins
  • If the grid fills up with no winner, it's a tie
  • Positions are specified as row,col coordinates (0-2 for each)

AI Difficulty Levels

  • Easy: Random moves
  • Medium: Blocks opponent wins and takes available wins
  • Hard: Uses minimax algorithm for optimal play

Development

Setup Development Environment

# Clone and install
git clone https://github.com/v-s-v-i-s-h-w-a-s/tic-tac-toe.git
cd tic-tac-toe
pip install -e .[dev]

# Install pre-commit hooks
pre-commit install

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=src/tictactoe --cov-report=html

# Run specific test file
pytest tests/test_board.py

Code Quality

# Format code
black src tests

# Sort imports
isort src tests

# Lint code
flake8 src tests
pylint src

# Run all pre-commit hooks
pre-commit run --all-files

Project Structure

tic-tac-toe/
├── .github/workflows/        # CI/CD pipelines
├── src/tictactoe/            # Main package
│   ├── __init__.py           # Package initialization
│   ├── board.py              # Game logic
│   ├── cli.py                # Command-line interface
│   └── ai.py                 # AI implementation
├── tests/                    # Test suite
├── pyproject.toml            # Project configuration
├── .pre-commit-config.yaml   # Code quality hooks
└── README.md                 # This file

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b amazing-feature)
  3. Make your changes
  4. Run the test suite (pytest)
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin amazing-feature)
  7. Open a Pull Request

Development Guidelines

  • Write tests for new features
  • Maintain code coverage above 95%
  • Follow PEP 8 style guidelines (enforced by black and flake8)
  • Add type hints to new functions
  • Update documentation as needed

License

This project is licensed under the MIT License - see the LICENSE file for details.


Acknowledgments

  • Classic Tic-Tac-Toe game rules
  • Minimax algorithm for AI implementation
  • Python community for excellent tooling

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tictactoe_vish-0.1.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tictactoe_vish-0.1.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file tictactoe_vish-0.1.0.tar.gz.

File metadata

  • Download URL: tictactoe_vish-0.1.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for tictactoe_vish-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e46e53a55a21b87bada19d112ac30b403c45674db4aaa716158d3f66a50d49ba
MD5 ba2088f776f8cadfa3395a3cbce2bf40
BLAKE2b-256 4a709a9b22e37978cde572830c8e5edd307113ab5f414e6786984427a77e1daa

See more details on using hashes here.

File details

Details for the file tictactoe_vish-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tictactoe_vish-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for tictactoe_vish-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 22aeb8be818cb8b3428972057e0e087362139bddae68a1108544ddca3a7bea9f
MD5 1c4d996da0cdbb523c6c369e95b1912f
BLAKE2b-256 42a1b46b52d73ebf01b423dd88a5d85f286e3879279672ecd3edc3984afbf908

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page