- switch (this.getPiece(sq[0], sq[1])) {
- case V.PAWN: return this.getPotentialPawnMoves(sq);
- case V.ROOK: return super.getPotentialRookMoves(sq);
- case V.KNIGHT: return this.getPotentialKnightMoves(sq);
- case V.ELEPHANT: return this.getPotentialElephantMoves(sq);
- case V.ADVISOR: return this.getPotentialAdvisorMoves(sq);
- case V.KING: return this.getPotentialKingMoves(sq);
- case V.CANNON: return this.getPotentialCannonMoves(sq);
+ let moves = [];
+ const piece = this.getPiece(sq[0], sq[1]);
+ switch (piece) {
+ case V.PAWN:
+ moves = this.getPotentialPawnMoves(sq);
+ break;
+ case V.ROOK:
+ moves = super.getPotentialRookMoves(sq);
+ break;
+ case V.KNIGHT:
+ moves = this.getPotentialKnightMoves(sq);
+ break;
+ case V.ELEPHANT:
+ moves = this.getPotentialElephantMoves(sq);
+ break;
+ case V.ADVISOR:
+ moves = this.getPotentialAdvisorMoves(sq);
+ break;
+ case V.KING:
+ moves = this.getPotentialKingMoves(sq);
+ break;
+ case V.CANNON:
+ moves = this.getPotentialCannonMoves(sq);
+ break;
+ }
+ if (piece != V.KING && this.kingPos['w'][1] != this.kingPos['b'][1])
+ return moves;
+ if (this.kingPos['w'][1] == this.kingPos['b'][1]) {
+ const colKing = this.kingPos['w'][1];
+ let intercept = 0; //count intercepting pieces
+ for (let i = this.kingPos['b'][0] + 1; i < this.kingPos['w'][0]; i++) {
+ if (this.board[i][colKing] != V.EMPTY) intercept++;
+ }
+ if (intercept >= 2) return moves;
+ // intercept == 1 (0 is impossible):
+ // Any move not removing intercept is OK
+ return moves.filter(m => {
+ return (
+ // From another column?
+ m.start.y != colKing ||
+ // From behind a king? (including kings themselves!)
+ m.start.x <= this.kingPos['b'][0] ||
+ m.start.x >= this.kingPos['w'][0] ||
+ // Intercept piece moving: must remain in-between
+ (
+ m.end.y == colKing &&
+ m.end.x > this.kingPos['b'][0] &&
+ m.end.x < this.kingPos['w'][0]
+ )
+ );
+ });