1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export class DarkRules
extends ChessRules
{
6 // Analyse in Dark mode makes no sense
7 static get CanAnalyze() {
11 // Moves are revealed only when game ends
12 static get ShowMoves() {
16 static get SomeHiddenMoves() {
20 setOtherVariables(fen
) {
21 super.setOtherVariables(fen
);
22 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
24 w: ArrayFun
.init(sizeX
, sizeY
),
25 b: ArrayFun
.init(sizeX
, sizeY
)
27 // Setup enlightened: squares reachable by each side
28 // (TODO: one side would be enough ?)
29 this.updateEnlightened();
33 for (let i
= 0; i
< V
.size
.x
; i
++) {
34 for (let j
= 0; j
< V
.size
.y
; j
++) {
35 this.enlightened
["w"][i
][j
] = false;
36 this.enlightened
["b"][i
][j
] = false;
39 const pawnShift
= { w: -1, b: 1 };
40 // Initialize with pieces positions (which are seen)
41 for (let i
= 0; i
< V
.size
.x
; i
++) {
42 for (let j
= 0; j
< V
.size
.y
; j
++) {
43 if (this.board
[i
][j
] != V
.EMPTY
) {
44 const c
= this.getColor(i
, j
);
45 this.enlightened
[c
][i
][j
] = true;
46 // Add potential squares visible by "impossible pawn capture"
47 if (this.getPiece(i
, j
) == V
.PAWN
) {
48 for (let shiftY
of [-1, 1]) {
50 V
.OnBoard(i
+ pawnShift
[c
], j
+ shiftY
) &&
51 this.board
[i
+ pawnShift
[c
]][j
+ shiftY
] == V
.EMPTY
53 this.enlightened
[c
][i
+ pawnShift
[c
]][j
+ shiftY
] = true;
60 const currentTurn
= this.turn
;
62 const movesWhite
= this.getAllValidMoves();
64 const movesBlack
= this.getAllValidMoves();
65 this.turn
= currentTurn
;
66 for (let move of movesWhite
)
67 this.enlightened
["w"][move.end
.x
][move.end
.y
] = true;
68 for (let move of movesBlack
)
69 this.enlightened
["b"][move.end
.x
][move.end
.y
] = true;
70 // Include en-passant capturing square if any:
71 let moves
= currentTurn
== "w" ? movesWhite : movesBlack
;
72 for (let m
of moves
) {
74 m
.appear
[0].p
== V
.PAWN
&&
75 m
.vanish
.length
== 2 &&
76 m
.vanish
[1].x
!= m
.end
.x
78 const psq
= m
.vanish
[1];
79 this.enlightened
[currentTurn
][psq
.x
][psq
.y
] = true;
86 // Used in the interface
90 // Has to be redefined to avoid an infinite loop
92 const color
= this.turn
;
93 let potentialMoves
= [];
94 for (let i
= 0; i
< V
.size
.x
; i
++) {
95 for (let j
= 0; j
< V
.size
.y
; j
++) {
96 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) == color
)
97 Array
.prototype.push
.apply(
99 this.getPotentialMovesFrom([i
, j
])
103 return potentialMoves
; //because there are no checks
111 super.postPlay(move);
112 if (move.vanish
.length
>= 2 && move.vanish
[1].p
== V
.KING
)
113 // We took opponent king (because if castle vanish[1] is a rook)
114 this.kingPos
[this.turn
] = [-1, -1];
116 // Update lights for both colors:
117 this.updateEnlightened();
121 super.postUndo(move);
122 const c
= move.vanish
[0].c
;
123 const oppCol
= V
.GetOppCol(c
);
124 if (this.kingPos
[oppCol
][0] < 0)
125 // Last move took opponent's king:
126 this.kingPos
[oppCol
] = [move.vanish
[1].x
, move.vanish
[1].y
];
128 // Update lights for both colors:
129 this.updateEnlightened();
133 const color
= this.turn
;
134 const kp
= this.kingPos
[color
];
137 return color
== "w" ? "0-1" : "1-0";
138 // Assume that stalemate is impossible (I think so. Would need proof...)
142 static get THRESHOLD_MATE() {
143 return 500; //checkmates evals may be slightly below 1000
146 // In this special situation, we just look 1 half move ahead
148 const maxeval
= V
.INFINITY
;
149 const color
= this.turn
;
150 const oppCol
= V
.GetOppCol(color
);
151 const pawnShift
= color
== "w" ? -1 : 1;
153 // Do not cheat: the current enlightment is all we can see
154 const myLight
= JSON
.parse(JSON
.stringify(this.enlightened
[color
]));
156 // Can a slider on (i,j) apparently take my king?
157 // NOTE: inaccurate because assume yes if some squares are shadowed
158 const sliderTake
= ([i
, j
], piece
) => {
159 const kp
= this.kingPos
[color
];
160 let step
= undefined;
161 if (piece
== V
.BISHOP
) {
162 if (Math
.abs(kp
[0] - i
) == Math
.abs(kp
[1] - j
)) {
164 (i
- kp
[0]) / Math
.abs(i
- kp
[0]),
165 (j
- kp
[1]) / Math
.abs(j
- kp
[1])
168 } else if (piece
== V
.ROOK
) {
169 if (kp
[0] == i
) step
= [0, (j
- kp
[1]) / Math
.abs(j
- kp
[1])];
170 else if (kp
[1] == j
) step
= [(i
- kp
[0]) / Math
.abs(i
- kp
[0]), 0];
172 if (!step
) return false;
173 // Check for obstacles
174 let obstacle
= false;
176 let x
= kp
[0] + step
[0], y
= kp
[1] + step
[1];
178 x
+= step
[0], y
+= step
[1]
180 if (myLight
[x
][y
] && this.board
[x
][y
] != V
.EMPTY
) {
185 if (!obstacle
) return true;
189 // Do I see something which can take my king ?
190 const kingThreats
= () => {
191 const kp
= this.kingPos
[color
];
192 for (let i
= 0; i
< V
.size
.x
; i
++) {
193 for (let j
= 0; j
< V
.size
.y
; j
++) {
196 this.board
[i
][j
] != V
.EMPTY
&&
197 this.getColor(i
, j
) != color
199 switch (this.getPiece(i
, j
)) {
201 if (kp
[0] + pawnShift
== i
&& Math
.abs(kp
[1] - j
) == 1)
206 (Math
.abs(kp
[0] - i
) == 2 && Math
.abs(kp
[1] - j
) == 1) ||
207 (Math
.abs(kp
[0] - i
) == 1 && Math
.abs(kp
[1] - j
) == 2)
213 if (Math
.abs(kp
[0] - i
) == 1 && Math
.abs(kp
[1] - j
) == 1)
217 if (sliderTake([i
, j
], V
.BISHOP
)) return true;
220 if (sliderTake([i
, j
], V
.ROOK
)) return true;
223 if (sliderTake([i
, j
], V
.BISHOP
) || sliderTake([i
, j
], V
.ROOK
))
233 let moves
= this.getAllValidMoves();
234 for (let move of moves
) {
236 if (this.kingPos
[oppCol
][0] >= 0 && kingThreats()) {
237 // We didn't take opponent king, and our king will be captured: bad
238 move.eval
= -maxeval
;
242 if (move.eval
) continue;
244 move.eval
= 0; //a priori...
246 // Can I take something ? If yes, do it if it seems good...
247 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= color
) {
248 // OK this isn't a castling move
249 const myPieceVal
= V
.VALUES
[move.appear
[0].p
];
250 const hisPieceVal
= V
.VALUES
[move.vanish
[1].p
];
252 if (myPieceVal
<= hisPieceVal
)
253 move.eval
= hisPieceVal
- myPieceVal
+ 1;
255 // Taking a pawn with minor piece,
256 // or minor piece or pawn with a rook,
257 // or anything but a queen with a queen,
258 // or anything with a king.
259 move.eval
= hisPieceVal
- myPieceVal
;
260 //Math.random() < 0.5 ? 1 : -1;
265 // TODO: also need to implement the case when an opponent piece (in light)
266 // is threatening something - maybe not the king, but e.g. pawn takes rook.
268 moves
.sort((a
, b
) => b
.eval
- a
.eval
);
269 let candidates
= [0];
270 for (let j
= 1; j
< moves
.length
&& moves
[j
].eval
== moves
[0].eval
; j
++)
272 return moves
[candidates
[randInt(candidates
.length
)]];