1 class LoserRules
extends ChessRules
5 const epSq
= this.moves
.length
> 0 ? this.getEpSquare(this.lastMove
) : undefined;
6 this.epSquares
= [ epSq
];
11 // No castling, hence no flags; but flags defined for compatibility
12 this.castleFlags
= { "w":[false,false], "b":[false,false] };
15 getPotentialPawnMoves([x
,y
])
17 let moves
= super.getPotentialPawnMoves([x
,y
]);
19 // Complete with promotion(s) into king, if possible
20 const color
= this.turn
;
21 const shift
= (color
== "w" ? -1 : 1);
22 const lastRank
= (color
== "w" ? 0 : V
.size
.x
-1);
23 if (x
+shift
== lastRank
)
26 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
27 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:V
.KING
}));
29 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
30 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
32 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1], {c:color
,p:V
.KING
}));
34 if (y
<V
.size
.y
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
35 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
37 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:color
,p:V
.KING
}));
44 getPotentialKingMoves(sq
)
46 return this.getSlideNJumpMoves(sq
,
47 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]), "oneStep");
50 // Stop at the first capture found (if any)
53 const color
= this.turn
;
54 const oppCol
= this.getOppCol(color
);
55 for (let i
=0; i
<V
.size
.x
; i
++)
57 for (let j
=0; j
<V
.size
.y
; j
++)
59 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
,j
) != oppCol
)
61 const moves
= this.getPotentialMovesFrom([i
,j
]);
64 for (let k
=0; k
<moves
.length
; k
++)
66 if (moves
[k
].vanish
.length
==2 && this.filterValid([moves
[k
]]).length
> 0)
76 // Trim all non-capturing moves
77 static KeepCaptures(moves
)
79 return moves
.filter(m
=> { return m
.vanish
.length
== 2; });
82 getPossibleMovesFrom(sq
)
84 let moves
= this.filterValid( this.getPotentialMovesFrom(sq
) );
85 // This is called from interface: we need to know if a capture is possible
86 if (this.atLeastOneCapture())
87 moves
= V
.KeepCaptures(moves
);
93 let moves
= super.getAllValidMoves();
94 if (moves
.some(m
=> { return m
.vanish
.length
== 2; }))
95 moves
= V
.KeepCaptures(moves
);
101 return false; //No notion of check
104 getCheckSquares(move)
110 updateVariables(move) { }
111 unupdateVariables(move) { }
120 // No valid move: you win!
121 return this.turn
== "w" ? "1-0" : "0-1";
124 static get VALUES() { //experimental...
135 static get SEARCH_DEPTH() { return 4; }
139 return - super.evalPosition(); //better with less material
142 static GenRandInitFen()
144 let pieces
= { "w": new Array(8), "b": new Array(8) };
145 // Shuffle pieces on first and last rank
146 for (let c
of ["w","b"])
148 let positions
= _
.range(8);
150 // Get random squares for bishops
151 let randIndex
= 2 * _
.random(3);
152 let bishop1Pos
= positions
[randIndex
];
153 // The second bishop must be on a square of different color
154 let randIndex_tmp
= 2 * _
.random(3) + 1;
155 let bishop2Pos
= positions
[randIndex_tmp
];
156 // Remove chosen squares
157 positions
.splice(Math
.max(randIndex
,randIndex_tmp
), 1);
158 positions
.splice(Math
.min(randIndex
,randIndex_tmp
), 1);
160 // Get random squares for knights
161 randIndex
= _
.random(5);
162 let knight1Pos
= positions
[randIndex
];
163 positions
.splice(randIndex
, 1);
164 randIndex
= _
.random(4);
165 let knight2Pos
= positions
[randIndex
];
166 positions
.splice(randIndex
, 1);
168 // Get random square for queen
169 randIndex
= _
.random(3);
170 let queenPos
= positions
[randIndex
];
171 positions
.splice(randIndex
, 1);
173 // Random square for king (no castle)
174 randIndex
= _
.random(2);
175 let kingPos
= positions
[randIndex
];
176 positions
.splice(randIndex
, 1);
178 // Rooks positions are now fixed
179 let rook1Pos
= positions
[0];
180 let rook2Pos
= positions
[1];
182 // Finally put the shuffled pieces in the board array
183 pieces
[c
][rook1Pos
] = 'r';
184 pieces
[c
][knight1Pos
] = 'n';
185 pieces
[c
][bishop1Pos
] = 'b';
186 pieces
[c
][queenPos
] = 'q';
187 pieces
[c
][kingPos
] = 'k';
188 pieces
[c
][bishop2Pos
] = 'b';
189 pieces
[c
][knight2Pos
] = 'n';
190 pieces
[c
][rook2Pos
] = 'r';
192 return pieces
["b"].join("") +
193 "/pppppppp/8/8/8/8/PPPPPPPP/" +
194 pieces
["w"].join("").toUpperCase() +
195 " 0000"; //add flags (TODO?!)