Spaces:
Configuration error
Configuration error
File size: 8,603 Bytes
11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 69373e6 11d1573 c903325 11d1573 c903325 11d1573 c903325 69373e6 11d1573 69373e6 c903325 11d1573 69373e6 11d1573 c903325 69373e6 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 c903325 11d1573 525124a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | # ๐ฒ 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
---
|