1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export class AlapoRules
extends ChessRules
{
7 static get HasFlags() {
11 static get HasEnpassant() {
23 return [V
.ROOK
, V
.BISHOP
, V
.QUEEN
, V
.ROOK_S
, V
.BISHOP_S
, V
.QUEEN_S
];
29 static get BISHOP_S() {
32 static get QUEEN_S() {
36 getPotentialMinirookMoves(sq
) {
37 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
], "oneStep");
39 getPotentialMinibishopMoves(sq
) {
40 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], "oneStep");
42 getPotentialMiniqueenMoves(sq
) {
44 super.getSlideNJumpMoves(
45 sq
, V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]), "oneStep")
49 getPotentialMovesFrom(sq
) {
50 switch (this.getPiece(sq
[0], sq
[1])) {
51 case V
.ROOK: return super.getPotentialRookMoves(sq
);
52 case V
.BISHOP: return super.getPotentialBishopMoves(sq
);
53 case V
.QUEEN: return super.getPotentialQueenMoves(sq
);
54 case V
.ROOK_S: return this.getPotentialMinirookMoves(sq
);
55 case V
.BISHOP_S: return this.getPotentialMinibishopMoves(sq
);
56 case V
.QUEEN_S: return this.getPotentialMiniqueenMoves(sq
);
62 return { x: 6, y: 6 };
65 getPpath(b
, color
, score
, orientation
) {
66 // 'i' for "inversed":
67 const suffix
= (b
[0] == orientation
? "" : "i");
68 return "Alapo/" + b
+ suffix
;
71 static GenRandInitFen(randomness
) {
73 return "rbqqbr/tcssct/6/6/TCSSCT/RBQQBR w 0";
81 let pieces
= { w: new Array(6), b: new Array(6) };
82 // Shuffle pieces on first (and last rank if randomness == 2)
83 for (let c
of ["w", "b"]) {
84 if (c
== 'b' && randomness
== 1) {
85 pieces
['b'] = pieces
['w'];
89 let positions
= ArrayFun
.range(6);
91 // Get random squares for bishops
92 let randIndex
= 2 * randInt(3);
93 const bishop1Pos
= positions
[randIndex
];
94 let randIndex_tmp
= 2 * randInt(3) + 1;
95 const bishop2Pos
= positions
[randIndex_tmp
];
96 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
97 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
99 // Get random square for queens
100 randIndex
= randInt(4);
101 const queen1Pos
= positions
[randIndex
];
102 positions
.splice(randIndex
, 1);
103 randIndex
= randInt(3);
104 const queen2Pos
= positions
[randIndex
];
105 positions
.splice(randIndex
, 1);
107 // Rooks positions are now fixed,
108 const rook1Pos
= positions
[0];
109 const rook2Pos
= positions
[1];
111 pieces
[c
][rook1Pos
] = "r";
112 pieces
[c
][bishop1Pos
] = "b";
113 pieces
[c
][queen1Pos
] = "q";
114 pieces
[c
][queen2Pos
] = "q";
115 pieces
[c
][bishop2Pos
] = "b";
116 pieces
[c
][rook2Pos
] = "r";
120 pieces
["b"].join("") + "/" +
121 pieces
["b"].map(p
=> piece2pawn
[p
]).join("") +
123 pieces
["w"].map(p
=> piece2pawn
[p
].toUpperCase()).join("") + "/" +
124 pieces
["w"].join("").toUpperCase() +
129 static IsGoodPosition(position
) {
130 if (position
.length
== 0) return false;
131 const rows
= position
.split("/");
132 if (rows
.length
!= V
.size
.x
) return false;
133 // Just check that at least one piece of each color is there:
134 let pieces
= { "w": 0, "b": 0 };
135 for (let row
of rows
) {
137 for (let i
= 0; i
< row
.length
; i
++) {
138 const lowerRi
= row
[i
].toLowerCase();
139 if (V
.PIECES
.includes(lowerRi
)) {
140 pieces
[row
[i
] == lowerRi
? "b" : "w"]++;
144 const num
= parseInt(row
[i
], 10);
145 if (isNaN(num
)) return false;
149 if (sumElts
!= V
.size
.y
) return false;
151 if (Object
.values(pieces
).some(v
=> v
== 0)) return false;
155 // Find possible captures by opponent on [x, y]
156 findCaptures([x
, y
]) {
157 const color
= this.getColor(x
, y
);
159 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
160 const oppCol
= V
.GetOppCol(color
);
161 for (let loop
= 0; loop
< steps
.length
; loop
++) {
162 const step
= steps
[loop
];
166 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
173 this.board
[i
][j
] != V
.EMPTY
&&
174 this.getColor(i
, j
) == oppCol
176 const oppPiece
= this.getPiece(i
, j
);
180 [V
.ROOK_S
, V
.BISHOP_S
, V
.QUEEN_S
].includes(oppPiece
)
184 [V
.BISHOP
, V
.BISHOP_S
].includes(oppPiece
) &&
185 step
.some(e
=> e
== 0)
189 [V
.ROOK
, V
.ROOK_S
].includes(oppPiece
) &&
190 step
.every(e
=> e
!= 0)
212 // Try both colors (to detect potential suicides)
213 for (let c
of ['w', 'b']) {
214 const oppCol
= V
.GetOppCol(c
);
215 const goal
= (c
== 'w' ? 0 : 5);
217 this.board
[goal
].some(
218 (b
,j
) => b
[0] == c
&& !this.findCaptures([goal
, j
])
221 return c
== 'w' ? "1-0" : "0-1";
224 return super.getCurrentScore();
227 static get VALUES() {