# ๐ŸŽฒ 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](https://github.com/josefwaller/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 ```bash git clone https://github.com/levinshon-98/PyCatan_AI.git cd PyCatan_AI pip install -e . ``` ### Dependencies ```bash pip install flask # For web visualization ``` ## ๐Ÿš€ Quick Start ### Play a Full Game ```python from pycatan import RealGame # Start an interactive game with console and web interfaces game = RealGame() game.run() ``` ### Basic Game Setup ```python 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 ```python 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 ```python # 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 ```python # 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 ```python 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: ```python 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 ```bash # 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](tests/README.md) for detailed information about the test structure. ## ๐ŸŒ Web Visualization The web visualization provides an interactive board in your browser: ```python 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](.github/instructions/ARCHITECTURE.md) - Project design and components - [Build Plan](.github/instructions/BUILD_PLAN.md) - Development roadmap and progress - [Coding Instructions](.github/copilot-instructions.md) - Development guidelines ## ๐Ÿค Contributing Contributions are welcome! Please: 1. Fork the repository 2. Create a feature branch 3. Write tests for new functionality 4. Submit a pull request ## ๐Ÿ“„ License This project is licensed under the MIT License - see the [LICENSE.txt](LICENSE.txt) file for details. ## ๐Ÿ™ Acknowledgments - Original [PyCatan](https://github.com/josefwaller/PyCatan) by Josef Waller - The Settlers of Catanยฎ is a trademark of Catan GmbH --- **Made with โค๏ธ for Catan enthusiasts and AI developers** --- title: PyCatan AI sdk: docker app_port: 7860 pinned: false license: mit ---