- const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
- const kp = this.kingPos[color];
- const oppCol = V.GetOppCol(color);
- for (let step of steps)
- {
- let [i,j] = [x+step[0],y+step[1]];
- if (!!computer && (i<x || (i==x && j<y)))
- continue; //only switch with superior indices
- if (V.OnBoard(i,j) && this.board[i][j]!=V.EMPTY
- && this.getColor(i,j)==color && this.getPiece(i,j)!=piece
- // No switching under check (theoretically non-king pieces could, but not)
- && !this.isAttacked(kp, [oppCol]))
- {
- let switchMove_s = this.getSwitchMove_s([x,y],[i,j]);
- if (switchMove_s.length == 1)
- moves.push(switchMove_s[0]);
- else //promotion
- moves = moves.concat(switchMove_s);
- }
- }
+ const color = this.turn;
+ const oppCol = V.GetOppCol(color);
+ const kp = this.kingPos[color];
+ // Add switches (if not under check, from anything but the king)
+ if (piece != V.KING && !this.isAttacked(kp, oppCol)) {
+ const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
+ for (let step of steps) {
+ const [i, j] = [x+step[0], y+step[1]];
+ if (
+ V.OnBoard(i, j) &&
+ this.board[i][j] != V.EMPTY &&
+ this.getColor(i,j) == color &&
+ this.getPiece(i,j) != piece
+ ) {
+ const switchMove_s = this.getSwitchMove_s([x,y], [i,j]);
+ Array.prototype.push.apply(moves, switchMove_s);
+ }
+ }
+ }