1 import { ChessRules
} from "@/base_rules";
3 export class AlapoRules
extends ChessRules
{
5 static get HasFlags() {
9 static get HasEnpassant() {
21 return [V
.ROOK
, V
.BISHOP
, V
.QUEEN
, V
.ROOK_S
, V
.BISHOP_S
, V
.QUEEN_S
];
27 static get BISHOP_S() {
30 static get QUEEN_S() {
34 getPotentialMinirookMoves(sq
) {
35 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
], "oneStep");
37 getPotentialMinibishopMoves(sq
) {
38 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], "oneStep");
40 getPotentialMiniqueenMoves(sq
) {
42 super.getSlideNJumpMoves(
43 sq
, V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]), "oneStep")
47 getPotentialMovesFrom(sq
) {
48 switch (this.getPiece(sq
[0], sq
[1])) {
49 case V
.ROOK: return super.getPotentialRookMoves(sq
);
50 case V
.BISHOP: return super.getPotentialBishopMoves(sq
);
51 case V
.QUEEN: return super.getPotentialQueenMoves(sq
);
52 case V
.ROOK_S: return this.getPotentialMinirookMoves(sq
);
53 case V
.BISHOP_S: return this.getPotentialMinibishopMoves(sq
);
54 case V
.QUEEN_S: return this.getPotentialMiniqueenMoves(sq
);
60 return { x: 6, y: 6 };
63 getPpath(b
, color
, score
, orientation
) {
64 // 'i' for "inversed":
65 const suffix
= (b
[0] == orientation
? "" : "i");
66 return "Alapo/" + b
+ suffix
;
69 static GenRandInitFen(randomness
) {
71 return "rbqqbr/tcssct/6/6/TCSSCT/RBQQBR w 0";
79 let pieces
= { w: new Array(6), b: new Array(6) };
80 // Shuffle pieces on first (and last rank if randomness == 2)
81 for (let c
of ["w", "b"]) {
82 if (c
== 'b' && randomness
== 1) {
83 pieces
['b'] = pieces
['w'];
87 let positions
= ArrayFun
.range(6);
89 // Get random squares for bishops
90 let randIndex
= 2 * randInt(3);
91 const bishop1Pos
= positions
[randIndex
];
92 let randIndex_tmp
= 2 * randInt(3) + 1;
93 const bishop2Pos
= positions
[randIndex_tmp
];
94 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
95 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
97 // Get random square for queens
98 randIndex
= randInt(4);
99 const queen1Pos
= positions
[randIndex
];
100 positions
.splice(randIndex
, 1);
101 randIndex
= randInt(3);
102 const queen2Pos
= positions
[randIndex
];
103 positions
.splice(randIndex
, 1);
105 // Rooks positions are now fixed,
106 const rook1Pos
= positions
[0];
107 const rook2Pos
= positions
[1];
109 pieces
[c
][rook1Pos
] = "r";
110 pieces
[c
][bishop1Pos
] = "b";
111 pieces
[c
][queen1Pos
] = "q";
112 pieces
[c
][queen2Pos
] = "q";
113 pieces
[c
][bishop2Pos
] = "b";
114 pieces
[c
][rook2Pos
] = "r";
117 pieces
["b"].join("") + "/" +
118 pieces
["b"].map(p
=> piece2pawn
[p
]).join() +
120 pieces
["w"].map(p
=> piece2pawn
[p
].toUpperCase()).join() + "/" +
121 pieces
["w"].join("").toUpperCase() +
126 static IsGoodPosition(position
) {
127 if (position
.length
== 0) return false;
128 const rows
= position
.split("/");
129 if (rows
.length
!= V
.size
.x
) return false;
130 // Just check that at least one piece of each color is there:
131 let pieces
= { "w": 0, "b": 0 };
132 for (let row
of rows
) {
134 for (let i
= 0; i
< row
.length
; i
++) {
135 const lowerRi
= row
[i
].toLowerCase();
136 if (V
.PIECES
.includes(lowerRi
)) {
137 pieces
[row
[i
] == lowerRi
? "b" : "w"]++;
141 const num
= parseInt(row
[i
], 10);
142 if (isNaN(num
)) return false;
146 if (sumElts
!= V
.size
.y
) return false;
148 if (Object
.values(pieces
).some(v
=> v
== 0)) return false;
152 // Find possible captures by opponent on [x, y]
153 findCaptures([x
, y
]) {
154 const color
= this.getColor(x
, y
);
156 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
157 const oppCol
= V
.GetOppCol(color
);
158 for (let loop
= 0; loop
< steps
.length
; loop
++) {
159 const step
= steps
[loop
];
163 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
170 this.board
[i
][j
] != V
.EMPTY
&&
171 this.getColor(i
, j
) == oppCol
173 const oppPiece
= this.getPiece(i
, j
);
177 [V
.ROOK_S
, V
.BISHOP_S
, V
.QUEEN_S
].includes(oppPiece
)
181 [V
.BISHOP
, V
.BISHOP_S
].includes(oppPiece
) &&
182 step
.some(e
=> e
== 0)
186 [V
.ROOK
, V
.ROOK_S
].includes(oppPiece
) &&
187 step
.every(e
=> e
!= 0)
209 // Try both colors (to detect potential suicides)
210 for (let c
of ['w', 'b']) {
211 const oppCol
= V
.GetOppCol(c
);
212 const goal
= (c
== 'w' ? 0 : 5);
214 this.board
[goal
].some(
215 (b
,j
) => b
[0] == c
&& !this.findCaptures([goal
, j
])
218 return c
== 'w' ? "1-0" : "0-1";
221 return super.getCurrentScore();
224 static get VALUES() {