gungi.js 1.0.20 → 3.0.1
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/LICENSE +2 -2
- package/README.md +674 -306
- package/dist/index.d.ts +109 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -29
- package/lib/gungi.d.ts +0 -58
- package/lib/gungi.d.ts.map +0 -1
- package/lib/gungi.js +0 -381
- package/lib/gungi.js.map +0 -1
- package/lib/interfaces/igungi.d.ts +0 -32
- package/lib/interfaces/igungi.d.ts.map +0 -1
- package/lib/interfaces/igungi.js +0 -8
- package/lib/interfaces/igungi.js.map +0 -1
- package/lib/utils/constants.d.ts +0 -37
- package/lib/utils/constants.d.ts.map +0 -1
- package/lib/utils/constants.js +0 -40
- package/lib/utils/constants.js.map +0 -1
- package/lib/utils/helper.d.ts +0 -15
- package/lib/utils/helper.d.ts.map +0 -1
- package/lib/utils/helper.js +0 -148
- package/lib/utils/helper.js.map +0 -1
- package/lib/utils/move_gen.d.ts +0 -25
- package/lib/utils/move_gen.d.ts.map +0 -1
- package/lib/utils/move_gen.js +0 -1112
- package/lib/utils/move_gen.js.map +0 -1
- package/lib/utils/stockpile_move_gen.d.ts +0 -9
- package/lib/utils/stockpile_move_gen.d.ts.map +0 -1
- package/lib/utils/stockpile_move_gen.js +0 -263
- package/lib/utils/stockpile_move_gen.js.map +0 -1
- package/src/gungi.ts +0 -593
- package/src/interfaces/igungi.ts +0 -133
- package/src/utils/constants.ts +0 -41
- package/src/utils/helper.ts +0 -199
- package/src/utils/move_gen.ts +0 -1258
- package/src/utils/stockpile_move_gen.ts +0 -325
- package/tsconfig.json +0 -75
package/README.md
CHANGED
|
@@ -1,38 +1,51 @@
|
|
|
1
1
|
# gungi.js
|
|
2
2
|
|
|
3
|
-
](https://github.com/jwyce/gungi.js/actions)
|
|
4
|
+
[](https://www.npmjs.com/package/gungi.js)
|
|
5
|
+
[](https://www.npmjs.com/package/gungi.js)
|
|
5
6
|
|
|
6
|
-
gungi.js is a
|
|
7
|
+
gungi.js is a TypeScript gungi library for the strategy game from the HUNTER×HUNTER manga. It implements the "official rules" trying to be as faithful as possible to what is shown in HUNTER×HUNTER.
|
|
8
|
+
Credit to these two reddit posts from [u/magickirua](https://www.reddit.com/r/HunterXHunter/comments/uqrtct/gungi_the_official_rules) and [u/MythicalTenshi](https://www.reddit.com/r/HunterXHunter/comments/105f43g/comment/j3evkkq)
|
|
9
|
+
which translate the rule book.
|
|
7
10
|
|
|
8
|
-
|
|
11
|
+
gungi.js is used for move generation/validation, piece placement/movement, and endgame detection - basically everything but the AI.
|
|
9
12
|
|
|
10
13
|
## Installation
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
Run the following command to install the most recent version of gungi.js from NPM:
|
|
13
16
|
|
|
14
17
|
```
|
|
15
|
-
# NPM
|
|
16
18
|
npm install gungi.js
|
|
17
|
-
|
|
18
|
-
# Yarn
|
|
19
|
-
yarn add gungi.js
|
|
20
19
|
```
|
|
21
20
|
|
|
22
21
|
## Example Code
|
|
23
22
|
|
|
24
23
|
The code below plays a random game of gungi:
|
|
25
24
|
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
```ts
|
|
26
|
+
import { ADVANCED_POSITION, Gungi } from './gungi';
|
|
27
|
+
|
|
28
|
+
function clearTerminal() {
|
|
29
|
+
process.stdout.write('\x1b[2J');
|
|
30
|
+
process.stdout.write('\x1b[H');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function printText(text: string) {
|
|
34
|
+
clearTerminal();
|
|
35
|
+
process.stdout.write(text);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const gungi = new Gungi(ADVANCED_POSITION);
|
|
29
39
|
|
|
30
|
-
while (!gungi.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
40
|
+
while (!gungi.isGameOver()) {
|
|
41
|
+
const moves = gungi.moves();
|
|
42
|
+
const move = moves[Math.floor(Math.random() * moves.length)];
|
|
43
|
+
gungi.move(move);
|
|
44
|
+
printText(gungi.ascii());
|
|
34
45
|
}
|
|
35
|
-
|
|
46
|
+
|
|
47
|
+
console.log(gungi.fen() + '\n');
|
|
48
|
+
console.log(gungi.pgn());
|
|
36
49
|
```
|
|
37
50
|
|
|
38
51
|
## API
|
|
@@ -40,371 +53,726 @@ console.log(gungi.ascii())
|
|
|
40
53
|
### Constants
|
|
41
54
|
|
|
42
55
|
```js
|
|
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
|
-
|
|
56
|
+
// colors
|
|
57
|
+
const WHITE = 'w';
|
|
58
|
+
const BLACK = 'b';
|
|
59
|
+
|
|
60
|
+
// special moves
|
|
61
|
+
export const TSUKE = '付';
|
|
62
|
+
export const TAKE = '取';
|
|
63
|
+
export const BETRAY = '返';
|
|
64
|
+
export const ARATA = '新';
|
|
65
|
+
|
|
66
|
+
// pieces
|
|
67
|
+
export const MARSHAL = '帥';
|
|
68
|
+
export const GENERAL = '大';
|
|
69
|
+
export const LIEUTENANT_GENERAL = '中';
|
|
70
|
+
export const MAJOR_GENERAL = '小';
|
|
71
|
+
export const WARRIOR = '侍';
|
|
72
|
+
export const LANCER = '槍';
|
|
73
|
+
export const RIDER = '馬';
|
|
74
|
+
export const SPY = '忍';
|
|
75
|
+
export const FORTRESS = '砦';
|
|
76
|
+
export const SOLDIER = '兵';
|
|
77
|
+
export const CANNON = '砲';
|
|
78
|
+
export const ARCHER = '弓';
|
|
79
|
+
export const MUSKETEER = '筒';
|
|
80
|
+
export const TACTICIAN = '謀';
|
|
81
|
+
|
|
82
|
+
// starting positions (in FEN)
|
|
83
|
+
export const INTRO_POSITION =
|
|
84
|
+
'3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1';
|
|
85
|
+
export const BEGINNNER_POSITION =
|
|
86
|
+
'3img3/1ra1n1as1/d1fwdwf1d/9/9/9/D1FWDWF1D/1SA1N1AR1/3GMI3 J2N2S1R1D1/j2n2s1r1d1 w 1 - 1';
|
|
87
|
+
export const INTERMEDIATE_POSITION =
|
|
88
|
+
'9/9/9/9/9/9/9/9/9 M1G1I1J2W2N3R2S2F2D4C1A2K1T1/m1g1i1j2w2n3r2s2f2d4c1a2k1t1 w 2 wb 1';
|
|
89
|
+
export const ADVANCED_POSITION =
|
|
90
|
+
'9/9/9/9/9/9/9/9/9 M1G1I1J2W2N3R2S2F2D4C1A2K1T1/m1g1i1j2w2n3r2s2f2d4c1a2k1t1 w 3 wb 1';
|
|
91
|
+
|
|
92
|
+
// square list
|
|
93
|
+
export const SQUARES = ['1-1', '1-2', '1-3', ..., '7-1', '8-1', '9-1']
|
|
94
|
+
|
|
95
|
+
// map of pieces to japanese names (romaji)
|
|
96
|
+
export const CANONICAL_NAMES = { 帥: 'sui', ..., 弓: 'yumi', 筒: 'tsutsu', 謀: 'boushou' }
|
|
97
|
+
|
|
98
|
+
// map of pieces to english names
|
|
99
|
+
export const ENGLISH_NAMES = { 帥: 'marshal', ..., 弓: 'archer', 筒: 'cannon', 謀: 'tactician' }
|
|
100
|
+
|
|
101
|
+
// map of pieces to fen code symbols
|
|
102
|
+
export const FEN_CODES = { 帥: 'm', ..., 弓: 'a', 筒: 'c', 謀: 't' }
|
|
68
103
|
```
|
|
69
104
|
|
|
70
|
-
###
|
|
105
|
+
### Constructor: Gungi([ fen ])
|
|
71
106
|
|
|
72
|
-
|
|
107
|
+
The Gungi() constructor takes an optional parameter which specifies the board configuration in a special variant of [Forsyth-Edwards Notation (FEN)](https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation).
|
|
108
|
+
Throws an exception if an invalid FEN string is provided. See specification differences [here](src/gungi/fen.ts).
|
|
73
109
|
|
|
74
|
-
|
|
110
|
+
```ts
|
|
111
|
+
import { Gungi } from 'gungi.js'
|
|
75
112
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
113
|
+
// an empty constructor defaults the starting position
|
|
114
|
+
let gungi = new Gungi()
|
|
115
|
+
|
|
116
|
+
// pass in a FEN string to load a particular position
|
|
117
|
+
let gungi = new Gungi(
|
|
118
|
+
'd1f1|r:d||r:j|j|k:a:c||g:s|/3|w:s|2|n:w|2/f8/3n2d1m/2i4W1/3dN1tR1/4D2R1/ASJ2|C:a|1A1/|I:F:W|F1SGMT1|K:J| -/- w 3 - 57',
|
|
119
|
+
)
|
|
83
120
|
|
|
84
|
-
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### .ascii([ options ])
|
|
124
|
+
|
|
125
|
+
Returns a string containing an ASCII diagram of the current position with ANSI colors. Options is an optional parameter which may contain a 'english' flag to display the pieces in English symbols.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const gungi = new Gungi(BEGINNNER_POSITION);
|
|
85
129
|
|
|
86
130
|
// make some moves
|
|
87
|
-
gungi.move(
|
|
88
|
-
gungi.move(
|
|
89
|
-
gungi.move(
|
|
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
|
-
//
|
|
131
|
+
gungi.move('槍(8-5-1)(7-5-2)付');
|
|
132
|
+
gungi.move('新馬(1-7-1)');
|
|
133
|
+
gungi.move('新忍(8-7-2)付');
|
|
134
|
+
|
|
135
|
+
gungi.ascii();
|
|
136
|
+
// -> ' 987654321
|
|
137
|
+
// +ーーーーーーーーー+
|
|
138
|
+
// |・・馬中帥大・・・|1
|
|
139
|
+
// |・馬弓・槍・弓忍・|2
|
|
140
|
+
// |兵・砦侍兵侍砦・兵|3
|
|
141
|
+
// |・・・・・・・・・|4
|
|
142
|
+
// |・・・・・・・・・|5
|
|
143
|
+
// |・・・・・・・・・|6
|
|
144
|
+
// |兵・砦侍槍侍砦・兵|7
|
|
145
|
+
// |・忍忍・・・弓馬・|8
|
|
146
|
+
// |・・・大帥中・・・|9
|
|
147
|
+
// +ーーーーーーーーー+'
|
|
148
|
+
gungi.ascii({ english: true });
|
|
149
|
+
// -> ' 987654321
|
|
150
|
+
// +ーーーーーーーーー+
|
|
151
|
+
// |・・rimg・・・|1
|
|
152
|
+
// |・ra・n・as・|2
|
|
153
|
+
// |d・fwdwf・d|3
|
|
154
|
+
// |・・・・・・・・・|4
|
|
155
|
+
// |・・・・・・・・・|5
|
|
156
|
+
// |・・・・・・・・・|6
|
|
157
|
+
// |D・FWNWF・D|7
|
|
158
|
+
// |・SS・・・AR・|8
|
|
159
|
+
// |・・・GMI・・・|9
|
|
160
|
+
// +ーーーーーーーーー+'
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### .board()
|
|
164
|
+
|
|
165
|
+
Returns a 3D tensor representation of the current position (9x9x3 array). Empty squares are represented by `null`.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
const gungi = new Gungi();
|
|
169
|
+
|
|
170
|
+
gungi.board();
|
|
171
|
+
// -> [[[null],
|
|
172
|
+
// [null],
|
|
173
|
+
// [null],
|
|
174
|
+
// [{ square: '1-6', tier: 1, type: '中', color: 'b' }],
|
|
175
|
+
// [{ square: '1-5', tier: 1, type: '帥', color: 'b' }],
|
|
176
|
+
// [{ square: '1-4', tier: 1, type: '大', color: 'b' }],
|
|
177
|
+
// [null],
|
|
178
|
+
// [null],
|
|
179
|
+
// [null]],
|
|
180
|
+
// [[null],
|
|
181
|
+
// [{ square: '2-8', tier: 1, type: '忍', color: 'b' }],
|
|
182
|
+
// [null],
|
|
183
|
+
// [null],
|
|
184
|
+
// [{ square: '2-5', tier: 1, type: '槍', color: 'b' }],
|
|
185
|
+
// [null],
|
|
186
|
+
// [null],
|
|
187
|
+
// [{ square: '2-2', tier: 1, type: '忍', color: 'b' }],
|
|
188
|
+
// [null]],
|
|
189
|
+
// [[{ square: '3-9', tier: 1, type: '兵', color: 'b' }],
|
|
190
|
+
// [null],
|
|
191
|
+
// [{ square: '3-7', tier: 1, type: '砦', color: 'b' }],
|
|
192
|
+
// [{ square: '3-6', tier: 1, type: '侍', color: 'b' }],
|
|
193
|
+
// [{ square: '3-5', tier: 1, type: '兵', color: 'b' }],
|
|
194
|
+
// [{ square: '3-4', tier: 1, type: '侍', color: 'b' }],
|
|
195
|
+
// [{ square: '3-3', tier: 1, type: '砦', color: 'b' }],
|
|
196
|
+
// [null],
|
|
197
|
+
// [{ square: '3-1', tier: 1, type: '兵', color: 'b' }]],
|
|
198
|
+
// [...],
|
|
199
|
+
// [...],
|
|
200
|
+
// [...],
|
|
201
|
+
// [[{ square: '7-9', tier: 1, type: '兵', color: 'w' }],
|
|
202
|
+
// [null],
|
|
203
|
+
// [{ square: '7-7', tier: 1, type: '砦', color: 'w' }],
|
|
204
|
+
// [{ square: '7-6', tier: 1, type: '侍', color: 'w' }],
|
|
205
|
+
// [{ square: '7-5', tier: 1, type: '兵', color: 'w' }],
|
|
206
|
+
// [{ square: '7-4', tier: 1, type: '侍', color: 'w' }],
|
|
207
|
+
// [{ square: '7-3', tier: 1, type: '砦', color: 'w' }],
|
|
208
|
+
// [null],
|
|
209
|
+
// [{ square: '7-1', tier: 1, type: '兵', color: 'w' }]],
|
|
210
|
+
// [[null],
|
|
211
|
+
// [{ square: '8-8', tier: 1, type: '忍', color: 'w' }],
|
|
212
|
+
// [null],
|
|
213
|
+
// [null],
|
|
214
|
+
// [{ square: '8-5', tier: 1, type: '槍', color: 'w' }],
|
|
215
|
+
// [null],
|
|
216
|
+
// [null],
|
|
217
|
+
// [{ square: '8-2', tier: 1, type: '忍', color: 'w' }],
|
|
218
|
+
// [null]],
|
|
219
|
+
// [[null],
|
|
220
|
+
// [null],
|
|
221
|
+
// [null],
|
|
222
|
+
// [{ square: '9-6', tier: 1, type: '大', color: 'w' }],
|
|
223
|
+
// [{ square: '9-5', tier: 1, type: '帥', color: 'w' }],
|
|
224
|
+
// [{ square: '9-4', tier: 1, type: '中', color: 'w' }],
|
|
225
|
+
// [null],
|
|
226
|
+
// [null],
|
|
227
|
+
// [null]]];
|
|
115
228
|
```
|
|
116
229
|
|
|
117
|
-
### .
|
|
230
|
+
### .captured(color)
|
|
118
231
|
|
|
119
|
-
Returns a
|
|
232
|
+
Returns a list of pieces captured. Color is an optional parameter to filter pieces by player color
|
|
120
233
|
|
|
121
|
-
```
|
|
122
|
-
const gungi = new Gungi()
|
|
123
|
-
gungi.move(
|
|
124
|
-
gungi.move(
|
|
234
|
+
```ts
|
|
235
|
+
const gungi = new Gungi(BEGINNNER_POSITION);
|
|
236
|
+
gungi.move('新小(7-5-2)付');
|
|
237
|
+
gungi.move('兵(3-5-1)(4-5-1)');
|
|
238
|
+
gungi.move('新槍(7-4-2)付');
|
|
239
|
+
gungi.move('兵(4-5-1)(5-5-1)');
|
|
240
|
+
gungi.move('小(7-5-2)取(5-5-1)');
|
|
125
241
|
|
|
126
|
-
gungi.
|
|
127
|
-
// -> [
|
|
128
|
-
[...],
|
|
129
|
-
[...],
|
|
130
|
-
[
|
|
131
|
-
[ null, null, null ],
|
|
132
|
-
[ {type: '帥', color: 'b'}, null, null ],
|
|
133
|
-
[ null, null, null ],
|
|
134
|
-
[ null, null, null ],
|
|
135
|
-
[ null, null, null ],
|
|
136
|
-
[ null, null, null ],
|
|
137
|
-
[ null, null, null ],
|
|
138
|
-
[ null, null, null ],
|
|
139
|
-
[ null, null, null ]
|
|
140
|
-
],
|
|
141
|
-
[...],
|
|
142
|
-
[...],
|
|
143
|
-
[...],
|
|
144
|
-
[...],
|
|
145
|
-
[
|
|
146
|
-
[ null, null, null ],
|
|
147
|
-
[ null, null, null ],
|
|
148
|
-
[ null, null, null ],
|
|
149
|
-
[ null, null, null ],
|
|
150
|
-
[ {type: '帥', color: 'w'}, null, null ],
|
|
151
|
-
[ null, null, null ],
|
|
152
|
-
[ null, null, null ],
|
|
153
|
-
[ null, null, null ],
|
|
154
|
-
[ null, null, null ]
|
|
155
|
-
],
|
|
156
|
-
[...]
|
|
157
|
-
]
|
|
242
|
+
gungi.captured('b');
|
|
243
|
+
// -> [{ type: '兵', color: 'b', count: 1 }]
|
|
158
244
|
```
|
|
159
245
|
|
|
160
|
-
### .
|
|
246
|
+
### .clear()
|
|
161
247
|
|
|
162
|
-
|
|
248
|
+
Clears the board.
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
gungi.clear();
|
|
252
|
+
gungi.fen();
|
|
253
|
+
// -> '9/9/9/9/9/9/9/9/9 M1G1I1J2W2N3R2S2F2D4C1A2K1T1/m1g1i1j2w2n3r2s2f2d4c1a2k1t1 w 3 wb 1' <- empty board
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### .fen()
|
|
257
|
+
|
|
258
|
+
Returns the FEN string for the current position.
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
const gungi = new Gungi(BEGINNNER_POSITION);
|
|
262
|
+
|
|
263
|
+
// make some moves
|
|
264
|
+
gungi.move('槍(8-5-1)(7-5-2)付');
|
|
265
|
+
gungi.move('新馬(1-7-1)');
|
|
266
|
+
gungi.move('新忍(8-7-2)付');
|
|
267
|
+
|
|
268
|
+
gungi.fen();
|
|
269
|
+
// -> '2rimg3/1ra1n1as1/d1fwdwf1d/9/9/9/D1FW|D:N|WF1D/1S|A:S|3AR1/3GMI3 J2N2R1D1/j2n2s1d1 b 1 - 2'
|
|
270
|
+
```
|
|
163
271
|
|
|
164
272
|
### .get(square)
|
|
165
273
|
|
|
166
|
-
Returns the tower of pieces on the square
|
|
274
|
+
Returns the tower of pieces on the square. Returns `undefined` if the square is empty.
|
|
167
275
|
|
|
168
|
-
```
|
|
169
|
-
const gungi = new Gungi()
|
|
276
|
+
```ts
|
|
277
|
+
const gungi = new Gungi(BEGINNNER_POSITION);
|
|
170
278
|
|
|
171
|
-
gungi.move(
|
|
172
|
-
gungi.move({src: {type: gungi.MARSHAL, color: gungi.WHITE}, dst: '2-5', type: gungi.PLACE})
|
|
173
|
-
gungi.move({src: {type: gungi.PAWN, color: gungi.BLACK}, dst: '8-5', type: gungi.PLACE})
|
|
174
|
-
gungi.move({src: {type: gungi.PAWN, color: gungi.WHITE}, dst: '1-5', type: gungi.PLACE})
|
|
175
|
-
gungi.move({src: {type: gungi.ARCHER, color: gungi.BLACK}, dst: '8-5', type: gungi.PLACE})
|
|
176
|
-
gungi.move({src: {type: gungi.SPY, color: gungi.WHITE}, dst: '1-5', type: gungi.PLACE})
|
|
279
|
+
gungi.move('砦(7-3-1)(7-4-2)付');
|
|
177
280
|
|
|
178
|
-
gungi.get('
|
|
179
|
-
// -> [
|
|
281
|
+
gungi.get('7-4');
|
|
282
|
+
// -> [{ square: '7-4', tier: 1, type: '侍', color: 'w' }, { square: '7-4', tier: 2, type: '砦', color: 'w' }]
|
|
283
|
+
gungi.get('7-3');
|
|
284
|
+
// -> undefined
|
|
180
285
|
```
|
|
181
286
|
|
|
182
|
-
### .
|
|
287
|
+
### .getDraftingRights(color)
|
|
183
288
|
|
|
184
|
-
|
|
289
|
+
Gets the drafting rights for the given color. Boolean value is return if color provided, otherwise returns an object with both colors.
|
|
185
290
|
|
|
186
|
-
```
|
|
187
|
-
const gungi = new Gungi()
|
|
291
|
+
```ts
|
|
292
|
+
const gungi = new Gungi(ADVANCED_POSITION);
|
|
188
293
|
|
|
189
|
-
gungi.move(
|
|
190
|
-
gungi.move({src: {type: gungi.MARSHAL, color: gungi.WHITE}, dst: '2-5', type: gungi.PLACE})
|
|
191
|
-
gungi.move({src: {type: gungi.PAWN, color: gungi.BLACK}, dst: '8-5', type: gungi.PLACE})
|
|
192
|
-
gungi.move({src: {type: gungi.PAWN, color: gungi.WHITE}, dst: '1-5', type: gungi.PLACE})
|
|
193
|
-
gungi.move({src: {type: gungi.ARCHER, color: gungi.BLACK}, dst: '8-5', type: gungi.PLACE})
|
|
194
|
-
gungi.move({src: {type: gungi.SPY, color: gungi.WHITE}, dst: '1-5', type: gungi.PLACE})
|
|
294
|
+
gungi.move('新帥(7-2-1)終');
|
|
195
295
|
|
|
196
|
-
gungi.
|
|
197
|
-
// -> {
|
|
296
|
+
gungi.getDraftingRights();
|
|
297
|
+
// -> { w: false, b: true }
|
|
298
|
+
gungi.getDraftingRights('b');
|
|
299
|
+
// -> true
|
|
198
300
|
```
|
|
199
301
|
|
|
200
|
-
### .
|
|
302
|
+
### .getTop(square)
|
|
201
303
|
|
|
202
|
-
Returns
|
|
304
|
+
Returns the top piece of the tower on the square. Returns `undefined` if the square is empty.
|
|
203
305
|
|
|
204
|
-
|
|
306
|
+
```ts
|
|
307
|
+
const gungi = new Gungi(BEGINNNER_POSITION);
|
|
205
308
|
|
|
206
|
-
|
|
309
|
+
gungi.get('7-4');
|
|
310
|
+
// -> [{ square: '7-4', tier: 1, type: '侍', color: 'w' }]
|
|
311
|
+
gungi.get('7-3');
|
|
312
|
+
// -> undefined
|
|
313
|
+
```
|
|
207
314
|
|
|
208
|
-
|
|
209
|
-
const gungi = new Gungi()
|
|
315
|
+
### .hand(color)
|
|
210
316
|
|
|
211
|
-
|
|
212
|
-
gungi.move({src: {type: gungi.MARSHAL, color: gungi.WHITE}, dst: '2-5', type: gungi.PLACE})
|
|
213
|
-
gungi.move({src: {type: gungi.PAWN, color: gungi.BLACK}, dst: '8-5', type: gungi.PLACE})
|
|
214
|
-
gungi.move({src: {type: gungi.PAWN, color: gungi.WHITE}, dst: '1-5', type: gungi.PLACE})
|
|
215
|
-
gungi.move({src: {type: gungi.CAPTAIN, color: gungi.BLACK}, dst: '8-5', type: gungi.PLACE})
|
|
216
|
-
gungi.move({src: {type: gungi.ARCHER, color: gungi.WHITE}, dst: '1-5', type: gungi.PLACE})
|
|
217
|
-
gungi.move({src: {type: gungi.MUSKETEER, color: gungi.BLACK}, dst: '8-5', type: gungi.PLACE})
|
|
317
|
+
Returns a list of pieces in players' hand. Color is an optional parameter to filter pieces by player color
|
|
218
318
|
|
|
219
|
-
|
|
220
|
-
|
|
319
|
+
```ts
|
|
320
|
+
const gungi = new Gungi(BEGINNNER_POSITION);
|
|
321
|
+
|
|
322
|
+
// make some moves
|
|
323
|
+
gungi.move('槍(8-5-1)(7-5-2)付');
|
|
324
|
+
gungi.move('新馬(1-7-1)');
|
|
325
|
+
gungi.move('新忍(8-7-2)付');
|
|
326
|
+
|
|
327
|
+
gungi.hand();
|
|
328
|
+
// -> [
|
|
329
|
+
// { type: '小', count: 2, color: 'w' },
|
|
330
|
+
// { type: '槍', count: 2, color: 'w' },
|
|
331
|
+
// { type: '馬', count: 1, color: 'w' },
|
|
332
|
+
// { type: '兵', count: 1, color: 'w' },
|
|
333
|
+
// { type: '小', count: 2, color: 'b' },
|
|
334
|
+
// { type: '槍', count: 2, color: 'b' },
|
|
335
|
+
// { type: '忍', count: 1, color: 'b' },
|
|
336
|
+
// { type: '兵', count: 1, color: 'b' }
|
|
337
|
+
// ]
|
|
338
|
+
gungi.hand('w');
|
|
339
|
+
// [
|
|
340
|
+
// { type: '小', count: 2, color: 'w' },
|
|
341
|
+
// { type: '槍', count: 2, color: 'w' },
|
|
342
|
+
// { type: '馬', count: 1, color: 'w' },
|
|
343
|
+
// { type: '兵', count: 1, color: 'w' }
|
|
344
|
+
// ]
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### .history([ options ])
|
|
348
|
+
|
|
349
|
+
Returns a list containing the moves of the current game. Options is an optional parameter which may contain a 'verbose' flag. See .moves() for a description of the verbose move fields.
|
|
350
|
+
A FEN string of the position prior to the move being made is added to the verbose history output.
|
|
351
|
+
|
|
352
|
+
```ts
|
|
353
|
+
const gungi = new Gungi(BEGINNNER_POSITION);
|
|
354
|
+
gungi.move('新小(7-5-2)付');
|
|
355
|
+
gungi.move('兵(3-5-1)(4-5-1)');
|
|
356
|
+
gungi.move('新槍(7-4-2)付');
|
|
357
|
+
gungi.move('兵(4-5-1)(5-5-1)');
|
|
358
|
+
gungi.move('小(7-5-2)取(5-5-1)');
|
|
359
|
+
|
|
360
|
+
gungi.history();
|
|
361
|
+
// -> ['砦(7-3-1)(7-4-2)付', '新小(7-5-2)付', '兵(3-5-1)(4-5-1)', '新槍(7-4-2)付', '兵(4-5-1)(5-5-1)', '小(7-5-2)取(5-5-1)']
|
|
362
|
+
|
|
363
|
+
gungi.history({ verbose: true });
|
|
364
|
+
// -->
|
|
365
|
+
// [
|
|
366
|
+
// {
|
|
367
|
+
// piece: '砦',
|
|
368
|
+
// color: 'w',
|
|
369
|
+
// from: '7-3-1',
|
|
370
|
+
// to: '7-4-2',
|
|
371
|
+
// type: 'tsuke',
|
|
372
|
+
// san: '砦(7-3-1)(7-4-2)付',
|
|
373
|
+
// before: '3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1',
|
|
374
|
+
// after: '3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWD|W:F|2D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 b 0 - 1',
|
|
375
|
+
// },
|
|
376
|
+
// {
|
|
377
|
+
// piece: '小',
|
|
378
|
+
// color: 'w',
|
|
379
|
+
// from: '',
|
|
380
|
+
// to: '7-5-2',
|
|
381
|
+
// type: 'arata',
|
|
382
|
+
// san: '新小(7-5-2)付',
|
|
383
|
+
// before: '3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1',
|
|
384
|
+
// after: '3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FW|D:J|WF1D/1S2N2S1/3GMI3 J1N2R2D1/j2n2r2d1 b 0 - 1',
|
|
385
|
+
// },
|
|
386
|
+
// {
|
|
387
|
+
// piece: '兵',
|
|
388
|
+
// color: 'b',
|
|
389
|
+
// from: '3-5-1',
|
|
390
|
+
// to: '4-5-1',
|
|
391
|
+
// type: 'route',
|
|
392
|
+
// san: '兵(3-5-1)(4-5-1)',
|
|
393
|
+
// before: '3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FW|D:J|WF1D/1S2N2S1/3GMI3 J1N2R2D1/j2n2r2d1 b 0 - 1',
|
|
394
|
+
// after: '3img3/1s2n2s1/d1fw1wf1d/4d4/9/9/D1FW|D:J|WF1D/1S2N2S1/3GMI3 J1N2R2D1/j2n2r2d1 w 0 - 2',
|
|
395
|
+
// },
|
|
396
|
+
// {
|
|
397
|
+
// piece: '槍',
|
|
398
|
+
// color: 'w',
|
|
399
|
+
// from: '',
|
|
400
|
+
// to: '7-4-2',
|
|
401
|
+
// type: 'arata',
|
|
402
|
+
// san: '新槍(7-4-2)付',
|
|
403
|
+
// before: '3img3/1s2n2s1/d1fw1wf1d/4d4/9/9/D1FW|D:J|WF1D/1S2N2S1/3GMI3 J1N2R2D1/j2n2r2d1 w 0 - 2',
|
|
404
|
+
// after: '3img3/1s2n2s1/d1fw1wf1d/4d4/9/9/D1FW|D:J||W:N|F1D/1S2N2S1/3GMI3 J1N1R2D1/j2n2r2d1 b 0 - 2',
|
|
405
|
+
// },
|
|
406
|
+
// {
|
|
407
|
+
// piece: '兵',
|
|
408
|
+
// color: 'b',
|
|
409
|
+
// from: '4-5-1',
|
|
410
|
+
// to: '5-5-1',
|
|
411
|
+
// type: 'route',
|
|
412
|
+
// san: '兵(4-5-1)(5-5-1)',
|
|
413
|
+
// before: '3img3/1s2n2s1/d1fw1wf1d/4d4/9/9/D1FW|D:J||W:N|F1D/1S2N2S1/3GMI3 J1N1R2D1/j2n2r2d1 b 0 - 2',
|
|
414
|
+
// after: '3img3/1s2n2s1/d1fw1wf1d/9/4d4/9/D1FW|D:J||W:N|F1D/1S2N2S1/3GMI3 J1N1R2D1/j2n2r2d1 w 0 - 3',
|
|
415
|
+
// },
|
|
416
|
+
// {
|
|
417
|
+
// piece: '小',
|
|
418
|
+
// color: 'w',
|
|
419
|
+
// from: '7-5-2',
|
|
420
|
+
// to: '5-5-1',
|
|
421
|
+
// type: 'capture',
|
|
422
|
+
// san: '小(7-5-2)取(5-5-1)',
|
|
423
|
+
// before: '3img3/1s2n2s1/d1fw1wf1d/9/4d4/9/D1FW|D:J||W:N|F1D/1S2N2S1/3GMI3 J1N1R2D1/j2n2r2d1 w 0 - 3',
|
|
424
|
+
// after: '3img3/1s2n2s1/d1fw1wf1d/9/4J4/9/D1FWD|W:N|F1D/1S2N2S1/3GMI3 J1N1R2D1/j2n2r2d1 b 0 - 3',
|
|
425
|
+
// captured: [ { square: '5-5', tier: 1, type: '兵', color: 'b' } ]
|
|
426
|
+
// }
|
|
427
|
+
// ]
|
|
221
428
|
```
|
|
222
429
|
|
|
223
|
-
### .
|
|
430
|
+
### .inDraft()
|
|
224
431
|
|
|
225
|
-
Returns true or false if
|
|
432
|
+
Returns true or false if either player is still in the drafting phase.
|
|
226
433
|
|
|
227
|
-
|
|
434
|
+
```ts
|
|
435
|
+
const gungi = new Gungi(ADVANCED_POSITION);
|
|
228
436
|
|
|
229
|
-
|
|
437
|
+
gungi.move('新帥(7-2-1)終');
|
|
438
|
+
gungi.inDraft();
|
|
439
|
+
// -> true
|
|
230
440
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
441
|
+
gungi.move('新帥(2-4-1)終');
|
|
442
|
+
gungi.inDraft();
|
|
443
|
+
// -> false
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
### .isFourfoldRepetition()
|
|
447
|
+
|
|
448
|
+
Returns true or false if the current board position has occurred four or more times.
|
|
449
|
+
|
|
450
|
+
```ts
|
|
451
|
+
const gungi = new Gungi(INTRO_POSITION);
|
|
452
|
+
// 3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1 occurs 1st time
|
|
453
|
+
gungi.isFourfoldRepetition();
|
|
454
|
+
// -> false
|
|
455
|
+
|
|
456
|
+
gungi.move('兵(7-1-1)(6-1-1)');
|
|
457
|
+
gungi.move('侍(3-4-1)(4-4-1)');
|
|
458
|
+
gungi.move('兵(6-1-1)(7-1-1)');
|
|
459
|
+
gungi.move('侍(4-4-1)(3-4-1)');
|
|
460
|
+
// 3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1 occurs 2nd time
|
|
461
|
+
gungi.isFourfoldRepetition();
|
|
462
|
+
// -> false
|
|
463
|
+
|
|
464
|
+
gungi.move('兵(7-1-1)(6-1-1)');
|
|
465
|
+
gungi.move('侍(3-4-1)(4-4-1)');
|
|
466
|
+
gungi.move('兵(6-1-1)(7-1-1)');
|
|
467
|
+
gungi.move('侍(4-4-1)(3-4-1)');
|
|
468
|
+
// 3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1 occurs 3rd time
|
|
469
|
+
gungi.isFourfoldRepetition();
|
|
470
|
+
// -> false
|
|
471
|
+
|
|
472
|
+
gungi.move('兵(7-1-1)(6-1-1)');
|
|
473
|
+
gungi.move('侍(3-4-1)(4-4-1)');
|
|
474
|
+
gungi.move('兵(6-1-1)(7-1-1)');
|
|
475
|
+
gungi.move('侍(4-4-1)(3-4-1)');
|
|
476
|
+
// 3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1 occurs 4th time
|
|
477
|
+
gungi.isFourfoldRepetition();
|
|
478
|
+
// -> true
|
|
479
|
+
```
|
|
234
480
|
|
|
235
|
-
|
|
481
|
+
### .isGameOver()
|
|
236
482
|
|
|
237
|
-
|
|
483
|
+
Returns true if the game has ended via either marshal catpured or fourfold repetition. Otherwise, returns false.
|
|
238
484
|
|
|
239
|
-
|
|
485
|
+
```ts
|
|
486
|
+
const gungi = new Gungi(ADVANCED_POSITION);
|
|
487
|
+
gungi.isGameOver();
|
|
488
|
+
// -> false
|
|
240
489
|
|
|
241
|
-
|
|
490
|
+
// marshal captured
|
|
491
|
+
gungi.load(
|
|
492
|
+
'1|g:N|2|W:N|Ad1f/7r1/1nd2Adfr/2|c:G|j2K2/6s1D/1w|W:T|6/2F4J|F:D|/i8/2|S:w||R:M|3C1 -/- b 3 - 164'
|
|
493
|
+
);
|
|
494
|
+
gungi.isGameOver();
|
|
495
|
+
// -> true
|
|
242
496
|
```
|
|
243
497
|
|
|
244
|
-
### .
|
|
498
|
+
### .load(fen)
|
|
245
499
|
|
|
246
|
-
|
|
500
|
+
Clears the board and loads the provided FEN string. Throws an exception if the FEN is invalid.
|
|
247
501
|
|
|
248
|
-
```
|
|
249
|
-
const gungi = new Gungi()
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
gungi.move({src: {type: gungi.MARSHAL, color: gungi.WHITE}, dst: '2-5', type: gungi.PLACE})
|
|
254
|
-
gungi.move({src: null, dst: null, type: gungi.READY})
|
|
255
|
-
gungi.move({src: {type: gungi.PAWN, color: gungi.WHITE}, dst: '1-5', type: gungi.PLACE})
|
|
256
|
-
gungi.move({src: {type: gungi.PAWN, color: gungi.WHITE}, dst: '1-6', type: gungi.PLACE})
|
|
257
|
-
gungi.move({src: {type: gungi.PAWN, color: gungi.WHITE}, dst: '1-7', type: gungi.PLACE})
|
|
258
|
-
gungi.move({src: {type: gungi.PAWN, color: gungi.WHITE}, dst: '1-8', type: gungi.PLACE})
|
|
259
|
-
gungi.move({src: null, dst: null, type: gungi.READY})
|
|
260
|
-
|
|
261
|
-
gungi.moves()
|
|
262
|
-
// ->[
|
|
263
|
-
// { src: '1-6', dst: '2-6', type: 'move' },
|
|
264
|
-
// { src: '1-7', dst: '2-7', type: 'move' },
|
|
265
|
-
// { src: '1-8', dst: '2-8', type: 'move' },
|
|
266
|
-
// { src: '2-5', dst: '1-4', type: 'move' },
|
|
267
|
-
// { src: '2-5', dst: '1-5', type: 'stack' },
|
|
268
|
-
// { src: '2-5', dst: '1-6', type: 'stack' },
|
|
269
|
-
// { src: '2-5', dst: '2-4', type: 'move' },
|
|
270
|
-
// { src: '2-5', dst: '2-6', type: 'move' },
|
|
271
|
-
// { src: '2-5', dst: '3-4', type: 'move' },
|
|
272
|
-
// { src: '2-5', dst: '3-5', type: 'move' },
|
|
273
|
-
// { src: '2-5', dst: '3-6', type: 'move' },
|
|
274
|
-
// { src: { type: '兵', color: 'w' }, dst: '1-1', type: 'place' },
|
|
275
|
-
// { src: { type: '兵', color: 'w' }, dst: '1-2', type: 'place' },
|
|
276
|
-
// { src: { type: '兵', color: 'w' }, dst: '1-3', type: 'place' },
|
|
277
|
-
// ... 605 more items
|
|
278
|
-
|
|
279
|
-
gungi.moves({square: '2-5'}) // single square move generation
|
|
280
|
-
// -> [
|
|
281
|
-
// { src: '2-5', dst: '1-4', type: 'move' },
|
|
282
|
-
// { src: '2-5', dst: '1-5', type: 'stack' },
|
|
283
|
-
// { src: '2-5', dst: '1-6', type: 'stack' },
|
|
284
|
-
// { src: '2-5', dst: '2-4', type: 'move' },
|
|
285
|
-
// { src: '2-5', dst: '2-6', type: 'move' },
|
|
286
|
-
// { src: '2-5', dst: '3-4', type: 'move' },
|
|
287
|
-
// { src: '2-5', dst: '3-5', type: 'move' },
|
|
288
|
-
// { src: '2-5', dst: '3-6', type: 'move' }
|
|
289
|
-
// ]
|
|
502
|
+
```ts
|
|
503
|
+
const gungi = new Gungi();
|
|
504
|
+
gungi.load(
|
|
505
|
+
'd1f1|r:d||r:j|j|k:a:c||g:s|/3|w:s|2|n:w|2/f8/3n2d1m/2i4W1/3dN1tR1/4D2R1/ASJ2|C:a|1A1/|I:F:W|F1SGMT1|K:J| -/- w 3 - 57'
|
|
506
|
+
);
|
|
290
507
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
508
|
+
try {
|
|
509
|
+
gungi.load('3img3/1ra1n1xas1/d1fwdwf1d/9/9/9/9/9 J2N2R1D1/j2n2r2d1 w 1 - 1');
|
|
510
|
+
} catch (e) {
|
|
511
|
+
console.log(e.message);
|
|
512
|
+
}
|
|
513
|
+
// -> Error: Invalid FEN: 1st field (piece positions) is invalid [expected 9 ranks, received 8]
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
### .loadPgn(pgn, fen, [ options ])
|
|
517
|
+
|
|
518
|
+
Load the moves of a game stored in [Portable Game Notation](http://en.wikipedia.org/wiki/Portable_Game_Notation). `pgn` should be a string. `fen` should be the starting position of the game (defaults to `ADVANCED_POSITION`).
|
|
519
|
+
Options is an optional object which may contain a string `newline`. PGN for gungi is slightly different from standard PGN, read more about it [here](src/gungi/pgn.ts).
|
|
520
|
+
|
|
521
|
+
The `newline` is a string representation of a valid RegExp fragment and is used to process the PGN. It defaults to `\n`
|
|
522
|
+
|
|
523
|
+
```ts
|
|
524
|
+
const gungi = new Gungi(ADVANCED_POSITION);
|
|
525
|
+
const pgn = [
|
|
526
|
+
'1.新帥(7-9-1)終 新帥(1-5-1) 新小(2-8-1) 新謀(1-2-1) 新馬(2-8-2)付 新大(3-4-1)終',
|
|
527
|
+
'2.帥(7-9-1)(8-8-1) 新砦(3-2-1) 3.新謀(8-2-1) 新弓(1-8-1) 4.新弓(9-9-1) 新馬(1-3-1)',
|
|
528
|
+
'5.新筒(9-5-1) 新弓(1-8-2)付 6.新兵(8-5-1) 新砦(3-6-1) 7.新大(8-9-1) 砦(3-6-1)(2-5-1)',
|
|
529
|
+
'8.新小(8-9-2)付 新小(1-7-1) 9.新槍(8-2-2)付 新侍(1-2-2)付 10.新兵(9-8-1) 小(1-7-1)(2-7-1)',
|
|
530
|
+
'11.新槍(8-6-1) 新侍(2-4-1) 12.新砦(9-9-2)付 侍(2-4-1)(3-3-1) 13.小(8-9-2)(7-8-1) 新兵(1-4-1)',
|
|
531
|
+
];
|
|
532
|
+
|
|
533
|
+
gungi.loadPgn(pgn.join('\n'));
|
|
534
|
+
gungi.print();
|
|
535
|
+
// -> ' 987654321
|
|
536
|
+
// +ーーーーーーーーー+
|
|
537
|
+
// |・弓・・帥兵馬侍・|1
|
|
538
|
+
// |・馬小・砦・・・・|2
|
|
539
|
+
// |・・・・・大侍砦・|3
|
|
540
|
+
// |・・・・・・・・・|4
|
|
541
|
+
// |・・・・・・・・・|5
|
|
542
|
+
// |・・・・・・・・・|6
|
|
543
|
+
// |・小・・・・・・・|7
|
|
544
|
+
// |大帥・槍兵・・槍・|8
|
|
545
|
+
// |砦兵・・筒・・・・|9
|
|
546
|
+
// +ーーーーーーーーー+'
|
|
307
547
|
```
|
|
308
548
|
|
|
309
|
-
### .
|
|
549
|
+
### .move(move, [ options ])
|
|
550
|
+
|
|
551
|
+
Makes a move on the board and returns a move object if the move was legal. The move argument can be either a string in Standard Algebraic Notation (SAN) or a move object.
|
|
552
|
+
Throws an 'Illegal move' exception if the move was illegal.
|
|
553
|
+
|
|
554
|
+
#### move() - Standard Algebraic Notation (SAN)
|
|
555
|
+
|
|
556
|
+
```ts
|
|
557
|
+
const gungi = new Gungi(INTRO_POSITION);
|
|
558
|
+
gungi.move('兵(7-1-1)(6-1-1)');
|
|
559
|
+
// -> {
|
|
560
|
+
// piece: '兵',
|
|
561
|
+
// color: 'w',
|
|
562
|
+
// from: '7-1-1',
|
|
563
|
+
// to: '6-1-1',
|
|
564
|
+
// type: 'route',
|
|
565
|
+
// san: '兵(7-1-1)(6-1-1)',
|
|
566
|
+
// before: '3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1',
|
|
567
|
+
// after: '3img3/1s2n2s1/d1fwdwf1d/9/9/8D/D1FWDWF2/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 b 0 - 1',
|
|
568
|
+
// }
|
|
569
|
+
gungi.move('大(1-4-1)(2-10-1)');
|
|
570
|
+
// Error: Invalid move: 大(1-4-1)(2-10-1)
|
|
571
|
+
```
|
|
310
572
|
|
|
311
|
-
|
|
573
|
+
#### .move() - Object Notation
|
|
574
|
+
|
|
575
|
+
A move object contains `to`, `from`, `piece`, `type` and, `captured` and `draftFinished` (only when necessary) fields.
|
|
576
|
+
|
|
577
|
+
```ts
|
|
578
|
+
const gungi = new Gungi(INTRO_POSITION);
|
|
579
|
+
gungi.move({ piece: SOLIDER, from: '7-1-1', to: '6-1-1', type: 'route' });
|
|
580
|
+
// -> {
|
|
581
|
+
// piece: '兵',
|
|
582
|
+
// color: 'w',
|
|
583
|
+
// from: '7-1-1',
|
|
584
|
+
// to: '6-1-1',
|
|
585
|
+
// type: 'route',
|
|
586
|
+
// san: '兵(7-1-1)(6-1-1)',
|
|
587
|
+
// before: '3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1',
|
|
588
|
+
// after: '3img3/1s2n2s1/d1fwdwf1d/9/9/8D/D1FWDWF2/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 b 0 - 1',
|
|
589
|
+
// }
|
|
590
|
+
```
|
|
312
591
|
|
|
313
|
-
|
|
314
|
-
const gungi = new Gungi()
|
|
592
|
+
### .moveNumber()
|
|
315
593
|
|
|
316
|
-
|
|
317
|
-
// -> [
|
|
318
|
-
// { piece: { type: '帥', color: 'w' }, amount: 1 },
|
|
319
|
-
// { piece: { type: '兵', color: 'w' }, amount: 9 },
|
|
320
|
-
// { piece: { type: '忍', color: 'w' }, amount: 2 },
|
|
321
|
-
// { piece: { type: '砲', color: 'w' }, amount: 2 },
|
|
322
|
-
// { piece: { type: '砦', color: 'w' }, amount: 2 },
|
|
323
|
-
// { piece: { type: '侍', color: 'w' }, amount: 2 },
|
|
324
|
-
// { piece: { type: '謀', color: 'w' }, amount: 1 },
|
|
325
|
-
// { piece: { type: '筒', color: 'w' }, amount: 1 },
|
|
326
|
-
// { piece: { type: '馬', color: 'w' }, amount: 2 },
|
|
327
|
-
// { piece: { type: '弓', color: 'w' }, amount: 2 },
|
|
328
|
-
// { piece: { type: '大', color: 'w' }, amount: 6 },
|
|
329
|
-
// { piece: { type: '中', color: 'w' }, amount: 4 },
|
|
330
|
-
// { piece: { type: '小', color: 'w' }, amount: 4 },
|
|
331
|
-
// { piece: { type: '帥', color: 'b' }, amount: 1 },
|
|
332
|
-
// { piece: { type: '兵', color: 'b' }, amount: 9 },
|
|
333
|
-
// { piece: { type: '忍', color: 'b' }, amount: 2 },
|
|
334
|
-
// { piece: { type: '砲', color: 'b' }, amount: 2 },
|
|
335
|
-
// { piece: { type: '砦', color: 'b' }, amount: 2 },
|
|
336
|
-
// { piece: { type: '侍', color: 'b' }, amount: 2 },
|
|
337
|
-
// { piece: { type: '謀', color: 'b' }, amount: 1 },
|
|
338
|
-
// { piece: { type: '筒', color: 'b' }, amount: 1 },
|
|
339
|
-
// { piece: { type: '馬', color: 'b' }, amount: 2 },
|
|
340
|
-
// { piece: { type: '弓', color: 'b' }, amount: 2 },
|
|
341
|
-
// { piece: { type: '大', color: 'b' }, amount: 6 },
|
|
342
|
-
// { piece: { type: '中', color: 'b' }, amount: 4 },
|
|
343
|
-
// { piece: { type: '小', color: 'b' }, amount: 4 }
|
|
344
|
-
// ]
|
|
594
|
+
Returns the current move number.
|
|
345
595
|
|
|
346
|
-
|
|
596
|
+
```ts
|
|
597
|
+
gungi.load(
|
|
598
|
+
'd1f1|r:d||r:j|j|k:a:c||g:s|/3|w:s|2|n:w|2/f8/3n2d1m/2i4W1/3dN1tR1/4D2R1/ASJ2|C:a|1A1/|I:F:W|F1SGMT1|K:J| -/- w 3 - 57'
|
|
599
|
+
);
|
|
600
|
+
gungi.moveNumber();
|
|
601
|
+
// -> 57
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
### .moves({ square?: Square, arata?: HandPiece, verbose = false} = {})
|
|
605
|
+
|
|
606
|
+
Returns a list of legal moves from the current position. This function takes an optional object which can be used to generate detailed move objects or to restrict the move generator to specific squares or arata.
|
|
607
|
+
|
|
608
|
+
```ts
|
|
609
|
+
const gungi = new Gungi(INTRO_POSITION);
|
|
610
|
+
gungi.moves();
|
|
347
611
|
// -> [
|
|
348
|
-
//
|
|
349
|
-
//
|
|
350
|
-
//
|
|
351
|
-
//
|
|
352
|
-
//
|
|
353
|
-
//
|
|
354
|
-
//
|
|
355
|
-
//
|
|
356
|
-
//
|
|
357
|
-
//
|
|
358
|
-
//
|
|
359
|
-
//
|
|
360
|
-
//
|
|
361
|
-
//
|
|
612
|
+
// '兵(7-1-1)(6-1-1)', '兵(7-1-1)(8-1-1)', '砦(7-3-1)(6-3-1)',
|
|
613
|
+
// '砦(7-3-1)(7-4-2)付', '砦(7-3-1)(7-2-1)', '砦(7-3-1)(8-4-1)',
|
|
614
|
+
// '砦(7-3-1)(8-2-2)付', '侍(7-4-1)(6-5-1)', '侍(7-4-1)(6-4-1)',
|
|
615
|
+
// '侍(7-4-1)(6-3-1)', '侍(7-4-1)(8-4-1)', '兵(7-5-1)(6-5-1)',
|
|
616
|
+
// '兵(7-5-1)(8-5-2)付', '侍(7-6-1)(6-7-1)', '侍(7-6-1)(6-6-1)',
|
|
617
|
+
// '侍(7-6-1)(6-5-1)', '侍(7-6-1)(8-6-1)', '砦(7-7-1)(6-7-1)',
|
|
618
|
+
// '砦(7-7-1)(7-8-1)', '砦(7-7-1)(7-6-2)付', '砦(7-7-1)(8-8-2)付',
|
|
619
|
+
// '砦(7-7-1)(8-6-1)', '兵(7-9-1)(6-9-1)', '兵(7-9-1)(8-9-1)',
|
|
620
|
+
// '忍(8-2-1)(7-3-2)付', '忍(8-2-1)(7-1-2)付', '忍(8-2-1)(9-3-1)',
|
|
621
|
+
// '忍(8-2-1)(9-1-1)', '槍(8-5-1)(7-6-2)付', '槍(8-5-1)(7-5-2)付',
|
|
622
|
+
// '槍(8-5-1)(7-4-2)付', '忍(8-8-1)(7-9-2)付', '忍(8-8-1)(7-7-2)付',
|
|
623
|
+
// '忍(8-8-1)(9-9-1)', '忍(8-8-1)(9-7-1)', '中(9-4-1)(8-5-2)付',
|
|
624
|
+
// '中(9-4-1)(8-4-1)', '中(9-4-1)(8-3-1)', '中(9-4-1)(7-2-1)',
|
|
625
|
+
// '中(9-4-1)(6-1-1)', '中(9-4-1)(9-3-1)', '帥(9-5-1)(8-6-1)',
|
|
626
|
+
// '帥(9-5-1)(8-5-2)付', '帥(9-5-1)(8-4-1)', '帥(9-5-1)(9-6-2)付',
|
|
627
|
+
// '帥(9-5-1)(9-4-2)付', '大(9-6-1)(8-7-1)', '大(9-6-1)(8-6-1)',
|
|
628
|
+
// '大(9-6-1)(7-6-2)付', '大(9-6-1)(8-5-2)付', '大(9-6-1)(9-7-1)',
|
|
629
|
+
// '大(9-6-1)(9-8-1)', '大(9-6-1)(9-9-1)', '新小(9-1-1)',
|
|
630
|
+
// '新小(9-2-1)', '新小(9-3-1)', '新小(9-4-2)付',
|
|
631
|
+
// '新小(9-6-2)付', '新小(9-7-1)', '新小(9-8-1)',
|
|
632
|
+
// '新小(9-9-1)', '新小(8-1-1)', '新小(8-2-2)付',
|
|
633
|
+
// '新小(8-3-1)', '新小(8-4-1)', '新小(8-5-2)付',
|
|
634
|
+
// '新小(8-6-1)', '新小(8-7-1)', '新小(8-8-2)付',
|
|
635
|
+
// '新小(8-9-1)', '新小(7-1-2)付', '新小(7-2-1)',
|
|
636
|
+
// '新小(7-3-2)付', '新小(7-4-2)付', '新小(7-5-2)付',
|
|
637
|
+
// '新小(7-6-2)付', '新小(7-7-2)付', '新小(7-8-1)',
|
|
638
|
+
// '新小(7-9-2)付', '新槍(9-1-1)', '新槍(9-2-1)',
|
|
639
|
+
// '新槍(9-3-1)', '新槍(9-4-2)付', '新槍(9-6-2)付',
|
|
640
|
+
// '新槍(9-7-1)', '新槍(9-8-1)', '新槍(9-9-1)',
|
|
641
|
+
// '新槍(8-1-1)', '新槍(8-2-2)付', '新槍(8-3-1)',
|
|
642
|
+
// '新槍(8-4-1)', '新槍(8-5-2)付', '新槍(8-6-1)',
|
|
643
|
+
// '新槍(8-7-1)', '新槍(8-8-2)付', '新槍(8-9-1)',
|
|
644
|
+
// '新槍(7-1-2)付', '新槍(7-2-1)', '新槍(7-3-2)付',
|
|
645
|
+
// '新槍(7-4-2)付',
|
|
646
|
+
// ... 57 more items
|
|
647
|
+
// ]
|
|
648
|
+
|
|
649
|
+
gungi.moves({ square: '8-5' }); // single square move generation
|
|
650
|
+
// -> ['槍(8-5-1)(7-6-2)付', '槍(8-5-1)(7-5-2)付', '槍(8-5-1)(7-4-2)付']
|
|
651
|
+
|
|
652
|
+
gungi.moves({ arata: gungi.hand('w').at(0) }); // generate moves for arata
|
|
653
|
+
// -> [
|
|
654
|
+
// '新小(9-1-1)', '新小(9-2-1)',
|
|
655
|
+
// '新小(9-3-1)', '新小(9-4-2)付',
|
|
656
|
+
// '新小(9-6-2)付', '新小(9-7-1)',
|
|
657
|
+
// '新小(9-8-1)', '新小(9-9-1)',
|
|
658
|
+
// '新小(8-1-1)', '新小(8-2-2)付',
|
|
659
|
+
// '新小(8-3-1)', '新小(8-4-1)',
|
|
660
|
+
// '新小(8-5-2)付', '新小(8-6-1)',
|
|
661
|
+
// '新小(8-7-1)', '新小(8-8-2)付',
|
|
662
|
+
// '新小(8-9-1)', '新小(7-1-2)付',
|
|
663
|
+
// '新小(7-2-1)', '新小(7-3-2)付',
|
|
664
|
+
// '新小(7-4-2)付', '新小(7-5-2)付',
|
|
665
|
+
// '新小(7-6-2)付', '新小(7-7-2)付',
|
|
666
|
+
// '新小(7-8-1)', '新小(7-9-2)付'
|
|
667
|
+
// ]
|
|
668
|
+
|
|
669
|
+
gungi.moves({ verbose: true }); // return verbose moves
|
|
670
|
+
/// -> [{ piece: '兵', color: 'w', from: '7-1-1', to: '6-1-1',
|
|
671
|
+
/// type: 'route',
|
|
672
|
+
/// san: '兵(7-1-1)(6-1-1)',
|
|
673
|
+
/// before: '3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1',
|
|
674
|
+
/// after: '3img3/1s2n2s1/d1fwdwf1d/9/9/8D/D1FWDWF2/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 b 0 - 1',
|
|
675
|
+
/// # a `draftFinished` field is included when a move is ending the drafting phase
|
|
676
|
+
/// # a `captured` field is included when a move is a capture or a betrayal
|
|
677
|
+
/// },
|
|
678
|
+
/// ...
|
|
679
|
+
/// ]
|
|
362
680
|
```
|
|
363
681
|
|
|
364
|
-
|
|
682
|
+
#### Move Object (e.g. when { verbose: true })
|
|
365
683
|
|
|
366
|
-
|
|
684
|
+
The `color` field indicates the color of the moving piece (`w` or `b`).
|
|
367
685
|
|
|
368
|
-
|
|
686
|
+
The `from` and `to` fields are from and to squares in algebraic notation.
|
|
369
687
|
|
|
370
|
-
|
|
688
|
+
The `type` field is the type of move made (`route`, `tsuke`, `capture`, `betray`, `arata`).
|
|
371
689
|
|
|
372
|
-
|
|
690
|
+
The `piece` and `captured` fields contain the lowercase representation of the applicable piece (mgijwnrsfdcakt). The captured field are only present when the move is a valid capture or betrayal.
|
|
373
691
|
|
|
374
|
-
|
|
692
|
+
The `draftFinished` field is included when a move is ending the drafting phase.
|
|
375
693
|
|
|
376
|
-
|
|
694
|
+
The `san` field is the move in Standard Algebraic Notation (SAN).
|
|
377
695
|
|
|
378
|
-
|
|
696
|
+
The `before` and `after` keys contain the FEN of the position before and after the move.
|
|
379
697
|
|
|
380
|
-
|
|
381
|
-
const gungi = new Gungi()
|
|
698
|
+
### .pgn([ options ])
|
|
382
699
|
|
|
383
|
-
|
|
384
|
-
// -> 'b'
|
|
385
|
-
gungi.move({src: {type: gungi.MARSHAL, color: gungi.BLACK}, dst: '7-2', type: gungi.PLACE})
|
|
700
|
+
Returns the game in PGN format. Options is an optional parameter which may include max width and/or a newline character settings.
|
|
386
701
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
gungi.move(
|
|
702
|
+
```ts
|
|
703
|
+
const gungi = new Gungi(BEGINNNER_POSITION);
|
|
704
|
+
gungi.move('槍(8-5-1)(7-5-2)付');
|
|
705
|
+
gungi.move('新馬(1-7-1)');
|
|
706
|
+
gungi.move('新忍(8-7-2)付');
|
|
707
|
+
|
|
708
|
+
gungi.pgn({ maxWidth: 2, newlineChar: '<br />' });
|
|
709
|
+
// -> '1. 槍(8-5-1)(7-5-2)付 新馬(1-7-1) <br />2. 新忍(8-7-2)付'
|
|
390
710
|
```
|
|
391
711
|
|
|
392
|
-
### .
|
|
712
|
+
### .print()
|
|
393
713
|
|
|
394
|
-
|
|
714
|
+
Print the ascii representation of the board to the console.
|
|
395
715
|
|
|
396
|
-
|
|
397
|
-
|
|
716
|
+
### .reset()
|
|
717
|
+
|
|
718
|
+
Reset the board to the initial starting position.
|
|
719
|
+
|
|
720
|
+
### .turn()
|
|
721
|
+
|
|
722
|
+
Returns the current side to move.
|
|
723
|
+
|
|
724
|
+
```ts
|
|
725
|
+
gungi.load(
|
|
726
|
+
'3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FW|D:J|WF1D/1S2N2S1/3GMI3 J1N2R2D1/j2n2r2d1 b 0 - 1'
|
|
727
|
+
);
|
|
728
|
+
gungi.turn();
|
|
729
|
+
// -> 'b'
|
|
730
|
+
```
|
|
731
|
+
|
|
732
|
+
### .undo()
|
|
733
|
+
|
|
734
|
+
Takeback the last half-move, returning a move object if successful, otherwise null.
|
|
735
|
+
|
|
736
|
+
```ts
|
|
737
|
+
const gungi = new Gungi(BEGINNNER_POSITION);
|
|
738
|
+
|
|
739
|
+
gungi.fen();
|
|
740
|
+
// -> '3img3/1ra1n1as1/d1fwdwf1d/9/9/9/D1FWDWF1D/1SA1N1AR1/3GMI3 J2N2S1R1D1/j2n2s1r1d1 w 1 - 1'
|
|
741
|
+
gungi.move('兵(7-1-1)(6-1-1)');
|
|
742
|
+
gungi.fen();
|
|
743
|
+
// -> '3img3/1ra1n1as1/d1fwdwf1d/9/9/8D/D1FWDWF2/1SA1N1AR1/3GMI3 J2N2S1R1D1/j2n2s1r1d1 b 1 - 1'
|
|
744
|
+
|
|
745
|
+
gungi.undo();
|
|
746
|
+
// {
|
|
747
|
+
// piece: '兵',
|
|
748
|
+
// color: 'w',
|
|
749
|
+
// from: '7-1-1',
|
|
750
|
+
// to: '6-1-1',
|
|
751
|
+
// type: 'route',
|
|
752
|
+
// san: '兵(7-1-1)(6-1-1)',
|
|
753
|
+
// before: '3img3/1ra1n1as1/d1fwdwf1d/9/9/9/D1FWDWF1D/1SA1N1AR1/3GMI3 J2N2S1R1D1/j2n2s1r1d1 w 1 - 1',
|
|
754
|
+
// after: '3img3/1ra1n1as1/d1fwdwf1d/9/9/8D/D1FWDWF2/1SA1N1AR1/3GMI3 J2N2S1R1D1/j2n2s1r1d1 b 1 - 1',
|
|
755
|
+
// }
|
|
756
|
+
|
|
757
|
+
gungi.fen();
|
|
758
|
+
// -> '3img3/1ra1n1as1/d1fwdwf1d/9/9/9/D1FWDWF1D/1SA1N1AR1/3GMI3 J2N2S1R1D1/j2n2s1r1d1 w 1 - 1'
|
|
759
|
+
gungi.undo();
|
|
760
|
+
// -> null
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
### validateFen(fen):
|
|
398
764
|
|
|
399
|
-
|
|
400
|
-
gungi.move({src: {type: gungi.MARSHAL, color: gungi.WHITE}, dst: '2-5', type: gungi.PLACE})
|
|
765
|
+
This static function returns a validation object specifying validity or the errors found within the FEN string.
|
|
401
766
|
|
|
402
|
-
|
|
403
|
-
|
|
767
|
+
```ts
|
|
768
|
+
import { validateFen } from 'gungi.js';
|
|
404
769
|
|
|
405
|
-
|
|
406
|
-
|
|
770
|
+
validateFen(
|
|
771
|
+
'3img3/1s2n2s1/d1fw1wf1d/9/4J4/9/D1FWD|W:N|F1D/1S2N2S1/3GMI3 J1N1R2D1/j2n2r2d1 b 0 - 3'
|
|
772
|
+
);
|
|
773
|
+
// -> { ok: true }
|
|
407
774
|
|
|
408
|
-
|
|
409
|
-
// ->
|
|
775
|
+
validateFen('3img3/1ra1n1xas1/d1fwdwf1d/9/9/9/9/9/9 J2N2R1D1/j2n2r2d1 w 1 - 1');
|
|
776
|
+
// -> { ok: false,
|
|
777
|
+
// error: '1st field (piece positions) is invalid [invalid piece] @(2-7)' }
|
|
410
778
|
```
|