1 import { ChessRules
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class Progressive1Rules
extends ChessRules
{
6 static get HasEnpassant() {
10 setOtherVariables(fen
) {
11 super.setOtherVariables(fen
);
16 if (moves
.length
== 0) return [];
17 const color
= this.turn
;
18 return moves
.filter(m
=> {
19 // Not using this.play() (would result ininfinite recursive calls)
20 V
.PlayOnBoard(this.board
, m
);
21 if (m
.appear
[0].p
== V
.KING
)
22 this.kingPos
[color
] = [m
.appear
[0].x
, m
.appear
[0].y
];
23 const res
= !this.underCheck(color
);
24 V
.UndoOnBoard(this.board
, m
);
25 if (m
.appear
[0].p
== V
.KING
)
26 this.kingPos
[color
] = [m
.vanish
[0].x
, m
.vanish
[0].y
];
32 if (V
.HasFlags
) move.flags
= JSON
.stringify(this.aggregateFlags());
33 const color
= this.turn
;
34 const oppCol
= V
.GetOppCol(color
);
35 move.turn
= [color
, this.subTurn
];
36 V
.PlayOnBoard(this.board
, move);
38 this.subTurn
> this.movesCount
||
39 this.underCheck(oppCol
) ||
40 !this.atLeastOneMove()
51 const c
= move.turn
[0];
52 const piece
= move.vanish
[0].p
;
53 const firstRank
= c
== "w" ? V
.size
.x
- 1 : 0;
55 if (piece
== V
.KING
&& move.appear
.length
> 0) {
56 this.kingPos
[c
][0] = move.appear
[0].x
;
57 this.kingPos
[c
][1] = move.appear
[0].y
;
58 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
61 const oppCol
= V
.GetOppCol(c
);
62 const oppFirstRank
= V
.size
.x
- 1 - firstRank
;
64 move.start
.x
== firstRank
&& //our rook moves?
65 this.castleFlags
[c
].includes(move.start
.y
)
67 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
68 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
71 move.end
.x
== oppFirstRank
&& //we took opponent rook?
72 this.castleFlags
[oppCol
].includes(move.end
.y
)
74 const flagIdx
= (move.end
.y
== this.castleFlags
[oppCol
][0] ? 0 : 1);
75 this.castleFlags
[oppCol
][flagIdx
] = V
.size
.y
;
80 this.disaggregateFlags(JSON
.parse(move.flags
));
81 V
.UndoOnBoard(this.board
, move);
82 if (this.turn
!= move.turn
[0]) this.movesCount
--;
83 this.turn
= move.turn
[0];
84 this.subTurn
= move.turn
[1];
94 q: 7, //slightly less than in orthodox game
99 // Random moves (too high branching factor otherwise). TODO
102 const color
= this.turn
;
103 while (this.turn
== color
) {
104 const moves
= this.getAllValidMoves();
105 const m
= moves
[randInt(moves
.length
)];
109 for (let i
=res
.length
- 1; i
>= 0; i
--) this.undo(res
[i
]);