1 import { ChessRules
, Move
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class DiceRules
extends ChessRules
{
5 static get CanAnalyze() {
10 const fenParts
= fen
.split(" ");
12 ChessRules
.ParseFen(fen
),
13 { toplay: fenParts
[5] }
17 setOtherVariables(fen
) {
18 super.setOtherVariables(fen
);
20 const toplay
= V
.ParseFen(fen
).toplay
;
21 if (toplay
!= "-") this.p2play
.push(toplay
);
25 return super.getFen() + " " + this.getToplayFen();
29 return super.getFenForRepeat() + "_" + this.getToplayFen();
33 const L
= this.p2play
.length
;
34 return (L
> 0 ? this.p2play
[L
-1] : "-");
37 static GenRandInitFen(randomness
) {
38 return ChessRules
.GenRandInitFen(randomness
) + " -";
41 canMove(piece
, color
, [x
, y
]) {
42 const oppCol
= V
.GetOppCol(color
);
43 if (piece
== V
.PAWN
) {
44 const forward
= (color
== 'w' ? -1 : 1);
45 if (this.board
[x
+ forward
][y
] == V
.EMPTY
) return true;
46 for (let shift
of [-1, 1]) {
47 const [i
, j
] = [x
+ forward
, y
+ shift
];
50 this.board
[i
][j
] != V
.EMPTY
&&
51 this.getColor(i
, j
) == oppCol
59 [V
.KING
, V
.QUEEN
].includes(piece
)
60 ? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
62 for (let s
of steps
) {
63 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
66 (this.board
[i
][j
] == V
.EMPTY
|| this.getColor(i
, j
) == oppCol
)
76 // Find pieces which can move and roll a dice
78 for (let i
=0; i
<8; i
++) {
79 for (let j
=0; j
<8; j
++) {
80 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) == color
) {
81 const piece
= this.getPiece(i
, j
);
82 if (!canMove
[piece
] && this.canMove(piece
, color
, [i
, j
]))
83 canMove
[piece
] = [i
, j
];
87 const options
= Object
.keys(canMove
);
88 const randPiece
= options
[randInt(options
.length
)];
89 return [randPiece
, canMove
[randPiece
]];
92 getPotentialMovesFrom([x
, y
]) {
93 const color
= this.turn
;
94 let moves
= undefined;
95 if (this.movesCount
== 0) moves
= super.getPotentialMovesFrom([x
, y
]);
97 const L
= this.p2play
.length
; //L is >= 1
98 const piece
= this.getPiece(x
, y
);
101 this.p2play
[L
-1] != V
.PAWN
&&
102 ((color
== 'w' && x
== 1) || (color
== 'b' && x
== 6))
104 // The piece is a pawn about to promote
105 const destX
= (color
== 'w' ? 0 : 7);
107 if (this.board
[destX
][y
] == V
.EMPTY
) {
110 [x
, y
], [destX
, y
], { c: color
, p: this.p2play
[L
-1] })
113 for (let shift
of [-1, 1]) {
114 const [i
, j
] = [destX
, y
+ shift
];
117 this.board
[i
][j
] != V
.EMPTY
&&
118 this.getColor(i
, j
) != color
122 [x
, y
], [i
, j
], { c: color
, p: this.p2play
[L
-1] })
127 else if (piece
!= this.p2play
[L
-1])
128 // The piece type must match last p2play
130 else moves
= super.getPotentialMovesFrom([x
, y
]);
132 // Decide which piece the opponent will play:
133 const oppCol
= V
.GetOppCol(color
);
135 V
.PlayOnBoard(this.board
, m
);
136 const [piece
, square
] = this.getRandPiece(oppCol
);
137 m
.start
.toplay
= square
;
139 V
.UndoOnBoard(this.board
, m
);
153 const color
= this.turn
;
154 if (this.kingPos
[color
][0] < 0) return (color
== 'w' ? "0-1" : "1-0");
159 this.p2play
.push(move.end
.piece
);
160 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
161 this.kingPos
[move.vanish
[1].c
] = [-1, -1];
162 // Castle flags for captured king won't be updated (not important...)
163 super.postPlay(move);
168 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
169 this.kingPos
[move.vanish
[1].c
] = [move.vanish
[1].x
, move.vanish
[1].y
];
170 super.postUndo(move);
173 static get SEARCH_DEPTH() {
178 return super.getNotation(move) + "/" + move.end
.piece
.toUpperCase();