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
---