| 1 | import { ChessRules } from "@/base_rules"; |
| 2 | import { Synchrone1Rules } from "@/variants/Synchrone1"; |
| 3 | import { randInt } from "@/utils/alea"; |
| 4 | |
| 5 | export class Synchrone2Rules extends Synchrone1Rules { |
| 6 | |
| 7 | static get CanAnalyze() { |
| 8 | return true;//false; |
| 9 | } |
| 10 | |
| 11 | static get HasEnpassant() { |
| 12 | return false; |
| 13 | } |
| 14 | |
| 15 | static IsGoodFen(fen) { |
| 16 | if (!Synchrone1Rules.IsGoodFen(fen)) return false; |
| 17 | const fenParsed = V.ParseFen(fen); |
| 18 | // 5) Check initFen (not really... TODO?) |
| 19 | if (!fenParsed.initFen || fenParsed.initFen == "-") return false; |
| 20 | return true; |
| 21 | } |
| 22 | |
| 23 | static ParseFen(fen) { |
| 24 | const fenParts = fen.split(" "); |
| 25 | return Object.assign( |
| 26 | { |
| 27 | initFen: fenParts[4], |
| 28 | whiteMove: fenParts[5] |
| 29 | }, |
| 30 | ChessRules.ParseFen(fen) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | getInitfenFen() { |
| 35 | if (!this.whiteMove) return "-"; |
| 36 | return JSON.stringify({ |
| 37 | start: this.whiteMove.start, |
| 38 | end: this.whiteMove.end, |
| 39 | appear: this.whiteMove.appear, |
| 40 | vanish: this.whiteMove.vanish |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | getFen() { |
| 45 | return ( |
| 46 | super.getFen() + " " + |
| 47 | this.getInitfenFen() + " " + |
| 48 | this.getWhitemoveFen() |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | static GenRandInitFen(randomness) { |
| 53 | const res = ChessRules.GenRandInitFen(randomness); |
| 54 | // Add initFen field: |
| 55 | return res.slice(0, -1) + " " + res.split(' ')[1] + " -"; |
| 56 | } |
| 57 | |
| 58 | setOtherVariables(fen) { |
| 59 | const parsedFen = V.ParseFen(fen); |
| 60 | this.setFlags(parsedFen.flags); |
| 61 | super.scanKings(fen); |
| 62 | // Also init whiteMove |
| 63 | this.whiteMove = |
| 64 | parsedFen.whiteMove != "-" |
| 65 | ? JSON.parse(parsedFen.whiteMove) |
| 66 | : null; |
| 67 | // And initFen (not empty) |
| 68 | this.initFen = parsedFen.initFen; |
| 69 | } |
| 70 | |
| 71 | getPotentialMovesFrom([x, y]) { |
| 72 | if (this.movesCount % 4 <= 1) return super.getPotentialMovesFrom([x, y]); |
| 73 | // TODO: either add a "blackMove' field in FEN (bof...), |
| 74 | // or write an helper function to detect from diff positions, |
| 75 | // which piece moved (if not disappeared!), which moves are valid. |
| 76 | // + do not forget pass move (king 2 king): always possible at stage 2. |
| 77 | return []; |
| 78 | } |
| 79 | |
| 80 | play(move) { |
| 81 | move.flags = JSON.stringify(this.aggregateFlags()); |
| 82 | // Do not play on board (would reveal the move...) |
| 83 | this.turn = V.GetOppCol(this.turn); |
| 84 | this.movesCount++; |
| 85 | this.postPlay(move); |
| 86 | } |
| 87 | |
| 88 | undo(move) { |
| 89 | this.disaggregateFlags(JSON.parse(move.flags)); |
| 90 | if (this.turn == 'w') |
| 91 | // Back to the middle of the move |
| 92 | V.UndoOnBoard(this.board, move.smove); |
| 93 | this.turn = V.GetOppCol(this.turn); |
| 94 | this.movesCount--; |
| 95 | this.postUndo(move); |
| 96 | } |
| 97 | |
| 98 | getCurrentScore() { |
| 99 | if (this.movesCount % 4 != 0) |
| 100 | // Turn (2 x white + black) not over yet |
| 101 | return "*"; |
| 102 | // Was a king captured? |
| 103 | if (this.kingPos['w'][0] < 0) return "0-1"; |
| 104 | if (this.kingPos['b'][0] < 0) return "1-0"; |
| 105 | const whiteCanMove = this.atLeastOneMove('w'); |
| 106 | const blackCanMove = this.atLeastOneMove('b'); |
| 107 | if (whiteCanMove && blackCanMove) return "*"; |
| 108 | // Game over |
| 109 | const whiteInCheck = this.underCheck('w'); |
| 110 | const blackInCheck = this.underCheck('b'); |
| 111 | if ( |
| 112 | (whiteCanMove && !this.underCheck('b')) || |
| 113 | (blackCanMove && !this.underCheck('w')) |
| 114 | ) { |
| 115 | return "1/2"; |
| 116 | } |
| 117 | // Checkmate: could be mutual |
| 118 | if (!whiteCanMove && !blackCanMove) return "1/2"; |
| 119 | return (whiteCanMove ? "1-0" : "0-1"); |
| 120 | } |
| 121 | |
| 122 | }; |