| 1 | class SwitchingRules extends ChessRules |
| 2 | { |
| 3 | // Build switch move between squares x1,y1 and x2,y2 |
| 4 | getSwitchMove_s([x1,y1],[x2,y2]) |
| 5 | { |
| 6 | |
| 7 | const c = this.getColor(x1,y1); //same as color at square 2 |
| 8 | const p1 = this.getPiece(x1,y1); |
| 9 | const p2 = this.getPiece(x2,y2); |
| 10 | let move = new Move({ |
| 11 | appear: [ |
| 12 | new PiPo({x:x2,y:y2,c:c,p:p1}), |
| 13 | new PiPo({x:x1,y:y1,c:c,p:p2}) |
| 14 | ], |
| 15 | vanish: [ |
| 16 | new PiPo({x:x1,y:y1,c:c,p:p1}), |
| 17 | new PiPo({x:x2,y:y2,c:c,p:p2}) |
| 18 | ], |
| 19 | start: {x:x1,y:y1}, |
| 20 | end: {x:x2,y:y2} |
| 21 | }); |
| 22 | // Move completion: promote switched pawns (as in Magnetic) |
| 23 | const sizeX = VariantRules.size[0]; |
| 24 | const lastRank = (c == "w" ? 0 : sizeX-1); |
| 25 | const V = VariantRules; |
| 26 | let moves = []; |
| 27 | if (p1==V.PAWN && x2==lastRank) //TODO: also the case p2==V.PAWN and x1==lastRank! see Magnetic chess |
| 28 | { |
| 29 | move.appear[0].p = V.ROOK; |
| 30 | moves.push(move); |
| 31 | for (let piece of [V.KNIGHT, V.BISHOP, V.QUEEN]) |
| 32 | { |
| 33 | let cmove = JSON.parse(JSON.stringify(move)); |
| 34 | cmove.appear[0].p = piece; |
| 35 | moves.push(cmove); |
| 36 | } |
| 37 | } |
| 38 | else //other cases |
| 39 | moves.push(move); |
| 40 | return moves; |
| 41 | } |
| 42 | |
| 43 | getPotentialMovesFrom([x,y]) |
| 44 | { |
| 45 | let moves = super.getPotentialMovesFrom([x,y]); |
| 46 | // Add switches: |
| 47 | const V = VariantRules; |
| 48 | const color = this.turn; |
| 49 | const piece = this.getPiece(x,y); |
| 50 | const [sizeX,sizeY] = V.size; |
| 51 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
| 52 | const kp = this.kingPos[color]; |
| 53 | const oppCol = this.getOppCol(color); |
| 54 | for (let step of steps) |
| 55 | { |
| 56 | let [i,j] = [x+step[0],y+step[1]]; |
| 57 | if (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j]!=V.EMPTY |
| 58 | && this.getColor(i,j)==color && this.getPiece(i,j)!=piece |
| 59 | // No switching under check (theoretically non-king pieces could, but not) |
| 60 | && !this.isAttacked(kp, [oppCol])) |
| 61 | { |
| 62 | let switchMove_s = this.getSwitchMove_s([x,y],[i,j]); |
| 63 | if (switchMove_s.length == 1) |
| 64 | moves.push(switchMove_s[0]); |
| 65 | else //promotion |
| 66 | moves = moves.concat(switchMove_s); |
| 67 | } |
| 68 | } |
| 69 | return moves; |
| 70 | } |
| 71 | |
| 72 | static get SEARCH_DEPTH() { return 2; } //branching factor is quite high |
| 73 | } |