1 import { ChessRules
, PiPo
} from "@/base_rules";
3 export class MagneticRules
extends ChessRules
{
4 static get HasEnpassant() {
8 getPotentialMovesFrom([x
, y
]) {
9 let standardMoves
= super.getPotentialMovesFrom([x
, y
]);
11 standardMoves
.forEach(m
=> {
12 let newMove_s
= this.applyMagneticLaws(m
);
13 if (newMove_s
.length
== 1) moves
.push(newMove_s
[0]);
15 else moves
= moves
.concat(newMove_s
);
20 // Complete a move with magnetic actions
21 // TODO: job is done multiple times for (normal) promotions.
22 applyMagneticLaws(move) {
23 // Exception: kings are not charged
24 if (move.appear
[0].p
== V
.KING
&& move.appear
.length
== 1) return [move];
25 // If castling, rook is charged:
26 const aIdx
= move.appear
[0].p
!= V
.KING
? 0 : 1;
27 const [x
, y
] = [move.appear
[aIdx
].x
, move.appear
[aIdx
].y
];
28 const color
= this.turn
;
29 const lastRank
= color
== "w" ? 0 : 7;
30 const standardMove
= JSON
.parse(JSON
.stringify(move));
31 this.play(standardMove
);
38 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
39 while (V
.OnBoard(i
, j
)) {
40 if (this.board
[i
][j
] != V
.EMPTY
) {
41 // Found something. Same color or not?
42 if (this.getColor(i
, j
) != color
) {
45 (Math
.abs(i
- x
) >= 2 || Math
.abs(j
- y
) >= 2) &&
46 this.getPiece(i
, j
) != V
.KING
50 p: this.getPiece(i
, j
),
51 c: this.getColor(i
, j
),
58 p: this.getPiece(i
, j
),
59 c: this.getColor(i
, j
),
67 if (this.getPiece(i
, j
) != V
.KING
) {
68 // Push it until we meet an obstacle or edge of the board
69 let [ii
, jj
] = [i
+ step
[0], j
+ step
[1]];
70 while (V
.OnBoard(ii
, jj
)) {
71 if (this.board
[ii
][jj
] != V
.EMPTY
) break;
77 if (Math
.abs(ii
- i
) >= 1 || Math
.abs(jj
- j
) >= 1) {
80 p: this.getPiece(i
, j
),
81 c: this.getColor(i
, j
),
88 p: this.getPiece(i
, j
),
89 c: this.getColor(i
, j
),
103 this.undo(standardMove
);
105 // Scan move for pawn (max 1) on 8th rank
106 for (let i
= 1; i
< move.appear
.length
; i
++) {
108 move.appear
[i
].p
== V
.PAWN
&&
109 move.appear
[i
].c
== color
&&
110 move.appear
[i
].x
== lastRank
112 move.appear
[i
].p
= V
.ROOK
;
114 for (let piece
of [V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]) {
115 let cmove
= JSON
.parse(JSON
.stringify(move));
116 cmove
.appear
[i
].p
= piece
;
119 // Swap appear[i] and appear[0] for moves presentation
120 // (TODO: this is awkward)
122 let tmp
= m
.appear
[0];
123 m
.appear
[0] = m
.appear
[i
];
129 if (moves
.length
== 0)
130 //no pawn on 8th rank
136 if (this.kingPos
[this.turn
][0] < 0) return false;
137 return true; //TODO: is it right?
141 // There are no checks
150 super.postPlay(move);
151 const c
= move.vanish
[0].c
;
152 if (move.vanish
.length
>= 2 && move.vanish
[1].p
== V
.KING
) {
153 // We took opponent king !
154 const oppCol
= V
.GetOppCol(c
);
155 this.kingPos
[oppCol
] = [-1, -1];
156 this.castleFlags
[oppCol
] = [8, 8];
158 // Did we magnetically move our (init) rooks or opponents' ones ?
159 const firstRank
= c
== "w" ? 7 : 0;
160 const oppFirstRank
= 7 - firstRank
;
161 const oppCol
= V
.GetOppCol(c
);
162 move.vanish
.forEach(psq
=> {
164 psq
.x
== firstRank
&&
165 this.castleFlags
[c
].includes(psq
.y
)
167 this.castleFlags
[c
][psq
.y
== this.castleFlags
[c
][0] ? 0 : 1] = 8;
170 psq
.x
== oppFirstRank
&&
171 this.castleFlags
[oppCol
].includes(psq
.y
)
173 const flagIdx
= (psq
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
174 this.castleFlags
[oppCol
][flagIdx
] = 8;
180 super.postUndo(move);
181 const c
= move.vanish
[0].c
;
182 const oppCol
= V
.GetOppCol(c
);
183 if (this.kingPos
[oppCol
][0] < 0) {
184 // Last move took opponent's king
185 for (let psq
of move.vanish
) {
187 this.kingPos
[oppCol
] = [psq
.x
, psq
.y
];
195 const color
= this.turn
;
196 const kp
= this.kingPos
[color
];
199 return color
== "w" ? "0-1" : "1-0";
200 if (this.atLeastOneMove())
203 return "1/2"; //no moves but kings still there
206 static get THRESHOLD_MATE() {
207 return 500; //checkmates evals may be slightly below 1000
210 static get SEARCH_DEPTH() {