gungi.js 1.0.20 → 3.0.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/src/gungi.ts DELETED
@@ -1,593 +0,0 @@
1
- import { stockpile_move_gen } from './utils/stockpile_move_gen';
2
- import {
3
- get,
4
- get_top,
5
- init_stockpile,
6
- put,
7
- remove,
8
- remove_stockpile,
9
- } from './utils/helper';
10
- import {
11
- Piece,
12
- StockPiece,
13
- Square,
14
- RelaxMove,
15
- IsPiece,
16
- Move,
17
- MoveHistory,
18
- } from './interfaces/igungi';
19
- import {
20
- BLACK,
21
- WHITE,
22
- DRAFT,
23
- GAME,
24
- MOVEMENT,
25
- ATTACK,
26
- STACK,
27
- PLACE,
28
- READY,
29
- MAJOR_GENERAL,
30
- LIEUTENANT_GENERAL,
31
- GENERAL,
32
- ARCHER,
33
- KNIGHT,
34
- MUSKETEER,
35
- CAPTAIN,
36
- SAMURAI,
37
- FORTRESS,
38
- CANNON,
39
- SPY,
40
- PAWN,
41
- MARSHAL,
42
- RANK_9,
43
- FILE_9,
44
- TIER1_BLACK,
45
- TIER1_WHITE,
46
- TIER2_BLACK,
47
- TIER2_WHITE,
48
- TIER3_BLACK,
49
- TIER3_WHITE,
50
- FILE_1,
51
- RANK_1,
52
- } from './utils/constants';
53
- import {
54
- generate_moves_from_probes,
55
- isSquareOutOfBounds,
56
- single_sqaure_move_gen,
57
- } from './utils/move_gen';
58
-
59
- var _ = require('lodash');
60
-
61
- class Gungi {
62
- readonly BLACK: 'b';
63
- readonly WHITE: 'w';
64
-
65
- readonly DRAFT: 'draft';
66
- readonly GAME: 'game';
67
-
68
- readonly ATTACK: 'attack';
69
- readonly MOVEMENT: 'move';
70
- readonly STACK: 'stack';
71
- readonly PLACE: 'place';
72
- readonly READY: 'ready';
73
-
74
- readonly MAJOR_GENERAL: '小';
75
- readonly LIEUTENANT_GENERAL: '中';
76
- readonly GENERAL: '大';
77
- readonly ARCHER: '弓';
78
- readonly KNIGHT = '馬';
79
- readonly MUSKETEER: '筒';
80
- readonly CAPTAIN: '謀';
81
- readonly SAMURAI: '侍';
82
- readonly FORTRESS: '砦';
83
- readonly CANNON: '砲';
84
- readonly SPY: '忍';
85
- readonly PAWN: '兵';
86
- readonly MARSHAL: '帥';
87
-
88
- board: Array<Array<Array<Piece | null>>>;
89
- turn: 'b' | 'w';
90
- phase: 'game' | 'draft';
91
- stockpile: StockPiece[];
92
-
93
- private _drafted: { w: number; b: number };
94
- private _marshal_placed: { w: number; b: number };
95
- private _army_size: { w: number; b: number };
96
- private _history: MoveHistory[];
97
- private _captured: StockPiece[];
98
- private _turn_count: number;
99
- private _turns_without_placing_or_capturing: number;
100
-
101
- ascii: () => string;
102
- get: (square: Square) => (Piece | null)[];
103
- get_top: (square: Square) => { piece: Piece | null; tier: number } | null;
104
- get_board: () => (Piece | null)[][][];
105
- get_stockpile: (color?: 'b' | 'w') => StockPiece[];
106
- get_army_size: (color: 'b' | 'w') => number;
107
- moves: (options?: { square?: Square | undefined } | undefined) => RelaxMove[];
108
- move: (player_move: Move) => RelaxMove | null;
109
- in_check: () => boolean;
110
- in_checkmate: () => boolean;
111
- in_stalemate: () => boolean;
112
- game_over: () => boolean;
113
- get_captured: (color?: 'b' | 'w') => StockPiece[];
114
- get_history: () => MoveHistory[];
115
-
116
- constructor() {
117
- this.BLACK = BLACK;
118
- this.WHITE = WHITE;
119
-
120
- this.DRAFT = DRAFT;
121
- this.GAME = GAME;
122
- this.ATTACK = ATTACK;
123
- this.MOVEMENT = MOVEMENT;
124
- this.STACK = STACK;
125
- this.PLACE = PLACE;
126
- this.READY = READY;
127
-
128
- this.MAJOR_GENERAL = MAJOR_GENERAL;
129
- this.LIEUTENANT_GENERAL = LIEUTENANT_GENERAL;
130
- this.GENERAL = GENERAL;
131
- this.ARCHER = ARCHER;
132
- this.KNIGHT = KNIGHT;
133
- this.MUSKETEER = MUSKETEER;
134
- this.CAPTAIN = CAPTAIN;
135
- this.SAMURAI = SAMURAI;
136
- this.FORTRESS = FORTRESS;
137
- this.CANNON = CANNON;
138
- this.SPY = SPY;
139
- this.PAWN = PAWN;
140
- this.MARSHAL = MARSHAL;
141
-
142
- this.board = [];
143
- for (let i: number = 0; i < RANK_9; i++) {
144
- this.board[i] = [];
145
- for (let k: number = 0; k < FILE_9; k++) {
146
- this.board[i][k] = [null, null, null];
147
- }
148
- }
149
-
150
- this.turn = BLACK;
151
- this.phase = DRAFT;
152
- this.stockpile = [];
153
- this._history = [];
154
- this._captured = [];
155
- this._drafted = {
156
- w: 0,
157
- b: 0,
158
- };
159
- this._marshal_placed = {
160
- w: 0,
161
- b: 0,
162
- };
163
- this._army_size = {
164
- w: 0,
165
- b: 0,
166
- };
167
- this._turn_count = 0;
168
- this._turns_without_placing_or_capturing = 0;
169
-
170
- Array.prototype.push.apply(this.stockpile, init_stockpile(WHITE));
171
- Array.prototype.push.apply(this.stockpile, init_stockpile(BLACK));
172
-
173
- this.get_board = () => {
174
- return [...this.board];
175
- };
176
-
177
- this.get_stockpile = (color?: 'w' | 'b') => {
178
- if (!color) {
179
- return [...this.stockpile];
180
- }
181
-
182
- return [...this.stockpile.filter((x) => x.piece.color === color)];
183
- };
184
-
185
- this.get_army_size = (color: 'w' | 'b') => {
186
- if (color === 'w') {
187
- return this._army_size.w;
188
- } else {
189
- return this._army_size.b;
190
- }
191
- };
192
-
193
- this.get_captured = (color?: 'w' | 'b') => {
194
- if (!color) {
195
- return [
196
- ..._.orderBy(
197
- this._captured,
198
- ['piece.color', 'piece.type'],
199
- ['asc', 'asc']
200
- ),
201
- ];
202
- }
203
-
204
- return [
205
- ..._.orderBy(
206
- this._captured,
207
- ['piece.color', 'piece.type'],
208
- ['asc', 'asc']
209
- ).filter((x: StockPiece) => x.piece.color == color),
210
- ];
211
- };
212
-
213
- this.get = (square: Square) => {
214
- return get(this.board, square);
215
- };
216
-
217
- this.get_top = (square: Square) => {
218
- return get_top(this.board, square);
219
- };
220
-
221
- this.get_history = () => {
222
- return [...this._history];
223
- };
224
-
225
- this.in_check = () => {
226
- var is_in_check = false;
227
- var vision: { rank: number; file: number }[] = [];
228
-
229
- for (var i = RANK_1; i <= RANK_9; i++) {
230
- for (var j = FILE_1; j <= FILE_9; j++) {
231
- var square = i + '-' + j;
232
- var src = get_top(this.board, square);
233
- if (src && src.piece?.color !== this.turn) {
234
- var probes = single_sqaure_move_gen(this.board, src, square);
235
- vision = vision.concat(probes);
236
- }
237
- }
238
- }
239
-
240
- vision.forEach((square) => {
241
- var top = get_top(this.board, square.rank + '-' + square.file);
242
- if (
243
- top !== null &&
244
- top.piece?.type === MARSHAL &&
245
- top.piece?.color == this.turn
246
- ) {
247
- is_in_check = true;
248
- }
249
- });
250
-
251
- return is_in_check;
252
- };
253
-
254
- this.in_checkmate = () => {
255
- return (
256
- (this.in_check() && this.moves().length == 0) ||
257
- this._captured.find(
258
- (x) => x.piece.color === this.turn && x.piece.type === MARSHAL
259
- ) !== undefined
260
- );
261
- };
262
-
263
- this.in_stalemate = () => {
264
- return (
265
- (!this.in_check() && this.moves().length == 0) ||
266
- this._turns_without_placing_or_capturing > 50
267
- );
268
- };
269
-
270
- this.game_over = () => {
271
- return this.in_checkmate() || this.in_stalemate();
272
- };
273
-
274
- this.moves = (options?: { square?: Square }) => {
275
- let moves: RelaxMove[] = [];
276
-
277
- if (this.phase === GAME) {
278
- for (var i = RANK_1; i <= RANK_9; i++) {
279
- for (var j = FILE_1; j <= FILE_9; j++) {
280
- var square = i + '-' + j;
281
- var src = get_top(this.board, square);
282
- if (src != null && src.piece?.color === this.turn) {
283
- var probes = single_sqaure_move_gen(this.board, src, square);
284
- moves = moves.concat(
285
- generate_moves_from_probes(
286
- this.board,
287
- probes,
288
- src,
289
- square,
290
- this.turn
291
- )
292
- );
293
- }
294
- }
295
- }
296
- }
297
-
298
- moves = moves.concat(
299
- stockpile_move_gen(
300
- this.board,
301
- this.phase,
302
- this.turn,
303
- this.stockpile,
304
- this._marshal_placed,
305
- this._army_size
306
- )
307
- );
308
-
309
- var temp_board = JSON.stringify(this.board);
310
- for (var i = moves.length - 1; i >= 0; i--) {
311
- var move = moves[i];
312
-
313
- switch (move.type) {
314
- case MOVEMENT:
315
- if (move.dst && typeof move.src === 'string') {
316
- var piece = remove(this.board, move.src);
317
- put(this.board, piece, move.dst);
318
- }
319
- break;
320
- case PLACE:
321
- if (move.src && move.dst && IsPiece(move.src)) {
322
- put(
323
- this.board,
324
- {
325
- type: move.src.type,
326
- color: move.src.color,
327
- },
328
- move.dst
329
- );
330
- }
331
- break;
332
- case ATTACK:
333
- if (move.dst) {
334
- var piece = remove(this.board, move.dst);
335
-
336
- if (get_top(this.board, move.dst) === null) {
337
- if (move.src && typeof move.src === 'string') {
338
- var temp = remove(this.board, move.src);
339
- put(this.board, temp, move.dst);
340
- }
341
- }
342
- }
343
- break;
344
- case STACK:
345
- if (move.src && move.dst && typeof move.src === 'string') {
346
- var piece = remove(this.board, move.src);
347
- put(this.board, piece, move.dst);
348
- }
349
- break;
350
- default:
351
- break;
352
- }
353
-
354
- // pawns can't place opponent into mate
355
- // if (move.src !== null && IsPiece(move.src) && move.src.type === PAWN) {
356
- // if (this.turn === BLACK) {
357
- // this.turn = WHITE;
358
- // } else {
359
- // this.turn = BLACK;
360
- // }
361
-
362
- // // if (this.in_checkmate()) {
363
- // // moves.splice(i, 1);
364
- // // }
365
-
366
- // if (this.turn === BLACK) {
367
- // this.turn = WHITE;
368
- // } else {
369
- // this.turn = BLACK;
370
- // }
371
- // }
372
-
373
- if (this.in_check()) {
374
- moves.splice(i, 1);
375
- }
376
-
377
- this.board = JSON.parse(temp_board);
378
- }
379
-
380
- if (options && options.square != null) {
381
- var input = options.square.split('-');
382
- var rank = parseInt(input[0]);
383
- var file = parseInt(input[1]);
384
-
385
- if (!isSquareOutOfBounds(rank, file)) {
386
- return moves.filter(
387
- (x) =>
388
- x.src !== null &&
389
- x.src === options.square &&
390
- x.type !== PLACE &&
391
- x.type !== READY
392
- );
393
- }
394
- }
395
-
396
- return moves;
397
- };
398
-
399
- this.move = (player_move: Move) => {
400
- var legal_moves = this.moves().filter(
401
- (x) => JSON.stringify(x) === JSON.stringify(player_move)
402
- );
403
- if (legal_moves.length) {
404
- var legal_move = legal_moves[0];
405
- var source_piece =
406
- typeof legal_move.src === 'string'
407
- ? get_top(this.board, legal_move.src)
408
- : null;
409
- var destination_piece = null;
410
-
411
- if (typeof legal_move.dst === 'string') {
412
- destination_piece = get_top(this.board, legal_move.dst);
413
- if (destination_piece == null) {
414
- destination_piece = { piece: null, tier: 1 };
415
- }
416
- }
417
-
418
- switch (legal_move.type) {
419
- case MOVEMENT:
420
- if (
421
- legal_move &&
422
- legal_move.dst &&
423
- typeof legal_move.src === 'string'
424
- ) {
425
- var piece = remove(this.board, legal_move.src);
426
- put(this.board, piece, legal_move.dst);
427
- this._turns_without_placing_or_capturing++;
428
- }
429
- break;
430
- case PLACE:
431
- if (legal_move && legal_move.dst && IsPiece(legal_move.src)) {
432
- remove_stockpile(this.stockpile, legal_move.src);
433
-
434
- put(
435
- this.board,
436
- {
437
- type: legal_move.src.type,
438
- color: legal_move.src.color,
439
- },
440
- legal_move.dst
441
- );
442
-
443
- this._turns_without_placing_or_capturing = 0;
444
-
445
- if (legal_move.src.color === BLACK) {
446
- this._army_size.b++;
447
- if (legal_move.src.type === MARSHAL) {
448
- this._marshal_placed.b = 1;
449
- }
450
- } else {
451
- this._army_size.w++;
452
- if (legal_move.src.type === MARSHAL) {
453
- this._marshal_placed.w = 1;
454
- }
455
- }
456
- }
457
-
458
- break;
459
- case ATTACK:
460
- if (legal_move.dst) {
461
- var piece = remove(this.board, legal_move.dst);
462
-
463
- if (piece) {
464
- var c = this._captured.filter(
465
- (x) => JSON.stringify(x.piece) === JSON.stringify(piece)
466
- );
467
- if (c.length > 0) {
468
- c[0].amount++;
469
- } else {
470
- this._captured.push({ piece, amount: 1 });
471
- }
472
-
473
- if (piece.color === 'b') {
474
- this._army_size.b--;
475
- } else {
476
- this._army_size.w--;
477
- }
478
- this._turns_without_placing_or_capturing = 0;
479
- }
480
-
481
- if (legal_move && typeof legal_move.src === 'string') {
482
- var temp = remove(this.board, legal_move.src);
483
- put(this.board, temp, legal_move.dst);
484
- }
485
- }
486
- break;
487
- case STACK:
488
- if (
489
- legal_move &&
490
- legal_move.dst &&
491
- typeof legal_move.src === 'string'
492
- ) {
493
- var piece = remove(this.board, legal_move.src);
494
- put(this.board, piece, legal_move.dst);
495
- this._turns_without_placing_or_capturing++;
496
- }
497
- break;
498
- case READY:
499
- if (this.turn === BLACK) {
500
- this._drafted.b = 1;
501
- } else {
502
- this._drafted.w = 1;
503
- }
504
- this._turns_without_placing_or_capturing = 0;
505
-
506
- break;
507
- }
508
-
509
- this._turn_count++;
510
- this._history.push({
511
- moveNumber: this._turn_count,
512
- turn: this.turn,
513
- src: legal_move.src,
514
- dst: legal_move.dst,
515
- type: legal_move.type,
516
- srcTier: source_piece?.tier,
517
- srcPiece: source_piece?.piece,
518
- dstTier: destination_piece?.tier,
519
- dstPiece: destination_piece?.piece,
520
- });
521
-
522
- if (this.phase === DRAFT) {
523
- if (this._drafted.w === 0 && this._drafted.b === 0) {
524
- if (this.turn === BLACK) {
525
- this.turn = WHITE;
526
- } else {
527
- this.turn = BLACK;
528
- }
529
- } else if (this._drafted.w === 1 && this._drafted.b === 0) {
530
- this.turn = BLACK;
531
- } else if (this._drafted.w === 0 && this._drafted.b === 1) {
532
- this.turn = WHITE;
533
- } else if (this._drafted.w === 1 && this._drafted.b === 1) {
534
- this.turn = WHITE;
535
- this.phase = GAME;
536
- }
537
- } else {
538
- // Game phase
539
- if (this.turn === BLACK) {
540
- this.turn = WHITE;
541
- } else {
542
- this.turn = BLACK;
543
- }
544
- }
545
-
546
- return legal_move;
547
- }
548
- return null;
549
- };
550
-
551
- this.ascii = () => {
552
- var s =
553
- ' +--------------------------------------------------------+\n';
554
- s += ' | |\n';
555
- for (let i = 9; i > 0; i--) {
556
- s += ` r${i} | `;
557
-
558
- for (let j = 1; j < 10; j++) {
559
- let top = get_top(this.board, i + '-' + j);
560
- if (top !== null) {
561
- let type = top.piece?.type;
562
- let color = top.piece?.color;
563
- let symbol = '';
564
-
565
- switch (top.tier) {
566
- case 1:
567
- symbol = color === WHITE ? TIER1_WHITE : TIER1_BLACK;
568
- break;
569
- case 2:
570
- symbol = color === WHITE ? TIER2_WHITE : TIER2_BLACK;
571
- break;
572
- case 3:
573
- symbol = color === WHITE ? TIER3_WHITE : TIER3_BLACK;
574
- break;
575
- }
576
-
577
- s += `${symbol + type} `;
578
- } else {
579
- s += '。。 ';
580
- }
581
- }
582
- s += '|\n';
583
- s +=
584
- ' | |\n';
585
- }
586
- s += ' +--------------------------------------------------------+\n';
587
- s += ' f1 f2 f3 f4 f5 f6 f7 f8 f9';
588
- return s;
589
- };
590
- }
591
- }
592
-
593
- export { Gungi };
@@ -1,133 +0,0 @@
1
- export interface StockPiece {
2
- piece: Piece;
3
- amount: number;
4
- }
5
-
6
- export interface MoveHistory {
7
- moveNumber: number;
8
- turn: string;
9
- src: Piece | string | null;
10
- srcTier?: number;
11
- srcPiece?: Piece | null;
12
- dst: string | null;
13
- dstTier?: number;
14
- dstPiece?: Piece | null;
15
- type: 'place' | 'ready' | 'attack' | 'move' | 'stack';
16
- }
17
-
18
- export interface Move {
19
- src: Piece | Square | null;
20
- dst: Square | null;
21
- type: 'place' | 'ready' | 'attack' | 'move' | 'stack';
22
- }
23
-
24
- export interface RelaxMove {
25
- src: Piece | string | null;
26
- dst: string | null;
27
- type: 'place' | 'ready' | 'attack' | 'move' | 'stack';
28
- }
29
-
30
- export function IsPiece(src: string | Piece | null): src is Piece {
31
- return (src as Piece).type !== undefined;
32
- }
33
-
34
- export interface Piece {
35
- type:
36
- | '小'
37
- | '中'
38
- | '大'
39
- | '弓'
40
- | '馬'
41
- | '筒'
42
- | '謀'
43
- | '侍'
44
- | '砦'
45
- | '砲'
46
- | '忍'
47
- | '兵'
48
- | '帥';
49
- color: 'w' | 'b';
50
- }
51
-
52
- export type Square =
53
- | '1-1'
54
- | '1-2'
55
- | '1-3'
56
- | '1-4'
57
- | '1-5'
58
- | '1-6'
59
- | '1-7'
60
- | '1-8'
61
- | '1-9'
62
- | '2-1'
63
- | '2-2'
64
- | '2-3'
65
- | '2-4'
66
- | '2-5'
67
- | '2-6'
68
- | '2-7'
69
- | '2-8'
70
- | '2-9'
71
- | '3-1'
72
- | '3-2'
73
- | '3-3'
74
- | '3-4'
75
- | '3-5'
76
- | '3-6'
77
- | '3-7'
78
- | '3-8'
79
- | '3-9'
80
- | '4-1'
81
- | '4-2'
82
- | '4-3'
83
- | '4-4'
84
- | '4-5'
85
- | '4-6'
86
- | '4-7'
87
- | '4-8'
88
- | '4-9'
89
- | '5-1'
90
- | '5-2'
91
- | '5-3'
92
- | '5-4'
93
- | '5-5'
94
- | '5-6'
95
- | '5-7'
96
- | '5-8'
97
- | '5-9'
98
- | '6-1'
99
- | '6-2'
100
- | '6-3'
101
- | '6-4'
102
- | '6-5'
103
- | '6-6'
104
- | '6-7'
105
- | '6-8'
106
- | '6-9'
107
- | '7-1'
108
- | '7-2'
109
- | '7-3'
110
- | '7-4'
111
- | '7-5'
112
- | '7-6'
113
- | '7-7'
114
- | '7-8'
115
- | '7-9'
116
- | '8-1'
117
- | '8-2'
118
- | '8-3'
119
- | '8-4'
120
- | '8-5'
121
- | '8-6'
122
- | '8-7'
123
- | '8-8'
124
- | '8-9'
125
- | '9-1'
126
- | '9-2'
127
- | '9-3'
128
- | '9-4'
129
- | '9-5'
130
- | '9-6'
131
- | '9-7'
132
- | '9-8'
133
- | '9-9';