1 class LoserRules
extends ChessRules
5 const epSq
= this.moves
.length
> 0 ? this.getEpSquare(this.lastMove
) : undefined;
6 this.epSquares
= [ epSq
];
9 static IsGoodFlags(flags
)
11 return true; //anything is good: no flags
16 // No castling, hence no flags; but flags defined for compatibility
17 this.castleFlags
= { "w":[false,false], "b":[false,false] };
20 getPotentialPawnMoves([x
,y
])
22 let moves
= super.getPotentialPawnMoves([x
,y
]);
24 // Complete with promotion(s) into king, if possible
25 const color
= this.turn
;
26 const shift
= (color
== "w" ? -1 : 1);
27 const lastRank
= (color
== "w" ? 0 : V
.size
.x
-1);
28 if (x
+shift
== lastRank
)
31 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
32 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:V
.KING
}));
34 if (y
>0 && 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
}));
39 if (y
<V
.size
.y
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
40 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
42 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:color
,p:V
.KING
}));
49 getPotentialKingMoves(sq
)
51 return this.getSlideNJumpMoves(sq
,
52 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]), "oneStep");
55 // Stop at the first capture found (if any)
58 const color
= this.turn
;
59 const oppCol
= this.getOppCol(color
);
60 for (let i
=0; i
<V
.size
.x
; i
++)
62 for (let j
=0; j
<V
.size
.y
; j
++)
64 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
,j
) != oppCol
)
66 const moves
= this.getPotentialMovesFrom([i
,j
]);
69 for (let k
=0; k
<moves
.length
; k
++)
71 if (moves
[k
].vanish
.length
==2 && this.filterValid([moves
[k
]]).length
> 0)
81 // Trim all non-capturing moves
82 static KeepCaptures(moves
)
84 return moves
.filter(m
=> { return m
.vanish
.length
== 2; });
87 getPossibleMovesFrom(sq
)
89 let moves
= this.filterValid( this.getPotentialMovesFrom(sq
) );
90 // This is called from interface: we need to know if a capture is possible
91 if (this.atLeastOneCapture())
92 moves
= V
.KeepCaptures(moves
);
98 let moves
= super.getAllValidMoves();
99 if (moves
.some(m
=> { return m
.vanish
.length
== 2; }))
100 moves
= V
.KeepCaptures(moves
);
106 return false; //No notion of check
109 getCheckSquares(move)
115 updateVariables(move) { }
116 unupdateVariables(move) { }
125 // No valid move: you win!
126 return this.turn
== "w" ? "1-0" : "0-1";
129 static get VALUES() { //experimental...
140 static get SEARCH_DEPTH() { return 4; }
144 return - super.evalPosition(); //better with less material
147 static GenRandInitFen()
149 let pieces
= { "w": new Array(8), "b": new Array(8) };
150 // Shuffle pieces on first and last rank
151 for (let c
of ["w","b"])
153 let positions
= _
.range(8);
155 // Get random squares for bishops
156 let randIndex
= 2 * _
.random(3);
157 let bishop1Pos
= positions
[randIndex
];
158 // The second bishop must be on a square of different color
159 let randIndex_tmp
= 2 * _
.random(3) + 1;
160 let bishop2Pos
= positions
[randIndex_tmp
];
161 // Remove chosen squares
162 positions
.splice(Math
.max(randIndex
,randIndex_tmp
), 1);
163 positions
.splice(Math
.min(randIndex
,randIndex_tmp
), 1);
165 // Get random squares for knights
166 randIndex
= _
.random(5);
167 let knight1Pos
= positions
[randIndex
];
168 positions
.splice(randIndex
, 1);
169 randIndex
= _
.random(4);
170 let knight2Pos
= positions
[randIndex
];
171 positions
.splice(randIndex
, 1);
173 // Get random square for queen
174 randIndex
= _
.random(3);
175 let queenPos
= positions
[randIndex
];
176 positions
.splice(randIndex
, 1);
178 // Random square for king (no castle)
179 randIndex
= _
.random(2);
180 let kingPos
= positions
[randIndex
];
181 positions
.splice(randIndex
, 1);
183 // Rooks positions are now fixed
184 let rook1Pos
= positions
[0];
185 let rook2Pos
= positions
[1];
187 // Finally put the shuffled pieces in the board array
188 pieces
[c
][rook1Pos
] = 'r';
189 pieces
[c
][knight1Pos
] = 'n';
190 pieces
[c
][bishop1Pos
] = 'b';
191 pieces
[c
][queenPos
] = 'q';
192 pieces
[c
][kingPos
] = 'k';
193 pieces
[c
][bishop2Pos
] = 'b';
194 pieces
[c
][knight2Pos
] = 'n';
195 pieces
[c
][rook2Pos
] = 'r';
197 return pieces
["b"].join("") +
198 "/pppppppp/8/8/8/8/PPPPPPPP/" +
199 pieces
["w"].join("").toUpperCase() +
200 " 0000 w"; //add flags (TODO?!)