7f623982d0f5ca7283e27b7ab430b57e12ea3ba6
1 import { ChessRules
} from "@/base_rules";
2 import { Synchrone1Rules
} from "@/variants/Synchrone1";
3 import { randInt
} from "@/utils/alea";
5 export class Synchrone2Rules
extends Synchrone1Rules
{
7 static get CanAnalyze() {
11 static get HasEnpassant() {
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;
23 static ParseFen(fen
) {
24 const fenParts
= fen
.split(" ");
28 whiteMove: fenParts
[5]
30 ChessRules
.ParseFen(fen
)
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
46 super.getFen() + " " +
47 this.getInitfenFen() + " " +
48 this.getWhitemoveFen()
52 static GenRandInitFen(randomness
) {
53 const res
= ChessRules
.GenRandInitFen(randomness
);
55 return res
.slice(0, -1) + " " + res
.split(' ')[1] + " -";
58 setOtherVariables(fen
) {
59 const parsedFen
= V
.ParseFen(fen
);
60 this.setFlags(parsedFen
.flags
);
62 // Also init whiteMove
64 parsedFen
.whiteMove
!= "-"
65 ? JSON
.parse(parsedFen
.whiteMove
)
67 // And initFen (not empty)
68 this.initFen
= parsedFen
.initFen
;
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.
81 move.flags
= JSON
.stringify(this.aggregateFlags());
82 // Do not play on board (would reveal the move...)
83 this.turn
= V
.GetOppCol(this.turn
);
89 this.disaggregateFlags(JSON
.parse(move.flags
));
91 // Back to the middle of the move
92 V
.UndoOnBoard(this.board
, move.smove
);
93 this.turn
= V
.GetOppCol(this.turn
);
99 if (this.movesCount
% 4 != 0)
100 // Turn (2 x white + black) not over yet
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 "*";
109 const whiteInCheck
= this.underCheck('w');
110 const blackInCheck
= this.underCheck('b');
112 (whiteCanMove
&& !this.underCheck('b')) ||
113 (blackCanMove
&& !this.underCheck('w'))
117 // Checkmate: could be mutual
118 if (!whiteCanMove
&& !blackCanMove
) return "1/2";
119 return (whiteCanMove
? "1-0" : "0-1");