1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
3 export class JangqiRules
extends ChessRules
{
5 static get Monochrome() {
9 static get Notoodark() {
15 // Draw all inter-squares lines, shifted:
16 for (let i
= 0; i
< V
.size
.x
; i
++)
17 lines
.push([[i
+0.5, 0.5], [i
+0.5, V
.size
.y
-0.5]]);
18 for (let j
= 0; j
< V
.size
.y
; j
++)
19 lines
.push([[0.5, j
+0.5], [V
.size
.x
-0.5, j
+0.5]]);
21 lines
.push([[0.5, 3.5], [2.5, 5.5]]);
22 lines
.push([[0.5, 5.5], [2.5, 3.5]]);
23 lines
.push([[9.5, 3.5], [7.5, 5.5]]);
24 lines
.push([[9.5, 5.5], [7.5, 3.5]]);
28 // No castle, but flag: bikjang
29 static get HasCastle() {
33 static get HasEnpassant() {
37 static get LoseOnRepetition() {
41 static get ELEPHANT() {
49 static get ADVISOR() {
54 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.ELEPHANT
, V
.ADVISOR
, V
.KING
, V
.CANNON
];
58 return "Jiangqi/" + b
;
62 return { x: 10, y: 9};
65 getPotentialMovesFrom(sq
) {
66 switch (this.getPiece(sq
[0], sq
[1])) {
67 case V
.PAWN: return this.getPotentialPawnMoves(sq
);
68 case V
.ROOK: return this.getPotentialRookMoves(sq
);
69 case V
.KNIGHT: return this.getPotentialKnightMoves(sq
);
70 case V
.ELEPHANT: return this.getPotentialElephantMoves(sq
);
71 case V
.ADVISOR: return this.getPotentialAdvisorMoves(sq
);
72 case V
.KING: return this.getPotentialKingMoves(sq
);
73 case V
.CANNON: return this.getPotentialCannonMoves(sq
);
75 return []; //never reached
78 static IsGoodFlags(flags
) {
79 // bikjang status of last move + pass
80 return !!flags
.match(/^[0-2]{2,2}$/);
84 return [this.bikjangFlag
, this.passFlag
];
87 disaggregateFlags(flags
) {
88 this.bikjangFlag
= flags
[0];
89 this.passFlag
= flags
[1];
93 return this.bikjangFlag
.toString() + this.passFlag
.toString()
97 this.bikjangFlag
= parseInt(fenflags
.charAt(0), 10);
98 this.passFlag
= parseInt(fenflags
.charAt(1), 10);
101 setOtherVariables(fen
) {
102 super.setOtherVariables(fen
);
103 // Sub-turn is useful only at first move...
107 getPotentialMovesFrom([x
, y
]) {
109 const c
= this.getColor(x
, y
);
110 const oppCol
= V
.GetOppCol(c
);
111 if (this.kingPos
[c
][0] == x
&& this.kingPos
[c
][1] == y
) {
112 // Add pass move (might be impossible if undercheck)
117 start: { x: this.kingPos
[c
][0], y: this.kingPos
[c
][1] },
118 end: { x: this.kingPos
[oppCol
][0], y: this.kingPos
[oppCol
][1] }
122 // TODO: next "if" is mutually exclusive with the block above
123 if (this.movesCount
<= 1) {
124 const firstRank
= (this.movesCount
== 0 ? 9 : 0);
125 const [initFile
, destFile
] = (this.subTurn
== 1 ? [1, 2] : [7, 6]);
126 // Only option is knight / elephant swap:
127 if (x
== firstRank
&& y
== initFile
) {
158 start: { x: x
, y: y
},
159 end: { x: x
, y: destFile
}
165 Array
.prototype.push
.apply(moves
, super.getPotentialMovesFrom([x
, y
]));
169 getPotentialPawnMoves([x
, y
]) {
170 const c
= this.getColor(x
, y
);
171 const oppCol
= V
.GetOppCol(c
);
172 const shiftX
= (c
== 'w' ? -1 : 1);
173 const rank23
= (oppCol
== 'w' ? [8, 7] : [1, 2]);
174 let steps
= [[shiftX
, 0], [0, -1], [0, 1]];
175 // Diagonal moves inside enemy palace:
176 if (y
== 4 && x
== rank23
[0])
177 Array
.prototype.push
.apply(steps
, [[shiftX
, 1], [shiftX
, -1]]);
178 else if (x
== rank23
[1]) {
179 if (y
== 3) steps
.push([shiftX
, 1]);
180 else if (y
== 5) steps
.push([shiftX
, -1]);
182 return super.getSlideNJumpMoves([x
, y
], steps
, "oneStep");
185 knightStepsFromRookStep(step
) {
186 if (step
[0] == 0) return [ [1, 2*step
[1]], [-1, 2*step
[1]] ];
187 return [ [2*step
[0], 1], [2*step
[0], -1] ];
190 getPotentialKnightMoves([x
, y
]) {
192 for (let rookStep
of ChessRules
.steps
[V
.ROOK
]) {
193 const [i
, j
] = [x
+ rookStep
[0], y
+ rookStep
[1]];
194 if (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
195 Array
.prototype.push
.apply(steps
,
196 // These moves might be impossible, but need to be checked:
197 this.knightStepsFromRookStep(rookStep
));
200 return super.getSlideNJumpMoves([x
, y
], steps
, "oneStep");
203 elephantStepsFromRookStep(step
) {
204 if (step
[0] == 0) return [ [2, 3*step
[1]], [-2, 3*step
[1]] ];
205 return [ [3*step
[0], 2], [3*step
[0], -2] ];
208 getPotentialElephantMoves([x
, y
]) {
210 for (let rookStep
of ChessRules
.steps
[V
.ROOK
]) {
211 const eSteps
= this.elephantStepsFromRookStep(rookStep
);
212 const [i
, j
] = [x
+ rookStep
[0], y
+ rookStep
[1]];
213 if (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
214 // Check second crossing:
215 const knightSteps
= this.knightStepsFromRookStep(rookStep
);
216 for (let k
of [0, 1]) {
217 const [ii
, jj
] = [x
+ knightSteps
[k
][0], y
+ knightSteps
[k
][1]];
218 if (V
.OnBoard(ii
, jj
) && this.board
[ii
][jj
] == V
.EMPTY
)
219 steps
.push(eSteps
[k
]); //ok: same ordering
223 return super.getSlideNJumpMoves([x
, y
], steps
, "oneStep");
226 palacePeopleMoves([x
, y
]) {
227 const c
= this.getColor(x
, y
);
230 if (x
< (c
== 'w' ? 9 : 2)) steps
.push([1, 0]);
231 if (x
> (c
== 'w' ? 7 : 0)) steps
.push([-1, 0]);
232 if (y
> 3) steps
.push([0, -1]);
233 if (y
< 5) steps
.push([0, 1]);
234 // Diagonal steps, if in the middle or corner:
238 (c
== 'w' && x
!= 8) ||
242 // In a corner: maximum one diagonal step available
244 const direction
= (c
== 'w' ? -1 : 1);
245 if ((c
== 'w' && x
== 9) || (c
== 'b' && x
== 0)) {
247 if (y
== 3) step
= [direction
, 1];
248 else step
= [direction
, -1];
250 else if ((c
== 'w' && x
== 7) || (c
== 'b' && x
== 2)) {
252 if (y
== 3) step
= [-direction
, 1];
253 else step
= [-direction
, -1];
260 (c
== 'w' && x
== 8) ||
264 // At the middle: all directions available
265 Array
.prototype.push
.apply(steps
, ChessRules
.steps
[V
.BISHOP
]);
267 return super.getSlideNJumpMoves([x
, y
], steps
, "oneStep");
270 getPotentialAdvisorMoves(sq
) {
271 return this.palacePeopleMoves(sq
);
274 getPotentialKingMoves(sq
) {
275 return this.palacePeopleMoves(sq
);
278 getPotentialRookMoves([x
, y
]) {
279 let moves
= super.getPotentialRookMoves([x
, y
]);
280 if ([3, 5].includes(y
) && [0, 2, 7, 9].includes(x
)) {
281 // In a corner of a palace: move along diagonal
282 const step
= [[0, 7].includes(x
) ? 1 : -1, 4 - y
];
283 const oppCol
= V
.GetOppCol(this.getColor(x
, y
));
284 for (let i
of [1, 2]) {
285 const [xx
, yy
] = [x
+ i
* step
[0], y
+ i
* step
[1]];
286 if (this.board
[xx
][yy
] == V
.EMPTY
)
287 moves
.push(this.getBasicMove([x
, y
], [xx
, yy
]));
289 if (this.getColor(xx
, yy
) == oppCol
)
290 moves
.push(this.getBasicMove([x
, y
], [xx
, yy
]));
295 else if (y
== 4 && [1, 8].includes(x
)) {
296 // In the middle of a palace: 4 one-diagonal-step to check
297 Array
.prototype.push
.apply(
299 super.getSlideNJumpMoves([x
, y
],
300 ChessRules
.steps
[V
.BISHOP
],
307 // NOTE: (mostly) duplicated from Shako (TODO?)
308 getPotentialCannonMoves([x
, y
]) {
309 const oppCol
= V
.GetOppCol(this.turn
);
311 // Look in every direction until an obstacle (to jump) is met
312 for (const step
of V
.steps
[V
.ROOK
]) {
315 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
319 // Then, search for an enemy (if jumped piece isn't a cannon)
320 if (V
.OnBoard(i
, j
) && this.getPiece(i
, j
) != V
.CANNON
) {
323 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
324 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
330 this.getColor(i
, j
) == oppCol
&&
331 this.getPiece(i
, j
) != V
.CANNON
333 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
337 if ([3, 5].includes(y
) && [0, 2, 7, 9].includes(x
)) {
338 // In a corner of a palace: hop over next obstacle if possible
339 const step
= [[0, 7].includes(x
) ? 1 : -1, 4 - y
];
340 const [x1
, y1
] = [x
+ step
[0], y
+ step
[1]];
341 const [x2
, y2
] = [x
+ 2 * step
[0], y
+ 2 * step
[1]];
343 this.board
[x1
][y1
] != V
.EMPTY
&&
344 this.getPiece(x1
, y1
) != V
.CANNON
&&
346 this.board
[x2
][y2
] == V
.EMPTY
||
348 this.getColor(x2
, y2
) == oppCol
&&
349 this.getPiece(x2
, y2
) != V
.CANNON
353 moves
.push(this.getBasicMove([x
, y
], [x2
, y2
]));
359 // (King) Never attacked by advisor, since it stays in the palace
360 isAttacked(sq
, color
) {
362 this.isAttackedByPawn(sq
, color
) ||
363 this.isAttackedByRook(sq
, color
) ||
364 this.isAttackedByKnight(sq
, color
) ||
365 this.isAttackedByElephant(sq
, color
) ||
366 this.isAttackedByCannon(sq
, color
)
370 onPalaceDiagonal([x
, y
]) {
372 (y
== 4 && [1, 8].includes(x
)) ||
373 ([3, 5].includes(y
) && [0, 2, 7, 9].includes(x
))
377 isAttackedByPawn([x
, y
], color
) {
378 const shiftX
= (color
== 'w' ? 1 : -1); //shift from king
379 if (super.isAttackedBySlideNJump(
380 [x
, y
], color
, V
.PAWN
, [[shiftX
, 0], [0, 1], [0, -1]], "oneStep")
384 if (this.onPalaceDiagonal([x
, y
])) {
385 for (let yStep
of [-1, 1]) {
386 const [xx
, yy
] = [x
+ shiftX
, y
+ yStep
];
388 this.onPalaceDiagonal([xx
,yy
]) &&
389 this.board
[xx
][yy
] != V
.EMPTY
&&
390 this.getColor(xx
, yy
) == color
&&
391 this.getPiece(xx
, yy
) == V
.PAWN
400 knightStepsFromBishopStep(step
) {
401 return [ [2*step
[0], step
[1]], [step
[0], 2*step
[1]] ];
404 isAttackedByKnight([x
, y
], color
) {
405 // Check bishop steps: if empty, look continuation knight step
407 for (let s
of ChessRules
.steps
[V
.BISHOP
]) {
408 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
411 this.board
[i
][j
] == V
.EMPTY
413 Array
.prototype.push
.apply(steps
, this.knightStepsFromBishopStep(s
));
417 super.isAttackedBySlideNJump([x
, y
], color
, V
.KNIGHT
, steps
, "oneStep")
421 elephantStepsFromBishopStep(step
) {
422 return [ [3*step
[0], 2*step
[1]], [2*step
[0], 3*step
[1]] ];
425 isAttackedByElephant([x
, y
], color
) {
426 // Check bishop steps: if empty, look continuation elephant step
428 for (let s
of ChessRules
.steps
[V
.BISHOP
]) {
429 const [i1
, j1
] = [x
+ s
[0], y
+ s
[1]];
430 const [i2
, j2
] = [x
+ 2*s
[0], y
+ 2*s
[1]];
432 V
.OnBoard(i2
, j2
) && this.board
[i2
][j2
] == V
.EMPTY
&&
433 V
.OnBoard(i1
, j1
) && this.board
[i1
][j1
] == V
.EMPTY
435 Array
.prototype.push
.apply(steps
, this.elephantStepsFromBishopStep(s
));
439 super.isAttackedBySlideNJump([x
, y
], color
, V
.ELEPHANT
, steps
, "oneStep")
443 isAttackedByRook([x
, y
], color
) {
444 if (super.isAttackedByRook([x
, y
], color
)) return true;
445 // Also check diagonals, if inside palace
446 if (this.onPalaceDiagonal([x
, y
])) {
447 // TODO: next scan is clearly suboptimal
448 for (let s
of ChessRules
.steps
[V
.BISHOP
]) {
449 for (let i
of [1, 2]) {
450 const [xx
, yy
] = [x
+ i
* s
[0], y
+ i
* s
[1]];
453 this.onPalaceDiagonal([xx
, yy
])
455 if (this.board
[xx
][yy
] != V
.EMPTY
) {
457 this.getColor(xx
, yy
) == color
&&
458 this.getPiece(xx
, yy
) == V
.ROOK
472 // NOTE: (mostly) duplicated from Shako (TODO?)
473 isAttackedByCannon([x
, y
], color
) {
474 // Reversed process: is there an obstacle in line,
475 // and a cannon next in the same line?
476 for (const step
of V
.steps
[V
.ROOK
]) {
477 let [i
, j
] = [x
+step
[0], y
+step
[1]];
478 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
482 if (V
.OnBoard(i
, j
) && this.getPiece(i
, j
) != V
.CANNON
) {
483 // Keep looking in this direction
486 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
492 this.getPiece(i
, j
) == V
.CANNON
&&
493 this.getColor(i
, j
) == color
503 if ([this.bikjangFlag
, this.passFlag
].includes(2)) return "1/2";
504 const color
= this.turn
;
505 // super.atLeastOneMove() does not consider passing (OK)
506 if (this.underCheck(color
) && !super.atLeastOneMove())
507 return (color
== "w" ? "0-1" : "1-0");
511 static get VALUES() {
523 static get SEARCH_DEPTH() {
527 static GenRandInitFen() {
528 // No randomization here (but initial setup choice)
530 "rnea1aenr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RNEA1AENR w 0 00"
535 move.subTurn
= this.subTurn
; //much easier
536 if (this.movesCount
>= 2 || this.subTurn
== 2 || move.vanish
.length
== 0) {
537 this.turn
= V
.GetOppCol(this.turn
);
541 else this.subTurn
= 2;
542 move.flags
= JSON
.stringify(this.aggregateFlags());
543 V
.PlayOnBoard(this.board
, move);
548 if (move.vanish
.length
> 0) super.postPlay(move);
549 else if (this.movesCount
> 2) this.passFlag
++;
550 // Update bikjang flag
551 if (this.kingPos
['w'][1] == this.kingPos
['b'][1]) {
552 const y
= this.kingPos
['w'][1];
554 for (let x
= this.kingPos
['b'][0] + 1; x
< this.kingPos
['w'][0]; x
++) {
555 if (this.board
[x
][y
] != V
.EMPTY
) {
560 if (bikjang
) this.bikjangFlag
++;
561 else this.bikjangFlag
= 0;
563 else this.bikjangFlag
= 0;
567 this.disaggregateFlags(JSON
.parse(move.flags
));
568 V
.UndoOnBoard(this.board
, move);
570 if (this.movesCount
>= 2 || this.subTurn
== 1 || move.vanish
.length
== 0) {
571 this.turn
= V
.GetOppCol(this.turn
);
574 this.subTurn
= move.subTurn
;
578 if (move.vanish
.length
> 0) super.postUndo(move);
582 if (move.vanish
.length
== 0) return "pass";
583 if (move.appear
.length
== 2) return "S"; //"swap"
584 let notation
= super.getNotation(move);
585 if (move.vanish
.length
== 2 && move.vanish
[0].p
== V
.PAWN
)
586 notation
= "P" + notation
.substr(1);