| 1 | import { ChessRules, Move, PiPo } from "@/base_rules"; |
| 2 | import { randInt } from "@/utils/alea"; |
| 3 | |
| 4 | export class Avalam2Rules extends ChessRules { |
| 5 | |
| 6 | static get Options() { |
| 7 | return null; |
| 8 | } |
| 9 | |
| 10 | static get HasFlags() { |
| 11 | return false; |
| 12 | } |
| 13 | |
| 14 | static get HasEnpassant() { |
| 15 | return false; |
| 16 | } |
| 17 | |
| 18 | static get Monochrome() { |
| 19 | return true; |
| 20 | } |
| 21 | |
| 22 | get showFirstTurn() { |
| 23 | return true; |
| 24 | } |
| 25 | |
| 26 | getPpath(b) { |
| 27 | return "Avalam/" + b; |
| 28 | } |
| 29 | |
| 30 | static get PIECES() { |
| 31 | // Towers of 1, 2, 3, 4 and 5 |
| 32 | return ['b', 'c', 'd', 'e', 'f']; |
| 33 | } |
| 34 | |
| 35 | static IsGoodPosition(position) { |
| 36 | if (position.length == 0) return false; |
| 37 | const rows = position.split("/"); |
| 38 | if (rows.length != V.size.x) return false; |
| 39 | for (let row of rows) { |
| 40 | let sumElts = 0; |
| 41 | for (let i = 0; i < row.length; i++) { |
| 42 | if (['x'].concat(V.PIECES).includes(row[i].toLowerCase())) sumElts++; |
| 43 | else { |
| 44 | const num = parseInt(row[i], 10); |
| 45 | if (isNaN(num)) return false; |
| 46 | sumElts += num; |
| 47 | } |
| 48 | } |
| 49 | if (sumElts != V.size.y) return false; |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | static GenRandInitFen() { |
| 55 | return ( |
| 56 | "BbBbBbBb/bBbBbBbB/BbBbBbBb/bBbBbBbB/" + |
| 57 | "BbBbBbBb/bBbBbBbB/BbBbBbBb/bBbBbBbB w 0" |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | canIplay(side) { |
| 62 | return this.turn == side; |
| 63 | } |
| 64 | |
| 65 | getColor() { |
| 66 | return this.turn; //:-) |
| 67 | } |
| 68 | |
| 69 | getBasicMove([x1, y1], [x2, y2]) { |
| 70 | const cp1 = this.board[x1][y1], |
| 71 | cp2 = this.board[x2][y2]; |
| 72 | const newPiece = |
| 73 | String.fromCharCode(cp1.charCodeAt(1) + cp2.charCodeAt(1) - 97); |
| 74 | return ( |
| 75 | new Move({ |
| 76 | vanish: [ |
| 77 | new PiPo({ x: x1, y: y1, c: cp1[0], p: cp1[1] }), |
| 78 | new PiPo({ x: x2, y: y2, c: cp2[0], p: cp2[1] }) |
| 79 | ], |
| 80 | appear: [ |
| 81 | new PiPo({ x: x2, y: y2, c: cp1[0], p: newPiece }) |
| 82 | ] |
| 83 | }) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | getPotentialMovesFrom([x, y]) { |
| 88 | const height = this.board[x][y].charCodeAt(1) - 97; |
| 89 | if (height == 5) return []; |
| 90 | let moves = []; |
| 91 | for (let s of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { |
| 92 | const [i, j] = [x + s[0], y + s[1]]; |
| 93 | if ( |
| 94 | V.OnBoard(i, j) && |
| 95 | this.board[i][j] != V.EMPTY && |
| 96 | (height + this.board[i][j].charCodeAt(1) - 97 <= 5) |
| 97 | ) { |
| 98 | moves.push(this.getBasicMove([x, y], [i, j])); |
| 99 | } |
| 100 | } |
| 101 | return moves; |
| 102 | } |
| 103 | |
| 104 | filterValid(moves) { |
| 105 | return moves; |
| 106 | } |
| 107 | |
| 108 | getCheckSquares() { |
| 109 | return []; |
| 110 | } |
| 111 | |
| 112 | getCurrentScore() { |
| 113 | let towersCount = { w: 0, b: 0 }; |
| 114 | for (let i = 0; i < V.size.x; i++) { |
| 115 | for (let j = 0; j < V.size.y; j++) { |
| 116 | if (this.board[i][j] != V.EMPTY) { |
| 117 | if (this.getPotentialMovesFrom([i, j]).length > 0) return '*'; |
| 118 | towersCount[ this.board[i][j][0] ]++; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | if (towersCount['w'] > towersCount['b']) return "1-0"; |
| 123 | if (towersCount['b'] > towersCount['w']) return "0-1"; |
| 124 | return "1/2"; |
| 125 | } |
| 126 | |
| 127 | getComputerMove() { |
| 128 | // Random mover (TODO) |
| 129 | const moves = super.getAllValidMoves(); |
| 130 | if (moves.length == 0) return null; |
| 131 | return moves[randInt(moves.length)]; |
| 132 | } |
| 133 | |
| 134 | }; |