1 class DarkRules
extends ChessRules
3 // Standard rules, in the shadow
6 super.setOtherVariables(fen
);
7 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
9 "w": doubleArray(sizeX
,sizeY
),
10 "b": doubleArray(sizeX
,sizeY
)
12 // Setup enlightened: squares reachable by each side
13 // (TODO: one side would be enough ?)
14 this.updateEnlightened();
19 for (let i
=0; i
<V
.size
.x
; i
++)
21 for (let j
=0; j
<V
.size
.y
; j
++)
23 this.enlightened
["w"][i
][j
] = false;
24 this.enlightened
["b"][i
][j
] = false;
27 const pawnShift
= {"w":-1, "b":1};
28 // Initialize with pieces positions (which are seen)
29 for (let i
=0; i
<V
.size
.x
; i
++)
31 for (let j
=0; j
<V
.size
.y
; j
++)
33 if (this.board
[i
][j
] != V
.EMPTY
)
35 const color
= this.getColor(i
,j
);
36 this.enlightened
[color
][i
][j
] = true;
37 // Add potential squares visible by "impossible pawn capture"
38 if (this.getPiece(i
,j
) == V
.PAWN
)
40 for (let shiftY
of [-1,1])
42 if (V
.OnBoard(i
+pawnShift
[color
],j
+shiftY
)
43 && this.board
[i
+pawnShift
[color
]][j
+shiftY
] == V
.EMPTY
)
45 this.enlightened
[color
][i
+pawnShift
[color
]][j
+shiftY
] = true;
52 const currentTurn
= this.turn
;
54 const movesWhite
= this.getAllValidMoves();
56 const movesBlack
= this.getAllValidMoves();
57 this.turn
= currentTurn
;
58 for (let move of movesWhite
)
59 this.enlightened
["w"][move.end
.x
][move.end
.y
] = true;
60 for (let move of movesBlack
)
61 this.enlightened
["b"][move.end
.x
][move.end
.y
] = true;
64 // Has to be redefined to avoid an infinite loop
67 const color
= this.turn
;
68 const oppCol
= V
.GetOppCol(color
);
69 let potentialMoves
= [];
70 for (let i
=0; i
<V
.size
.x
; i
++)
72 for (let j
=0; j
<V
.size
.y
; j
++)
74 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
,j
) == color
)
75 Array
.prototype.push
.apply(potentialMoves
, this.getPotentialMovesFrom([i
,j
]));
78 return potentialMoves
; //because there are no checks
83 if (this.kingPos
[this.turn
][0] < 0)
85 return true; //TODO: is it right?
90 return false; //there is no check
93 getCheckSquares(color
)
100 super.updateVariables(move);
101 if (move.vanish
.length
>= 2 && move.vanish
[1].p
== V
.KING
)
103 // We took opponent king !
104 this.kingPos
[this.turn
] = [-1,-1];
107 // Update moves for both colors:
108 this.updateEnlightened();
111 unupdateVariables(move)
113 super.unupdateVariables(move);
114 const c
= move.vanish
[0].c
;
115 const oppCol
= V
.GetOppCol(c
);
116 if (this.kingPos
[oppCol
][0] < 0)
118 // Last move took opponent's king
119 for (let psq
of move.vanish
)
123 this.kingPos
[oppCol
] = [psq
.x
, psq
.y
];
129 // Update moves for both colors:
130 this.updateEnlightened();
135 // No valid move: our king disappeared
136 return this.turn
== "w" ? "0-1" : "1-0";
139 static get THRESHOLD_MATE()
141 return 500; //checkmates evals may be slightly below 1000
144 // In this special situation, we just look 1 half move ahead
147 const maxeval
= V
.INFINITY
;
148 const color
= this.turn
;
149 const oppCol
= V
.GetOppCol(color
);
150 const pawnShift
= (color
== "w" ? -1 : 1);
151 const kp
= this.kingPos
[color
];
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 let step
= undefined;
160 if (piece
== V
.BISHOP
)
162 if (Math
.abs(kp
[0] - i
) == Math
.abs(kp
[1] - j
))
166 (i
-kp
[0]) / Math
.abs(i
-kp
[0]),
167 (j
-kp
[1]) / Math
.abs(j
-kp
[1])
171 else if (piece
== V
.ROOK
)
174 step
= [0, (j
-kp
[1]) / Math
.abs(j
-kp
[1])];
176 step
= [(i
-kp
[0]) / Math
.abs(i
-kp
[0]), 0];
180 // Check for obstacles
181 let obstacle
= false;
183 let x
=kp
[0]+step
[0], y
=kp
[1]+step
[1];
185 x
+= step
[0], y
+= step
[1])
187 if (myLight
[x
][y
] && this.board
[x
][y
] != V
.EMPTY
)
198 // Do I see something which can take my king ?
199 const kingThreats
= () => {
200 for (let i
=0; i
<V
.size
.x
; i
++)
202 for (let j
=0; j
<V
.size
.y
; j
++)
204 if (myLight
[i
][j
] && this.board
[i
][j
] != V
.EMPTY
205 && this.getColor(i
,j
) != color
)
207 switch (this.getPiece(i
,j
))
210 if (kp
[0] + pawnShift
== i
&& Math
.abs(kp
[1]-j
) == 1)
214 if ((Math
.abs(kp
[0] - i
) == 2 && Math
.abs(kp
[1] - j
) == 1) ||
215 (Math
.abs(kp
[0] - i
) == 1 && Math
.abs(kp
[1] - j
) == 2))
221 if (Math
.abs(kp
[0] - i
) == 1 && Math
.abs(kp
[1] - j
) == 1)
225 if (sliderTake([i
,j
], V
.BISHOP
))
229 if (sliderTake([i
,j
], V
.ROOK
))
233 if (sliderTake([i
,j
], V
.BISHOP
) || sliderTake([i
,j
], V
.ROOK
))
243 let moves
= this.getAllValidMoves();
244 for (let move of moves
)
247 if (this.kingPos
[oppCol
][0] >= 0 && kingThreats())
249 // We didn't take opponent king, and our king will be captured: bad
250 move.eval
= -maxeval
;
256 move.eval
= 0; //a priori...
258 // Can I take something ? If yes, do it if it seems good...
259 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= color
) //avoid castle
261 const myPieceVal
= V
.VALUES
[move.appear
[0].p
];
262 const hisPieceVal
= V
.VALUES
[move.vanish
[1].p
];
263 if (myPieceVal
<= hisPieceVal
)
264 move.eval
= hisPieceVal
- myPieceVal
+ 2; //favor captures
267 // Taking a pawn with minor piece,
268 // or minor piece or pawn with a rook,
269 // or anything but a queen with a queen,
270 // or anything with a king.
271 // ==> Do it at random, although
272 // this is clearly inferior to what a human can deduce...
273 move.eval
= (Math
.random() < 0.5 ? 1 : -1);
278 // TODO: also need to implement the case when an opponent piece (in light)
279 // is threatening something - maybe not the king, but e.g. pawn takes rook.
281 moves
.sort((a
,b
) => b
.eval
- a
.eval
);
282 let candidates
= [0];
283 for (let j
=1; j
<moves
.length
&& moves
[j
].eval
== moves
[0].eval
; j
++)
285 return moves
[_
.sample(candidates
, 1)];