| 1 | import ChessRules from "/base_rules.js"; |
| 2 | import {Random} from "/utils/alea.js"; |
| 3 | |
| 4 | export default class DiceRules extends ChessRules { |
| 5 | |
| 6 | static get Options() { |
| 7 | let res = C.Options; |
| 8 | res.select["defaut"] = 2; |
| 9 | return { |
| 10 | select: res.select, |
| 11 | input: [ |
| 12 | { |
| 13 | label: "Biased alea", |
| 14 | variable: "biased", |
| 15 | type: "checkbox", |
| 16 | defaut: true |
| 17 | }, |
| 18 | { |
| 19 | label: "Falling pawn", |
| 20 | variable: "pawnfall", |
| 21 | type: "checkbox", |
| 22 | defaut: false |
| 23 | } |
| 24 | ], |
| 25 | styles: [ |
| 26 | "atomic", |
| 27 | "capture", |
| 28 | "crazyhouse", |
| 29 | "cylinder", |
| 30 | "madrasi", |
| 31 | "recycle", |
| 32 | "rifle", |
| 33 | "zen" |
| 34 | ] |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | getPartFen(o) { |
| 39 | let toplay = ''; |
| 40 | if (o.init) { |
| 41 | let canMove = (this.options["biased"] |
| 42 | ? Array(8).fill('p').concat(Array(2).fill('n')) |
| 43 | : ['p', 'n']); |
| 44 | toplay = canMove[Random.randInt(canMove.length)]; |
| 45 | } |
| 46 | return Object.assign( |
| 47 | { toplay: (o.init ? toplay : this.getRandomPiece(this.turn)) }, |
| 48 | super.getPartFen(o) |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | constructor(o) { |
| 53 | super(o); |
| 54 | this.afterPlay = (move_s, newTurn, ops) => { |
| 55 | // Movestack contains only one move: |
| 56 | move_s[0].toplay = this.getRandomPiece(this.turn); |
| 57 | this.toplay = move_s[0].toplay; |
| 58 | this.displayMessage(move_s[0].toplay, |
| 59 | C.GetOppTurn(move_s[0].appear[0].c)); |
| 60 | o.afterPlay(move_s, newTurn, ops); |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | static get PieceToUnicode() { |
| 65 | return { |
| 66 | 'K': "♔", |
| 67 | 'Q': "♕", |
| 68 | 'R': "♖", |
| 69 | 'B': "♗", |
| 70 | 'N': "♘", |
| 71 | 'P': "♙", |
| 72 | 'k': "♚", |
| 73 | 'q': "♛", |
| 74 | 'r': "♜", |
| 75 | 'b': "♝", |
| 76 | 'n': "♞", |
| 77 | 'p': "♟" |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | displayMessage(piece, color) { |
| 82 | if (color == 'w') |
| 83 | piece = piece.toUpperCase(); |
| 84 | super.displayMessage(this.message, |
| 85 | '<span>to play:</span> ' + |
| 86 | '<span class="symb">' + V.PieceToUnicode[piece] + '</span>' |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | setOtherVariables(fenParsed) { |
| 91 | super.setOtherVariables(fenParsed); |
| 92 | this.toplay = fenParsed.toplay; |
| 93 | this.message = document.createElement("div"); |
| 94 | C.AddClass_es(this.message, "piece-text"); |
| 95 | let container = document.getElementById(this.containerId); |
| 96 | container.appendChild(this.message); |
| 97 | this.displayMessage(this.toplay, fenParsed.turn); |
| 98 | } |
| 99 | |
| 100 | getRandomPiece(color) { |
| 101 | // Find pieces which can move and roll a (biased) dice |
| 102 | let canMove = []; |
| 103 | for (let i=0; i<8; i++) { |
| 104 | for (let j=0; j<8; j++) { |
| 105 | if (this.board[i][j] != "" && this.getColor(i, j) == color) { |
| 106 | const piece = this.getPiece(i, j); |
| 107 | if (this.findDestSquares([i, j], {one: true})) |
| 108 | canMove.push(piece); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | if (!this.options["biased"]) |
| 113 | canMove = [...new Set(canMove)]; |
| 114 | return canMove[Random.randInt(canMove.length)]; |
| 115 | } |
| 116 | |
| 117 | postProcessPotentialMoves(moves) { |
| 118 | return super.postProcessPotentialMoves(moves).filter(m => { |
| 119 | return ( |
| 120 | (m.appear.length >= 1 && m.appear[0].p == this.toplay) || |
| 121 | (m.vanish.length >= 1 && m.vanish[0].p == this.toplay) |
| 122 | ); |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | filterValid(moves) { |
| 127 | return moves; |
| 128 | } |
| 129 | |
| 130 | playReceivedMove(moves, callback) { |
| 131 | this.toplay = moves[0].toplay; //only one move |
| 132 | this.displayMessage(this.toplay, C.GetOppTurn(moves[0].appear[0].c)); |
| 133 | super.playReceivedMove(moves, callback); |
| 134 | } |
| 135 | |
| 136 | }; |