1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { shuffle
} from "@/utils/alea";
5 export class FootballRules
extends ChessRules
{
7 static get HasFlags() {
11 static get PawnSpecs() {
15 { promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.KING
]) }
34 static IsGoodPosition(position
) {
35 if (position
.length
== 0) return false;
36 const rows
= position
.split("/");
37 if (rows
.length
!= V
.size
.x
) return false;
38 // Just check that at least one piece of each color is there:
39 let pieces
= { "w": 0, "b": 0 };
40 for (let row
of rows
) {
42 for (let i
= 0; i
< row
.length
; i
++) {
43 const lowerRi
= row
[i
].toLowerCase();
44 if (V
.PIECES
.includes(lowerRi
)) {
45 pieces
[row
[i
] == lowerRi
? "b" : "w"]++;
48 const num
= parseInt(row
[i
], 10);
49 if (isNaN(num
)) return false;
53 if (sumElts
!= V
.size
.y
) return false;
55 if (Object
.values(pieces
).some(v
=> v
== 0)) return false;
69 // No variables update because no royal king + no castling
76 const oppCol
= V
.GetOppCol(this.turn
);
77 const goal
= (oppCol
== 'w' ? 0 : 7);
78 if (this.board
[goal
].slice(3, 5).some(b
=> b
[0] == oppCol
))
79 return oppCol
== 'w' ? "1-0" : "0-1";
80 if (this.atLeastOneMove()) return "*";
84 static GenRandInitFen(randomness
) {
86 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 -";
88 let pieces
= { w: new Array(8), b: new Array(8) };
89 for (let c
of ["w", "b"]) {
90 if (c
== 'b' && randomness
== 1) {
91 pieces
['b'] = pieces
['w'];
95 // Get random squares for every piece, totally freely
96 let positions
= shuffle(ArrayFun
.range(8));
97 const composition
= ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q'];
98 const rem2
= positions
[0] % 2;
99 if (rem2
== positions
[1] % 2) {
100 // Fix bishops (on different colors)
101 for (let i
=2; i
<8; i
++) {
102 if (positions
[i
] % 2 != rem2
)
103 [positions
[1], positions
[i
]] = [positions
[i
], positions
[1]];
106 for (let i
= 0; i
< 8; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
109 pieces
["b"].join("") +
110 "/pppppppp/8/8/8/8/PPPPPPPP/" +
111 pieces
["w"].join("").toUpperCase() +
112 // En-passant allowed, but no flags