229c7b35b30512b564104215d43bcb87bf6e38e6
1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
3 export class SwitchingRules
extends ChessRules
{
4 // Build switch move between squares x1,y1 and x2,y2
5 getSwitchMove_s([x1
, y1
], [x2
, y2
]) {
6 const c
= this.getColor(x1
, y1
); //same as color at square 2
7 const p1
= this.getPiece(x1
, y1
);
8 const p2
= this.getPiece(x2
, y2
);
11 new PiPo({ x: x2
, y: y2
, c: c
, p: p1
}),
12 new PiPo({ x: x1
, y: y1
, c: c
, p: p2
})
15 new PiPo({ x: x1
, y: y1
, c: c
, p: p1
}),
16 new PiPo({ x: x2
, y: y2
, c: c
, p: p2
})
19 // Move completion: promote switched pawns (as in Magnetic)
20 const lastRank
= (c
== "w" ? 0 : V
.size
.x
- 1);
22 if ((p1
== V
.PAWN
&& x2
== lastRank
) || (p2
== V
.PAWN
&& x1
== lastRank
)) {
23 const idx
= (p1
== V
.PAWN
? 0 : 1);
24 move.appear
[idx
].p
= V
.ROOK
;
26 for (let piece
of [V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]) {
27 let cmove
= JSON
.parse(JSON
.stringify(move));
28 cmove
.appear
[idx
].p
= piece
;
32 // Swap moves[i].appear[0] and [1] for moves presentation [TODO...]
34 let tmp
= m
.appear
[0];
35 m
.appear
[0] = m
.appear
[1];
46 getPotentialMovesFrom([x
,y
]) {
47 let moves
= super.getPotentialMovesFrom([x
,y
]);
48 const piece
= this.getPiece(x
,y
);
49 const color
= this.turn
;
50 const oppCol
= V
.GetOppCol(color
);
51 const kp
= this.kingPos
[color
];
52 // Add switches (if not under check, from anything but the king)
53 if (piece
!= V
.KING
&& !this.isAttacked(kp
, oppCol
)) {
54 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
55 for (let step
of steps
) {
56 const [i
, j
] = [x
+step
[0], y
+step
[1]];
59 this.board
[i
][j
] != V
.EMPTY
&&
60 this.getColor(i
,j
) == color
&&
61 this.getPiece(i
,j
) != piece
63 const switchMove_s
= this.getSwitchMove_s([x
,y
], [i
,j
]);
64 Array
.prototype.push
.apply(moves
, switchMove_s
);
72 // Did some king move?
73 move.appear
.forEach(a
=> {
75 this.kingPos
[a
.c
] = [a
.x
, a
.y
];
76 this.castleFlags
[a
.c
] = [V
.size
.y
, V
.size
.y
];
79 for (let coords
of [move.start
, move.end
]) {
81 Object
.keys(firstRank
).includes(coords
.x
) &&
82 this.castleFlags
[firstRank
[coords
.x
]].includes(coords
.y
)
84 const c
= firstRank
[coords
.x
];
85 const flagIdx
= (coords
.y
== this.castleFlags
[c
][0] ? 0 : 1);
86 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
92 // Did some king move?
93 move.vanish
.forEach(v
=> {
94 if (v
.p
== V
.KING
) this.kingPos
[v
.c
] = [v
.x
, v
.y
];
98 static get SEARCH_DEPTH() {
99 // Branching factor is quite high
103 getAllPotentialMoves() {
104 // Since this function is used only for computer play,
105 // remove duplicate switches:
106 return super.getAllPotentialMoves().filter(m
=> {
108 m
.appear
.length
== 1 ||
109 (move.appear
[0].p
== V
.KING
&& move.appear
[1].p
== V
.ROOK
) ||
110 (m
.appear
[1].x
<= m
.vanish
[1].x
&& m
.appear
[1].y
<= m
.vanish
[1].y
)
116 if (move.appear
.length
== 1)
118 return super.getNotation(move);
119 if (move.appear
[0].p
== V
.KING
&& move.appear
[1].p
== V
.ROOK
)
121 return (move.end
.y
< move.start
.y
? "0-0-0" : "0-0");
123 return "S" + V
.CoordsToSquare(move.start
) + V
.CoordsToSquare(move.end
);