+ static IsGoodPosition(position) {
+ if (position.length == 0) return false;
+ const rows = position.split("/");
+ if (rows.length != V.size.x) return false;
+ let kings = { "w": 0, "b": 0 };
+ const allPiecesCodes = V.PIECES.concat(V.AUGMENTED_PIECES);
+ const kingBlackCodes = ['j','k','l'];
+ const kingWhiteCodes = ['J','K','L'];
+ for (let row of rows) {
+ let sumElts = 0;
+ for (let i = 0; i < row.length; i++) {
+ if (kingBlackCodes.includes(row[i])) kings['b']++;
+ else if (kingWhiteCodes.includes(row[i])) kings['w']++;
+ if (allPiecesCodes.includes(row[i].toLowerCase())) sumElts++;
+ else {
+ const num = parseInt(row[i], 10);
+ if (isNaN(num)) return false;
+ sumElts += num;
+ }
+ }
+ if (sumElts != V.size.y) return false;
+ }
+ // Both kings should be on board, only one of each color:
+ if (Object.values(kings).some(v => v != 1)) return false;
+ return true;
+ }
+
+ // Kings may be augmented:
+ scanKings(fen) {
+ this.kingPos = { w: [-1, -1], b: [-1, -1] };
+ const rows = V.ParseFen(fen).position.split("/");
+ for (let i = 0; i < rows.length; i++) {
+ let k = 0; //column index on board
+ for (let j = 0; j < rows[i].length; j++) {
+ const piece = rows[i].charAt(j);
+ if (['j','k','l'].includes(piece.toLowerCase())) {
+ const color = (piece.charCodeAt(0) <= 90 ? 'w' : 'b');
+ this.kingPos[color] = [i, k];
+ }
+ else {
+ const num = parseInt(rows[i].charAt(j), 10);
+ if (!isNaN(num)) k += num - 1;
+ }
+ k++;
+ }
+ }
+ }
+
+ // If piece not in usual list, bishop or knight appears.
+ getPotentialMovesFrom([x, y]) {
+ if (this.movesCount <= 3) {
+ // Setup stage
+ const color = this.getColor(x, y);
+ const firstRank = (color == 'w' ? 7 : 0);
+ if (x != firstRank || V.AUGMENTED_PIECES.includes(this.board[x][y][1]))
+ return [];
+ const piece = this.getPiece(x, y);
+ const move = new Move({
+ appear: [
+ new PiPo({ x: x, y: y, c: color, p: this.getAugmented(piece) })
+ ],
+ vanish: [
+ new PiPo({ x: x, y: y, c: color, p: piece })
+ ],
+ start: { x: x, y: y },
+ end: { x: x, y: y }
+ });
+ return [move];
+ }
+ let moves = super.getPotentialMovesFrom([x, y]);
+ const initialPiece = this.getPiece(x, y);