Spaces:
Configuration error
Configuration error
๐ฒ PyCatan AI
A Python library for simulating and playing The Settlers of Catan with support for multiple interfaces and AI players.
๐ Active Development: This project extends the original PyCatan with a complete simulation framework including GameManager, AI players, and multiple visualization interfaces.
โจ Features
- Complete Game Logic - Full implementation of Settlers of Catan rules
- Multiple Interfaces:
- ๐ฅ๏ธ Console/Terminal interface with colored output
- ๐ Web browser interface with interactive board
- Game Management - Turn management, phases, and game flow control
- Extensible Architecture - Easy to add AI players and custom visualizations
- Human & AI Players - Support for multiple player types
๐ฆ Installation
From Source
git clone https://github.com/levinshon-98/PyCatan_AI.git
cd PyCatan_AI
pip install -e .
Dependencies
pip install flask # For web visualization
๐ Quick Start
Play a Full Game
from pycatan import RealGame
# Start an interactive game with console and web interfaces
game = RealGame()
game.run()
Basic Game Setup
from pycatan import Game, Statuses
# Create a new game with 3 players
game = Game(num_of_players=3)
# Access the board
board = game.board
# Build a settlement (during setup phase)
point = board.points[0][0]
result = game.add_settlement(player=0, point=point, is_starting=True)
if result == Statuses.ALL_GOOD:
print("Settlement built successfully!")
Using the Game Manager
from pycatan import GameManager, HumanUser, ConsoleVisualization
# Create players
users = [
HumanUser("Alice", player_num=0),
HumanUser("Bob", player_num=1),
HumanUser("Charlie", player_num=2)
]
# Create game manager
game_manager = GameManager(users)
# Add visualization
console_viz = ConsoleVisualization()
game_manager.visualization_manager.add_visualization(console_viz)
# Start the game loop
game_manager.start_game()
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ GameManager โ โ Manages turns and flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โข game_loop() โ
โ โข handle_turn_rules() โ
โ โข coordinate_interactions() โ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโผโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Game โ โ Users โ โVisualizationsโ
โ (Core) โ โ(Players)โ โ (Display) โ
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโโโโโ
Core Components
| Component | Description |
|---|---|
Game |
Core game logic - rules, validation, state management |
GameManager |
Turn management, game flow, user coordination |
User |
Abstract base class for players (Human/AI) |
HumanUser |
Human player with CLI input |
Visualization |
Base class for display interfaces |
ConsoleVisualization |
Terminal-based game display |
WebVisualization |
Browser-based interactive board |
๐ Game Actions
Building
# Build settlement
game.add_settlement(player=0, point=point, is_starting=False)
# Build road
game.add_road(player=0, start=point1, end=point2, is_starting=False)
# Build city (upgrade settlement)
game.add_city(player=0, point=point)
Trading
# Trade with bank (4:1)
game.trade_to_bank(player=0, give=ResCard.Wood, get=ResCard.Brick)
# Trade with harbor (3:1 or 2:1)
game.trade_to_bank(player=0, give=ResCard.Wheat, get=ResCard.Ore)
Development Cards
from pycatan import DevCard
# Buy development card
game.build_dev(player=0)
# Use Knight card
game.use_dev_card(player=0, card=DevCard.Knight, args={
'robber_pos': [2, 1],
'victim': 1
})
๐ฎ Status-Based Error Handling
All game actions return Statuses enum values instead of exceptions:
from pycatan import Statuses
result = game.add_settlement(player=0, point=point)
match result:
case Statuses.ALL_GOOD:
print("Success!")
case Statuses.ERR_BLOCKED:
print("Location blocked by another building")
case Statuses.ERR_NOT_ENOUGH_RES:
print("Not enough resources")
case Statuses.ERR_BAD_LOCATION:
print("Invalid building location")
๐๏ธ Project Structure
PyCatan_AI/
โโโ pycatan/ # ๐ฆ Main library code
โ โโโ game.py # Core game logic
โ โโโ game_manager.py # Turn and flow management
โ โโโ board.py # Board base class
โ โโโ default_board.py # Standard Catan board
โ โโโ player.py # Player state management
โ โโโ card.py # Resource and development cards
โ โโโ actions.py # Action types and results
โ โโโ log_events.py # Event logging system
โ โโโ user.py # User base class
โ โโโ human_user.py # Human player implementation
โ โโโ visualization.py # Visualization base class
โ โโโ console_visualization.py # Console display
โ โโโ web_visualization.py # Web interface
โ โโโ real_game.py # Complete game orchestration
โ โโโ statuses.py # Status codes for actions
โ
โโโ tests/ # ๐งช Test suite
โ โโโ unit/ # Unit tests for individual modules
โ โโโ integration/ # Integration tests for game scenarios
โ โโโ manual/ # Manual/interactive tests
โ
โโโ examples/ # ๐ Examples and demos
โ โโโ demos/ # Playable game demonstrations
โ โโโ scripts/ # Utility scripts for development
โ
โโโ ืืืื/ # ๐ Hebrew blog posts
โโโ [Configuration files] # setup.py, README.md, etc.
๐งช Running Tests
# Run all tests
python -m pytest tests/
# Run only unit tests
python -m pytest tests/unit/
# Run only integration tests
python -m pytest tests/integration/
# Run specific test file
python -m pytest tests/unit/test_game.py
# Run with verbose output
python -m pytest tests/ -v
# Run with coverage report
python -m pytest tests/ --cov=pycatan
See tests/README.md for detailed information about the test structure.
๐ Web Visualization
The web visualization provides an interactive board in your browser:
from pycatan import WebVisualization, create_web_visualization
# Create web visualization
web_viz = create_web_visualization(port=5000)
# Start server (opens browser automatically)
web_viz.start()
# Update with game state
web_viz.update_full_state(game_state)
Features:
- ๐บ๏ธ Interactive hexagonal board
- ๐ Real-time updates via Server-Sent Events
- ๐ Player info panel with resources and points
- ๐ Action log with timestamped events
๐ Documentation
- Architecture Overview - Project design and components
- Build Plan - Development roadmap and progress
- Coding Instructions - Development guidelines
๐ค Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Write tests for new functionality
- Submit a pull request
๐ License
This project is licensed under the MIT License - see the LICENSE.txt file for details.
๐ Acknowledgments
- Original PyCatan by Josef Waller
- The Settlers of Catanยฎ is a trademark of Catan GmbH