| 1 | import ChessRules from "/base_rules.js"; |
| 2 | |
| 3 | export default class AbsorptionRules extends ChessRules { |
| 4 | |
| 5 | static get Options() { |
| 6 | return { |
| 7 | select: C.Options.select, |
| 8 | check: [], |
| 9 | styles: [ |
| 10 | "balance", |
| 11 | "capture", |
| 12 | "cylinder", |
| 13 | "dark", |
| 14 | "doublemove", |
| 15 | "progressive", |
| 16 | "recycle", |
| 17 | "rifle", |
| 18 | "teleport", |
| 19 | "zen" |
| 20 | ] |
| 21 | }; |
| 22 | } |
| 23 | |
| 24 | pieces(color) { |
| 25 | let fusions = { |
| 26 | // amazon |
| 27 | 'a': { |
| 28 | "class": "amazon", |
| 29 | moves: [ |
| 30 | { |
| 31 | steps: [ |
| 32 | [0, 1], [0, -1], [1, 0], [-1, 0], |
| 33 | [1, 1], [1, -1], [-1, 1], [-1, -1] |
| 34 | ] |
| 35 | }, |
| 36 | { |
| 37 | steps: [ |
| 38 | [1, 2], [1, -2], [-1, 2], [-1, -2], |
| 39 | [2, 1], [-2, 1], [2, -1], [-2, -1] |
| 40 | ], |
| 41 | range: 1 |
| 42 | } |
| 43 | ] |
| 44 | }, |
| 45 | // empress |
| 46 | 'e': { |
| 47 | "class": "empress", |
| 48 | steps: [ |
| 49 | [1, 2], [1, -2], [-1, 2], [-1, -2], |
| 50 | [2, 1], [-2, 1], [2, -1], [-2, -1] |
| 51 | ], |
| 52 | }, |
| 53 | // princess |
| 54 | 'b': { |
| 55 | "class": "bishop", |
| 56 | steps: [[1, 1], [1, -1], [-1, 1], [-1, -1]] |
| 57 | } |
| 58 | }; |
| 59 | return Object.assign(fusions, super.pieces(color)); |
| 60 | } |
| 61 | |
| 62 | static get MergeComposed() { |
| 63 | return { |
| 64 | "be": "a", |
| 65 | "bs": "s", |
| 66 | "er": "e", |
| 67 | "rs": "a", |
| 68 | "eq": "a", |
| 69 | "qs": "a", |
| 70 | "ee": "e", |
| 71 | "es": "a", |
| 72 | "ss": "s" |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | static Fusion(p1, p2) { |
| 77 | if (p1 == V.KING) return p1; |
| 78 | if (p1 == V.PAWN) return p2; |
| 79 | if (p2 == V.PAWN) return p1; |
| 80 | if ([p1, p2].includes(V.KNIGHT)) { |
| 81 | if ([p1, p2].includes(V.QUEEN)) return V.QN; |
| 82 | if ([p1, p2].includes(V.ROOK)) return V.RN; |
| 83 | if ([p1, p2].includes(V.BISHOP)) return V.BN; |
| 84 | // p1 or p2 already have knight + other piece |
| 85 | return (p1 == V.KNIGHT ? p2 : p1); |
| 86 | } |
| 87 | if ([p1, p2].includes(V.QN)) return V.QN; |
| 88 | for (let p of [p1, p2]) { |
| 89 | if ([V.BN, V.RN].includes(p)) |
| 90 | return V.MergeComposed[[p1, p2].sort().join("")]; |
| 91 | } |
| 92 | // bishop + rook, or queen + [bishop or rook] |
| 93 | return V.QUEEN; |
| 94 | } |
| 95 | |
| 96 | getPotentialMovesFrom(sq) { |
| 97 | let moves = []; |
| 98 | const piece = this.getPiece(sq[0], sq[1]); |
| 99 | switch (piece) { |
| 100 | case V.RN: |
| 101 | moves = |
| 102 | super.getPotentialRookMoves(sq).concat( |
| 103 | super.getPotentialKnightMoves(sq)); |
| 104 | break; |
| 105 | case V.BN: |
| 106 | moves = |
| 107 | super.getPotentialBishopMoves(sq).concat( |
| 108 | super.getPotentialKnightMoves(sq)); |
| 109 | break; |
| 110 | case V.QN: |
| 111 | moves = |
| 112 | super.getPotentialQueenMoves(sq).concat( |
| 113 | super.getPotentialKnightMoves(sq)); |
| 114 | break; |
| 115 | default: |
| 116 | moves = super.getPotentialMovesFrom(sq); |
| 117 | } |
| 118 | // Filter out capturing promotions (except one), |
| 119 | // because they are all the same. |
| 120 | moves = moves.filter(m => { |
| 121 | return ( |
| 122 | m.vanish.length == 1 || |
| 123 | m.vanish[0].p != V.PAWN || |
| 124 | [V.PAWN, V.QUEEN].includes(m.appear[0].p) |
| 125 | ); |
| 126 | }); |
| 127 | moves.forEach(m => { |
| 128 | if ( |
| 129 | m.vanish.length == 2 && |
| 130 | m.appear.length == 1 && |
| 131 | piece != m.vanish[1].p |
| 132 | ) { |
| 133 | // Augment pieces abilities in case of captures |
| 134 | m.appear[0].p = V.Fusion(piece, m.vanish[1].p); |
| 135 | } |
| 136 | }); |
| 137 | return moves; |
| 138 | } |
| 139 | |
| 140 | isAttacked(sq, color) { |
| 141 | return ( |
| 142 | super.isAttacked(sq, color) || |
| 143 | this.isAttackedByBN(sq, color) || |
| 144 | this.isAttackedByRN(sq, color) || |
| 145 | this.isAttackedByQN(sq, color) |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | isAttackedByBN(sq, color) { |
| 150 | return ( |
| 151 | this.isAttackedBySlideNJump(sq, color, V.BN, V.steps[V.BISHOP]) || |
| 152 | this.isAttackedBySlideNJump( |
| 153 | sq, color, V.BN, V.steps[V.KNIGHT], 1) |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | isAttackedByRN(sq, color) { |
| 158 | return ( |
| 159 | this.isAttackedBySlideNJump(sq, color, V.RN, V.steps[V.ROOK]) || |
| 160 | this.isAttackedBySlideNJump( |
| 161 | sq, color, V.RN, V.steps[V.KNIGHT], 1) |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | isAttackedByQN(sq, color) { |
| 166 | return ( |
| 167 | this.isAttackedBySlideNJump( |
| 168 | sq, color, V.QN, V.steps[V.BISHOP].concat(V.steps[V.ROOK])) || |
| 169 | this.isAttackedBySlideNJump( |
| 170 | sq, color, V.QN, V.steps[V.KNIGHT], 1) |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | static get VALUES() { |
| 175 | return Object.assign( |
| 176 | { a: 12, e: 7, s: 5 }, |
| 177 | ChessRules.VALUES |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | getNotation(move) { |
| 182 | let notation = super.getNotation(move); |
| 183 | if (move.vanish[0].p != V.PAWN && move.appear[0].p != move.vanish[0].p) |
| 184 | // Fusion (not from a pawn: handled in ChessRules) |
| 185 | notation += "=" + move.appear[0].p.toUpperCase(); |
| 186 | return notation; |
| 187 | } |
| 188 | |
| 189 | }; |