- const c = this.getOppCol(this.turn); //opponent
- const saveKingPos = this.kingPos[c]; //king might be taken
- this.play(move);
- // The only way to be "under check" is to have lost the king (thus game over)
- let res = this.kingPos[c][0] < 0
- ? [ JSON.parse(JSON.stringify(saveKingPos)) ]
- : [ ];
- this.undo(move);
- return res;
+ // Square (x,y) must be surroundable by two enemy pieces,
+ // and one of them at least should be a pawn (moving).
+ const dirs = [ [1,0],[0,1] ];
+ const steps = V.steps[V.ROOK];
+ for (let dir of dirs)
+ {
+ const [i1,j1] = [x-dir[0],y-dir[1]]; //"before"
+ const [i2,j2] = [x+dir[0],y+dir[1]]; //"after"
+ if (V.OnBoard(i1,j1) && V.OnBoard(i2,j2))
+ {
+ if ((this.board[i1][j1]!=V.EMPTY && colors.includes(this.getColor(i1,j1))
+ && this.board[i2][j2]==V.EMPTY)
+ ||
+ (this.board[i2][j2]!=V.EMPTY && colors.includes(this.getColor(i2,j2))
+ && this.board[i1][j1]==V.EMPTY))
+ {
+ // Search a movable enemy pawn landing on the empty square
+ for (let step of steps)
+ {
+ let [ii,jj] = (this.board[i1][j1]==V.EMPTY ? [i1,j1] : [i2,j2]);
+ let [i3,j3] = [ii+step[0],jj+step[1]];
+ while (V.OnBoard(i3,j3) && this.board[i3][j3]==V.EMPTY)
+ {
+ i3 += step[0];
+ j3 += step[1];
+ }
+ if (V.OnBoard(i3,j3) && colors.includes(this.getColor(i3,j3))
+ && this.getPiece(i3,j3) == V.PAWN && !this.isImmobilized([i3,j3]))
+ {
+ return true;
+ }
+ }
+ }
+ }
+ }
+ return false;