1 import { ChessRules
, Move
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class DiceRules
extends ChessRules
{
5 static get CanAnalyze() {
13 this.board
[square
[0]][square
[1]] != V
.EMPTY
17 // Announce the piece' type to be played:
18 return this.getRandPieceMove();
21 getPotentialMovesFrom([x
, y
]) {
22 const L
= this.p2play
.length
;
25 // The piece type must match last p2play
26 this.getPiece(x
, y
) != this.p2play
[L
-1]
30 return super.getPotentialMovesFrom([x
, y
]);
33 setOtherVariables(fen
) {
34 super.setOtherVariables(fen
);
48 const color
= this.turn
;
49 if (this.kingPos
[color
][0] < 0) return (color
== 'w' ? "0-1" : "1-0");
54 if (this.subTurn
== 1) {
56 this.p2play
.push(move.appear
[0].p
);
59 // Subturn == 2 means the (dice-constrained) move is played
60 move.flags
= JSON
.stringify(this.aggregateFlags());
61 V
.PlayOnBoard(this.board
, move);
62 this.epSquares
.push(this.getEpSquare(move));
64 this.turn
= V
.GetOppCol(this.turn
);
70 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
71 this.kingPos
[move.vanish
[1].c
] = [-1, -1];
72 // Castle flags for captured king won't be updated (not important...)
77 if (this.subTurn
== 2) {
82 this.disaggregateFlags(JSON
.parse(move.flags
));
83 V
.UndoOnBoard(this.board
, move);
86 this.turn
= V
.GetOppCol(this.turn
);
92 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
93 this.kingPos
[move.vanish
[1].c
] = [move.vanish
[1].x
, move.vanish
[1].y
];
98 // For current turn, find pieces which can move and roll a dice
100 const color
= this.turn
;
101 for (let i
=0; i
<8; i
++) {
102 for (let j
=0; j
<8; j
++) {
103 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) == color
) {
104 const piece
= this.getPiece(i
, j
);
107 super.getPotentialMovesFrom([i
, j
]).length
> 0
109 canMove
[piece
] = [i
, j
];
114 const options
= Object
.keys(canMove
);
115 const randPiece
= options
[randInt(options
.length
)];
118 appear: [{ p: randPiece
}],
120 start: { x: -1, y: -1 },
121 end: { x: canMove
[randPiece
][0], y: canMove
[randPiece
][1] }
128 const toPlay
= this.getRandPieceMove();
130 const moves
= this.getAllValidMoves();
131 const choice
= moves
[randInt(moves
.length
)];
133 return [toPlay
, choice
];
137 if (this.subTurn
== 1) return move.appear
[0].p
.toUpperCase();
138 return super.getNotation(move);