knightstour 1.0.2 → 1.0.3
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 +115 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
```
|
|
2
|
+
# KnightsTour
|
|
3
|
+
|
|
4
|
+
A flexible **Knight's Tour** game engine in JavaScript/TypeScript.
|
|
5
|
+
Supports single-player (Open/Closed tours) and multiplayer (Co-op/Versus) modes, with built-in move validation, undo, hints, and automated tour generation.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install knightstour
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
or with Yarn:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
yarn add knightstour
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import KnightsTour from "knightstour";
|
|
27
|
+
|
|
28
|
+
// Create a new game
|
|
29
|
+
const game = new KnightsTour({
|
|
30
|
+
rows: 8,
|
|
31
|
+
cols: 8,
|
|
32
|
+
players: [
|
|
33
|
+
{ name: "Alice", color: "red", moveIndexes: [] },
|
|
34
|
+
{ name: "Bob", color: "blue", moveIndexes: [] }
|
|
35
|
+
],
|
|
36
|
+
gameMode: "Co-op", // Open, Closed, Versus, Co-op
|
|
37
|
+
disabledIndexes: [0, 7] // optional blocked cells
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Make a move
|
|
41
|
+
game.move(10);
|
|
42
|
+
|
|
43
|
+
// Undo last move
|
|
44
|
+
game.undo();
|
|
45
|
+
|
|
46
|
+
// Get the next available moves for current player
|
|
47
|
+
const nextMoves = game.getAvailableMovesFor(game.turnIndex);
|
|
48
|
+
console.log(nextMoves);
|
|
49
|
+
|
|
50
|
+
// Reset the game
|
|
51
|
+
game.reset();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## API
|
|
57
|
+
|
|
58
|
+
### `new KnightsTour(config?: KnightsTourConfig)`
|
|
59
|
+
|
|
60
|
+
- `rows` – number of rows (default: 6)
|
|
61
|
+
- `cols` – number of columns (default: 5)
|
|
62
|
+
- `players` – array of player objects:
|
|
63
|
+
```ts
|
|
64
|
+
{
|
|
65
|
+
name: string;
|
|
66
|
+
color: string;
|
|
67
|
+
moveIndexes: number[];
|
|
68
|
+
CPU?: boolean; // optional
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
- `gameMode` – `"Open" | "Closed" | "Co-op" | "Versus"` (default depends on player count)
|
|
72
|
+
- `disabledIndexes` – array of blocked cell indices
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
### Methods
|
|
77
|
+
|
|
78
|
+
- `move(index: number): boolean` – Move the current player to the given index
|
|
79
|
+
- `undo(): void` – Undo the last move
|
|
80
|
+
- `getAvailableMovesFor(playerIndex: number): number[]` – Get valid moves for a player
|
|
81
|
+
- `reset(): void` – Reset the game to initial configuration
|
|
82
|
+
- `updateBoard(autoMove?: boolean): void` – Refresh the board and calculate next moves
|
|
83
|
+
- `hint(): void` – Suggest and execute the best move for the current player
|
|
84
|
+
- `solve(increment?: number): void` – Attempt to solve the tour (single-player)
|
|
85
|
+
- `generate(increment?: number): void` – Attempt to generate a valid tour
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
### Properties
|
|
90
|
+
|
|
91
|
+
- `rows`, `cols` – board dimensions
|
|
92
|
+
- `players` – current players with their moves
|
|
93
|
+
- `turnIndex` – index of current player
|
|
94
|
+
- `cells` – array of cell objects for rendering
|
|
95
|
+
- `state` – current game state (e.g., `"Game Over"`, `"Alice's Turn"`)
|
|
96
|
+
- `solved` – boolean indicating if a tour is completed
|
|
97
|
+
- `generated` – boolean indicating if a tour was generated
|
|
98
|
+
- `allMoveIndexes` – flattened move history of all players
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## TypeScript Support
|
|
103
|
+
|
|
104
|
+
Type definitions are included. Importing in TypeScript will give full IntelliSense support.
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import KnightsTour from "knightstour";
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT © Puzzookie
|
|
115
|
+
```
|