Advance on Chakart, fix Football GenRandInitFen() + try fixing newly introduced bug...
[vchess.git] / client / src / variants / Football.js
CommitLineData
107dc1bd
BA
1import { ChessRules } from "@/base_rules";
2
3export class FootballRules extends ChessRules {
4 static get HasFlags() {
5 return false;
6 }
7
8 static get PawnSpecs() {
9 return Object.assign(
10 {},
11 ChessRules.PawnSpecs,
12 { promotions: ChessRules.PawnSpecs.promotions.concat([V.KING]) }
13 );
14 }
15
16 static get Lines() {
17 return [
18 // White goal:
19 [[0, 3], [0, 5]],
20 [[0, 5], [1, 5]],
21 [[1, 5], [1, 3]],
22 [[1, 3], [0, 3]],
23 // Black goal:
24 [[8, 3], [8, 5]],
25 [[8, 5], [7, 5]],
26 [[7, 5], [7, 3]],
27 [[7, 3], [8, 3]]
28 ];
29 }
30
31 static IsGoodPosition(position) {
32 if (position.length == 0) return false;
33 const rows = position.split("/");
34 if (rows.length != V.size.x) return false;
35 // Just check that at least one piece of each color is there:
36 let pieces = { "w": 0, "b": 0 };
37 for (let row of rows) {
38 let sumElts = 0;
39 for (let i = 0; i < row.length; i++) {
40 const lowerRi = row[i].toLowerCase();
41 if (V.PIECES.includes(lowerRi)) {
42 pieces[row[i] == lowerRi ? "b" : "w"]++;
43 sumElts++;
44 } else {
45 const num = parseInt(row[i]);
46 if (isNaN(num)) return false;
47 sumElts += num;
48 }
49 }
50 if (sumElts != V.size.y) return false;
51 }
52 if (Object.values(pieces).some(v => v == 0)) return false;
53 return true;
54 }
55
56 scanKings() {}
57
58 filterValid(moves) {
59 return moves;
60 }
61
62 getCheckSquares() {
63 return [];
64 }
65
66 // No variables update because no royal king + no castling
67 prePlay() {}
68 postPlay() {}
69 preUndo() {}
70 postUndo() {}
71
72 getCurrentScore() {
73 const oppCol = V.GetOppCol(this.turn);
74 const goal = (oppCol == 'w' ? 0 : 7);
75 if (this.board[goal].slice(3, 5).some(b => b[0] == oppCol))
76 return oppCol == 'w' ? "1-0" : "0-1";
77 if (this.atLeastOneMove()) return "*";
78 return "1/2";
79 }
b9ce3d0f
BA
80
81 static GenRandInitFen(randomness) {
82 if (randomness == 0)
83 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 -";
84
85 let pieces = { w: new Array(8), b: new Array(8) };
86 for (let c of ["w", "b"]) {
87 if (c == 'b' && randomness == 1) {
88 pieces['b'] = pieces['w'];
89 break;
90 }
91
92 // Get random squares for every piece, totally freely
93 let positions = shuffle(ArrayFun.range(8));
94 const composition = ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q'];
95 const rem2 = positions[0] % 2;
96 if (rem2 == positions[1] % 2) {
97 // Fix bishops (on different colors)
98 for (let i=2; i<8; i++) {
99 if (positions[i] % 2 != rem2)
100 [positions[1], positions[i]] = [positions[i], positions[1]];
101 }
102 }
103 for (let i = 0; i < 8; i++) pieces[c][positions[i]] = composition[i];
104 }
105 return (
106 pieces["b"].join("") +
107 "/pppppppp/8/8/8/8/PPPPPPPP/" +
108 pieces["w"].join("").toUpperCase() +
109 // En-passant allowed, but no flags
110 " w 0 -"
111 );
112 }
107dc1bd 113};