1 class LosersRules
extends ChessRules
3 static get HasFlags() { return false; }
5 getPotentialPawnMoves([x
,y
])
7 let moves
= super.getPotentialPawnMoves([x
,y
]);
9 // Complete with promotion(s) into king, if possible
10 const color
= this.turn
;
11 const shift
= (color
== "w" ? -1 : 1);
12 const lastRank
= (color
== "w" ? 0 : V
.size
.x
-1);
13 if (x
+shift
== lastRank
)
16 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
17 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:V
.KING
}));
19 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
20 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
22 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1], {c:color
,p:V
.KING
}));
24 if (y
<V
.size
.y
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
25 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
27 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:color
,p:V
.KING
}));
34 getPotentialKingMoves(sq
)
37 return this.getSlideNJumpMoves(sq
,
38 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]), "oneStep");
41 // Stop at the first capture found (if any)
44 const color
= this.turn
;
45 const oppCol
= this.getOppCol(color
);
46 for (let i
=0; i
<V
.size
.x
; i
++)
48 for (let j
=0; j
<V
.size
.y
; j
++)
50 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
,j
) != oppCol
)
52 const moves
= this.getPotentialMovesFrom([i
,j
]);
55 for (let k
=0; k
<moves
.length
; k
++)
57 if (moves
[k
].vanish
.length
==2 && this.filterValid([moves
[k
]]).length
> 0)
67 // Trim all non-capturing moves
68 static KeepCaptures(moves
)
70 return moves
.filter(m
=> { return m
.vanish
.length
== 2; });
73 getPossibleMovesFrom(sq
)
75 let moves
= this.filterValid( this.getPotentialMovesFrom(sq
) );
76 // This is called from interface: we need to know if a capture is possible
77 if (this.atLeastOneCapture())
78 moves
= V
.KeepCaptures(moves
);
84 let moves
= super.getAllValidMoves();
85 if (moves
.some(m
=> { return m
.vanish
.length
== 2; }))
86 moves
= V
.KeepCaptures(moves
);
92 return false; //No notion of check
100 // No variables update because no royal king + no castling
101 updateVariables(move) { }
102 unupdateVariables(move) { }
106 // No valid move: you win!
107 return this.turn
== "w" ? "1-0" : "0-1";
123 static get SEARCH_DEPTH() { return 4; }
127 return - super.evalPosition(); //better with less material
130 static GenRandInitFen()
132 let pieces
= { "w": new Array(8), "b": new Array(8) };
133 // Shuffle pieces on first and last rank
134 for (let c
of ["w","b"])
136 let positions
= _
.range(8);
138 // Get random squares for bishops
139 let randIndex
= 2 * _
.random(3);
140 let bishop1Pos
= positions
[randIndex
];
141 // The second bishop must be on a square of different color
142 let randIndex_tmp
= 2 * _
.random(3) + 1;
143 let bishop2Pos
= positions
[randIndex_tmp
];
144 // Remove chosen squares
145 positions
.splice(Math
.max(randIndex
,randIndex_tmp
), 1);
146 positions
.splice(Math
.min(randIndex
,randIndex_tmp
), 1);
148 // Get random squares for knights
149 randIndex
= _
.random(5);
150 let knight1Pos
= positions
[randIndex
];
151 positions
.splice(randIndex
, 1);
152 randIndex
= _
.random(4);
153 let knight2Pos
= positions
[randIndex
];
154 positions
.splice(randIndex
, 1);
156 // Get random square for queen
157 randIndex
= _
.random(3);
158 let queenPos
= positions
[randIndex
];
159 positions
.splice(randIndex
, 1);
161 // Random square for king (no castle)
162 randIndex
= _
.random(2);
163 let kingPos
= positions
[randIndex
];
164 positions
.splice(randIndex
, 1);
166 // Rooks positions are now fixed
167 let rook1Pos
= positions
[0];
168 let rook2Pos
= positions
[1];
170 // Finally put the shuffled pieces in the board array
171 pieces
[c
][rook1Pos
] = 'r';
172 pieces
[c
][knight1Pos
] = 'n';
173 pieces
[c
][bishop1Pos
] = 'b';
174 pieces
[c
][queenPos
] = 'q';
175 pieces
[c
][kingPos
] = 'k';
176 pieces
[c
][bishop2Pos
] = 'b';
177 pieces
[c
][knight2Pos
] = 'n';
178 pieces
[c
][rook2Pos
] = 'r';
180 return pieces
["b"].join("") +
181 "/pppppppp/8/8/8/8/PPPPPPPP/" +
182 pieces
["w"].join("").toUpperCase() +
183 " w -"; //no en-passant
187 const VariantRules
= LosersRules
;