@pompidup/kingdomino-engine 2.3.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +155 -2
- package/dist/index.d.ts +701 -4
- package/dist/index.js +520 -132
- package/dist/index.js.map +1 -1
- package/package.json +13 -15
package/README.md
CHANGED
|
@@ -7,10 +7,42 @@
|
|
|
7
7
|

|
|
8
8
|

|
|
9
9
|
|
|
10
|
+
A simple, lightweight TypeScript engine for the Kingdomino board game.
|
|
11
|
+
|
|
12
|
+
## Table of Contents
|
|
13
|
+
|
|
14
|
+
- [Overview](#overview)
|
|
15
|
+
- [Requirements](#requirements)
|
|
16
|
+
- [Features](#features)
|
|
17
|
+
- [Installation](#installation)
|
|
18
|
+
- [Usage](#usage)
|
|
19
|
+
- [Basic Setup](#basic-setup)
|
|
20
|
+
- [Options for GameEngine](#options-for-gameengine)
|
|
21
|
+
- [Adding Players](#adding-players)
|
|
22
|
+
- [Getting Available Game Modes](#getting-available-game-modes)
|
|
23
|
+
- [Getting Available Extra Rules](#getting-available-extra-rules)
|
|
24
|
+
- [Setting Up Extra Rules](#setting-up-extra-rules)
|
|
25
|
+
- [Starting the Game](#starting-the-game)
|
|
26
|
+
- [Player Actions](#player-actions)
|
|
27
|
+
- [Game Flow](#game-flow)
|
|
28
|
+
- [Scoring](#scoring)
|
|
29
|
+
- [Complete Game Example](#complete-game-example)
|
|
30
|
+
- [Extra Rules](#extra-rules)
|
|
31
|
+
- [API Documentation](#api-documentation)
|
|
32
|
+
- [Key Types and Interfaces](#key-types-and-interfaces)
|
|
33
|
+
- [Methods](#methods)
|
|
34
|
+
- [Development](#development)
|
|
35
|
+
- [Setup](#setup)
|
|
36
|
+
- [Testing](#testing)
|
|
37
|
+
- [Building](#building)
|
|
38
|
+
- [Contributing](#contributing)
|
|
39
|
+
|
|
10
40
|
## Overview
|
|
11
41
|
|
|
12
42
|
This is a simple and lightweight TypeScript engine designed to facilitate the gameplay of Kingdomino. It provides the core logic for managing game states, rules, and player interactions, making it easy to integrate into any application that requires a Kingdomino game engine.
|
|
13
43
|
|
|
44
|
+
Kingdomino is a tile-placement game where players build kingdoms by connecting domino-like tiles with different terrains. This engine handles all the game logic, allowing you to focus on building the user interface and experience.
|
|
45
|
+
|
|
14
46
|
## Requirements
|
|
15
47
|
|
|
16
48
|
- Node v20.6.0 or higher
|
|
@@ -22,6 +54,8 @@ This is a simple and lightweight TypeScript engine designed to facilitate the ga
|
|
|
22
54
|
- **Player Interaction**: Simplify player interactions, including drawing tiles, placing dominoes, and scoring.
|
|
23
55
|
- **Extra Rules Support**: Implement and manage additional game rules for enhanced gameplay.
|
|
24
56
|
- **Multiple Game Modes**: Support for different game modes, including Classic and potentially others.
|
|
57
|
+
- **Scoring System**: Automatic calculation of scores based on kingdom layouts and rule sets.
|
|
58
|
+
- **Typescript Support**: Full TypeScript support with comprehensive type definitions.
|
|
25
59
|
|
|
26
60
|
## Installation
|
|
27
61
|
|
|
@@ -81,7 +115,7 @@ const engine = createGameEngine({});
|
|
|
81
115
|
Add players to the game by passing an array of player names:
|
|
82
116
|
|
|
83
117
|
```typescript
|
|
84
|
-
gameState = engine.addPlayers({gameState, ["Player 1", "Player 2", "Player 3"]});
|
|
118
|
+
gameState = engine.addPlayers({ game: gameState, players: ["Player 1", "Player 2", "Player 3"] });
|
|
85
119
|
```
|
|
86
120
|
|
|
87
121
|
### Getting Available Game Modes
|
|
@@ -141,7 +175,6 @@ gameState = engine.placeDomino({
|
|
|
141
175
|
game: gameState,
|
|
142
176
|
lordId: "lordId",
|
|
143
177
|
position: { x: 0, y: 0 },
|
|
144
|
-
orientation: "horizontal",
|
|
145
178
|
rotation: 0,
|
|
146
179
|
});
|
|
147
180
|
```
|
|
@@ -186,6 +219,68 @@ You have the possibility to get the results at any time of a kingdom, you can us
|
|
|
186
219
|
const score = engine.calculateScore({ kingdom: gameState.players[0].kingdom });
|
|
187
220
|
```
|
|
188
221
|
|
|
222
|
+
## Complete Game Example
|
|
223
|
+
|
|
224
|
+
Here's a complete example of how to simulate a game from start to finish:
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import { createGameEngine, isGameWithNextAction } from "@pompidup/kingdomino-engine";
|
|
228
|
+
|
|
229
|
+
// Create the game engine
|
|
230
|
+
const engine = createGameEngine({
|
|
231
|
+
logging: true,
|
|
232
|
+
// Optional: provide custom shuffle method
|
|
233
|
+
shuffleMethod: (array) => array,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// Create a new game with Classic mode
|
|
237
|
+
let game = engine.createGame({ mode: "Classic" });
|
|
238
|
+
|
|
239
|
+
// Add players
|
|
240
|
+
const players = ["Alice", "Bob"];
|
|
241
|
+
game = engine.addPlayers({ game, players });
|
|
242
|
+
|
|
243
|
+
// Start the game
|
|
244
|
+
game = engine.startGame({ game });
|
|
245
|
+
|
|
246
|
+
// Game loop - continue until there are no more actions
|
|
247
|
+
while (isGameWithNextAction(game)) {
|
|
248
|
+
const currentLordId = game.nextAction.nextLord;
|
|
249
|
+
const nextAction = game.nextAction.nextAction;
|
|
250
|
+
|
|
251
|
+
// Handle different actions
|
|
252
|
+
if (nextAction === "pickDomino") {
|
|
253
|
+
// Choose a domino from the available ones
|
|
254
|
+
const dominoToChoose = game.currentDominoes[0].domino.number;
|
|
255
|
+
game = engine.chooseDomino({
|
|
256
|
+
game,
|
|
257
|
+
lordId: currentLordId,
|
|
258
|
+
dominoPick: dominoToChoose,
|
|
259
|
+
});
|
|
260
|
+
} else if (nextAction === "placeDomino") {
|
|
261
|
+
// Try to place the domino
|
|
262
|
+
try {
|
|
263
|
+
game = engine.placeDomino({
|
|
264
|
+
game,
|
|
265
|
+
lordId: currentLordId,
|
|
266
|
+
position: { x: 0, y: 0 }, // Position to place the domino
|
|
267
|
+
rotation: 0, // Rotation of the domino (0, 90, 180, 270)
|
|
268
|
+
});
|
|
269
|
+
} catch (error) {
|
|
270
|
+
// If placement fails, discard the domino
|
|
271
|
+
game = engine.discardDomino({
|
|
272
|
+
game,
|
|
273
|
+
lordId: currentLordId,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Get the final results
|
|
280
|
+
const gameResult = engine.getResults({ game });
|
|
281
|
+
console.log("Game results:", gameResult.result);
|
|
282
|
+
```
|
|
283
|
+
|
|
189
284
|
## Extra Rules
|
|
190
285
|
|
|
191
286
|
The engine supports extra rules that can modify gameplay. Some examples include:
|
|
@@ -193,6 +288,13 @@ The engine supports extra rules that can modify gameplay. Some examples include:
|
|
|
193
288
|
- **The Middle Kingdom**: Gain 10 additional points if your castle is in the middle of the kingdom.
|
|
194
289
|
- **Harmony**: Gain 5 additional points if your kingdom is complete (no discarded dominoes).
|
|
195
290
|
|
|
291
|
+
You can get all available extra rules for a specific game mode and number of players:
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
const extraRules = engine.getExtraRules({ mode: "Classic", players: 2 });
|
|
295
|
+
console.log("Available extra rules:", extraRules);
|
|
296
|
+
```
|
|
297
|
+
|
|
196
298
|
## API Documentation
|
|
197
299
|
|
|
198
300
|
### Key Types and Interfaces
|
|
@@ -303,6 +405,57 @@ Calculates and returns the final game results.
|
|
|
303
405
|
|
|
304
406
|
Calculates and returns the score of a kingdom.
|
|
305
407
|
|
|
408
|
+
## Development
|
|
409
|
+
|
|
410
|
+
### Setup
|
|
411
|
+
|
|
412
|
+
To set up the project for development:
|
|
413
|
+
|
|
414
|
+
1. Clone the repository:
|
|
415
|
+
```bash
|
|
416
|
+
git clone https://github.com/Pompidup/KDomino.git
|
|
417
|
+
cd KDomino
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
2. Install dependencies:
|
|
421
|
+
```bash
|
|
422
|
+
pnpm install
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
### Testing
|
|
426
|
+
|
|
427
|
+
The project uses Vitest for testing. To run the tests:
|
|
428
|
+
|
|
429
|
+
```bash
|
|
430
|
+
# Run all tests
|
|
431
|
+
pnpm test
|
|
432
|
+
|
|
433
|
+
# Run tests with coverage
|
|
434
|
+
pnpm coverage
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
### Building
|
|
438
|
+
|
|
439
|
+
To build the project:
|
|
440
|
+
|
|
441
|
+
```bash
|
|
442
|
+
pnpm build
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
## Contributing
|
|
446
|
+
|
|
447
|
+
Contributions are welcome! Here's how you can contribute:
|
|
448
|
+
|
|
449
|
+
1. Fork the repository
|
|
450
|
+
2. Create a new branch (`git checkout -b feature/your-feature-name`)
|
|
451
|
+
3. Make your changes
|
|
452
|
+
4. Run tests to ensure everything works (`pnpm test`)
|
|
453
|
+
5. Commit your changes (`git commit -m 'Add some feature'`)
|
|
454
|
+
6. Push to the branch (`git push origin feature/your-feature-name`)
|
|
455
|
+
7. Open a Pull Request
|
|
456
|
+
|
|
457
|
+
Please make sure your code follows the existing style and includes appropriate tests.
|
|
458
|
+
|
|
306
459
|
## Advanced Usage
|
|
307
460
|
|
|
308
461
|
For more advanced usage and detailed API documentation, please refer to the source code and inline comments.
|