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 if (this.subTurn
== 1) return [];
23 const L
= this.p2play
.length
;
24 const piece
= this.getPiece(x
, y
);
25 if (piece
== V
.PAWN
&& this.p2play
[L
-1] != V
.PAWN
) {
26 // The piece must be a pawn about to promote.
27 const color
= this.turn
;
28 const beforeLastRank
= (color
== 'w' ? 1 : 0);
29 const forward
= (color
== 'w' ? -1 : 1);
31 if (this.board
[x
+ forward
][y
] == V
.EMPTY
) {
34 [x
, y
], [x
+ forward
], { c: color
, p: this.p2play
[L
-1] })
37 for (let shift
of [-1, 1]) {
38 const [i
, j
] = [x
+ forward
, y
+ shift
];
41 this.board
[i
][j
] != V
.EMPTY
&&
42 this.getColor(i
, j
) != color
46 [x
, y
], [i
, j
], { c: color
, p: this.p2play
[L
-1] })
52 if (piece
!= this.p2play
[L
-1])
53 // The piece type must match last p2play
55 return super.getPotentialMovesFrom([x
, y
]);
58 setOtherVariables(fen
) {
59 super.setOtherVariables(fen
);
73 const color
= this.turn
;
74 if (this.kingPos
[color
][0] < 0) return (color
== 'w' ? "0-1" : "1-0");
79 if (this.subTurn
== 1) {
81 this.p2play
.push(move.appear
[0].p
);
84 // Subturn == 2 means the (dice-constrained) move is played
85 move.flags
= JSON
.stringify(this.aggregateFlags());
86 V
.PlayOnBoard(this.board
, move);
87 this.epSquares
.push(this.getEpSquare(move));
89 this.turn
= V
.GetOppCol(this.turn
);
95 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
96 this.kingPos
[move.vanish
[1].c
] = [-1, -1];
97 // Castle flags for captured king won't be updated (not important...)
102 if (this.subTurn
== 2) {
107 this.disaggregateFlags(JSON
.parse(move.flags
));
108 V
.UndoOnBoard(this.board
, move);
109 this.epSquares
.pop();
111 this.turn
= V
.GetOppCol(this.turn
);
117 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
118 this.kingPos
[move.vanish
[1].c
] = [move.vanish
[1].x
, move.vanish
[1].y
];
119 super.postUndo(move);
123 // For current turn, find pieces which can move and roll a dice
125 const color
= this.turn
;
126 for (let i
=0; i
<8; i
++) {
127 for (let j
=0; j
<8; j
++) {
128 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) == color
) {
129 const piece
= this.getPiece(i
, j
);
132 super.getPotentialMovesFrom([i
, j
]).length
> 0
134 canMove
[piece
] = [i
, j
];
139 const options
= Object
.keys(canMove
);
140 const randPiece
= options
[randInt(options
.length
)];
143 appear: [{ p: randPiece
}],
145 start: { x: -1, y: -1 },
146 end: { x: canMove
[randPiece
][0], y: canMove
[randPiece
][1] }
153 const toPlay
= this.getRandPieceMove();
155 const moves
= this.getAllValidMoves();
156 const choice
= moves
[randInt(moves
.length
)];
158 return [toPlay
, choice
];
162 if (this.subTurn
== 1) return move.appear
[0].p
.toUpperCase();
163 return super.getNotation(move);