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