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 const pawnShift
= {"w":-1, "b":1};
20 // Initialize with pieces positions (which are seen)
21 for (let i
=0; i
<V
.size
.x
; i
++)
23 for (let j
=0; j
<V
.size
.y
; j
++)
25 this.enlightened
["w"][i
][j
] = false;
26 this.enlightened
["b"][i
][j
] = false;
27 if (this.board
[i
][j
] != V
.EMPTY
)
29 const color
= this.getColor(i
,j
);
30 this.enlightened
[color
][i
][j
] = true;
31 // Add potential squares visible by "impossible pawn capture"
32 if (this.getPiece(i
,j
) == V
.PAWN
)
34 for (let shiftY
of [-1,1])
36 if (V
.OnBoard(i
+pawnShift
[color
],j
+shiftY
)
37 && this.board
[i
+pawnShift
[color
]][j
+shiftY
] == V
.EMPTY
)
39 this.enlightened
[color
][i
+pawnShift
[color
]][j
+shiftY
] = true;
46 const currentTurn
= this.turn
;
48 const movesWhite
= this.getAllValidMoves();
50 const movesBlack
= this.getAllValidMoves();
51 this.turn
= currentTurn
;
52 for (let move of movesWhite
)
53 this.enlightened
["w"][move.end
.x
][move.end
.y
] = true;
54 for (let move of movesBlack
)
55 this.enlightened
["b"][move.end
.x
][move.end
.y
] = true;
58 // Has to be redefined to avoid an infinite loop
61 const color
= this.turn
;
62 const oppCol
= this.getOppCol(color
);
63 let potentialMoves
= [];
64 for (let i
=0; i
<V
.size
.x
; i
++)
66 for (let j
=0; j
<V
.size
.y
; j
++)
68 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
,j
) == color
)
69 Array
.prototype.push
.apply(potentialMoves
, this.getPotentialMovesFrom([i
,j
]));
72 return potentialMoves
; //because there are no checks
77 if (this.kingPos
[this.turn
][0] < 0)
79 return true; //TODO: is it right?
84 return false; //there is no check
87 getCheckSquares(color
)
94 super.updateVariables(move);
95 if (move.vanish
.length
>= 2 && move.vanish
[1].p
== V
.KING
)
97 // We took opponent king !
98 this.kingPos
[this.turn
] = [-1,-1];
101 // Update moves for both colors:
102 this.updateEnlightened();
105 unupdateVariables(move)
107 super.unupdateVariables(move);
108 const c
= move.vanish
[0].c
;
109 const oppCol
= this.getOppCol(c
);
110 if (this.kingPos
[oppCol
][0] < 0)
112 // Last move took opponent's king
113 for (let psq
of move.vanish
)
117 this.kingPos
[oppCol
] = [psq
.x
, psq
.y
];
123 // Update moves for both colors:
124 this.updateEnlightened();
129 // No valid move: our king disappeared
130 return this.turn
== "w" ? "0-1" : "1-0";
133 static get THRESHOLD_MATE()
135 return 500; //checkmates evals may be slightly below 1000
138 // In this special situation, we just look 1 half move ahead
141 const maxeval
= V
.INFINITY
;
142 const color
= this.turn
;
143 const oppCol
= this.getOppCol(color
);
144 const pawnShift
= (color
== "w" ? -1 : 1);
145 const kp
= this.kingPos
[color
];
147 // Do not cheat: the current enlightment is all we can see
148 const myLight
= JSON
.parse(JSON
.stringify(this.enlightened
[color
]));
150 // Can a slider on (i,j) apparently take my king?
151 // NOTE: inaccurate because assume yes if some squares are shadowed
152 const sliderTake
= ([i
,j
], piece
) => {
153 let step
= undefined;
154 if (piece
== V
.BISHOP
)
156 if (Math
.abs(kp
[0] - i
) == Math
.abs(kp
[1] - j
))
160 (i
-kp
[0]) / Math
.abs(i
-kp
[0]),
161 (j
-kp
[1]) / Math
.abs(j
-kp
[1])
165 else if (piece
== V
.ROOK
)
168 step
= [0, (j
-kp
[1]) / Math
.abs(j
-kp
[1])];
170 step
= [(i
-kp
[0]) / Math
.abs(i
-kp
[0]), 0];
174 // Check for obstacles
175 let obstacle
= false;
177 let x
=kp
[0]+step
[0], y
=kp
[1]+step
[1];
179 x
+= step
[0], y
+= step
[1])
181 if (myLight
[x
][y
] && this.board
[x
][y
] != V
.EMPTY
)
192 // Do I see something which can take my king ?
193 const kingThreats
= () => {
194 for (let i
=0; i
<V
.size
.x
; i
++)
196 for (let j
=0; j
<V
.size
.y
; j
++)
198 if (myLight
[i
][j
] && this.board
[i
][j
] != V
.EMPTY
199 && this.getColor(i
,j
) != color
)
201 switch (this.getPiece(i
,j
))
204 if (kp
[0] + pawnShift
== i
&& Math
.abs(kp
[1]-j
) == 1)
208 if ((Math
.abs(kp
[0] - i
) == 2 && Math
.abs(kp
[1] - j
) == 1) ||
209 (Math
.abs(kp
[0] - i
) == 1 && Math
.abs(kp
[1] - j
) == 2))
215 if (Math
.abs(kp
[0] - i
) == 1 && Math
.abs(kp
[1] - j
) == 1)
219 if (sliderTake([i
,j
], V
.BISHOP
))
223 if (sliderTake([i
,j
], V
.ROOK
))
227 if (sliderTake([i
,j
], V
.BISHOP
) || sliderTake([i
,j
], V
.ROOK
))
237 let moves
= this.getAllValidMoves();
238 for (let move of moves
)
241 if (this.kingPos
[oppCol
][0] >= 0 && kingThreats())
243 // We didn't take opponent king, and our king will be captured: bad
244 move.eval
= -maxeval
;
250 move.eval
= 0; //a priori...
252 // Can I take something ? If yes, do it if it seems good...
253 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= color
) //avoid castle
255 const myPieceVal
= V
.VALUES
[move.appear
[0].p
];
256 const hisPieceVal
= V
.VALUES
[move.vanish
[1].p
];
257 if (myPieceVal
<= hisPieceVal
)
258 move.eval
= hisPieceVal
- myPieceVal
+ 2; //favor captures
261 // Taking a pawn with minor piece,
262 // or minor piece or pawn with a rook,
263 // or anything but a queen with a queen,
264 // or anything with a king.
265 // ==> Do it at random, although
266 // this is clearly inferior to what a human can deduce...
267 move.eval
= (Math
.random() < 0.5 ? 1 : -1);
272 // TODO: also need to implement the case when an opponent piece (in light)
273 // is threatening something - maybe not the king, but e.g. pawn takes rook.
275 moves
.sort((a
,b
) => b
.eval
- a
.eval
);
276 let candidates
= [0];
277 for (let j
=1; j
<moves
.length
&& moves
[j
].eval
== moves
[0].eval
; j
++)
279 return moves
[_
.sample(candidates
, 1)];
283 const VariantRules
= DarkRules
;