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