| 1 | import { ChessRules } from "@/base_rules"; |
| 2 | import { ArrayFun } from "@/utils/array"; |
| 3 | import { randInt } from "@/utils/alea"; |
| 4 | |
| 5 | export class LosersRules extends ChessRules { |
| 6 | // Trim all non-capturing moves |
| 7 | static KeepCaptures(moves) { |
| 8 | return moves.filter(m => m.vanish.length == 2 && m.appear.length == 1); |
| 9 | } |
| 10 | |
| 11 | // Stop at the first capture found (if any) |
| 12 | atLeastOneCapture() { |
| 13 | const color = this.turn; |
| 14 | for (let i = 0; i < V.size.x; i++) { |
| 15 | for (let j = 0; j < V.size.y; j++) { |
| 16 | if ( |
| 17 | this.board[i][j] != V.EMPTY && |
| 18 | this.getColor(i, j) == color && |
| 19 | this.getPotentialMovesFrom([i, j]).some(m => { |
| 20 | return ( |
| 21 | // Warning: discard castle moves |
| 22 | m.vanish.length == 2 && m.appear.length == 1 && |
| 23 | this.filterValid([m]).length == 1 |
| 24 | ); |
| 25 | }) |
| 26 | ) { |
| 27 | return true; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | getPossibleMovesFrom(sq) { |
| 35 | let moves = this.filterValid(this.getPotentialMovesFrom(sq)); |
| 36 | const captureMoves = V.KeepCaptures(moves); |
| 37 | |
| 38 | console.log(this.atLeastOneCapture()); |
| 39 | |
| 40 | if (captureMoves.length > 0) return captureMoves; |
| 41 | if (this.atLeastOneCapture()) return []; |
| 42 | return moves; |
| 43 | } |
| 44 | |
| 45 | getAllValidMoves() { |
| 46 | const moves = super.getAllValidMoves(); |
| 47 | if (moves.some(m => m.vanish.length == 2 && m.appear.length == 1)) |
| 48 | return V.KeepCaptures(moves); |
| 49 | return moves; |
| 50 | } |
| 51 | |
| 52 | getCurrentScore() { |
| 53 | // If only my king remains, I win |
| 54 | const color = this.turn; |
| 55 | let onlyKing = true; |
| 56 | outerLoop: for (let i=0; i<V.size.x; i++) { |
| 57 | for (let j=0; j<V.size.y; j++) { |
| 58 | if ( |
| 59 | this.board[i][j] != V.EMPTY && |
| 60 | this.getColor(i,j) == color && |
| 61 | this.getPiece(i,j) != V.KING |
| 62 | ) { |
| 63 | onlyKing = false; |
| 64 | break outerLoop; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | if (onlyKing) return color == "w" ? "1-0" : "0-1"; |
| 69 | if (this.atLeastOneMove()) return "*"; |
| 70 | // No valid move: the side who cannot move (or is checkmated) wins |
| 71 | return this.turn == "w" ? "1-0" : "0-1"; |
| 72 | } |
| 73 | |
| 74 | evalPosition() { |
| 75 | // Less material is better (more subtle in fact but...) |
| 76 | return -super.evalPosition(); |
| 77 | } |
| 78 | }; |