Add Relayup
[vchess.git] / client / src / variants / Football.js
CommitLineData
107dc1bd 1import { ChessRules } from "@/base_rules";
ad030c7d
BA
2import { ArrayFun } from "@/utils/array";
3import { shuffle } from "@/utils/alea";
107dc1bd
BA
4
5export class FootballRules extends ChessRules {
7e8a7ea1 6
107dc1bd
BA
7 static get HasFlags() {
8 return false;
9 }
10
11 static get PawnSpecs() {
12 return Object.assign(
13 {},
14 ChessRules.PawnSpecs,
15 { promotions: ChessRules.PawnSpecs.promotions.concat([V.KING]) }
16 );
17 }
18
19 static get Lines() {
20 return [
21 // White goal:
22 [[0, 3], [0, 5]],
23 [[0, 5], [1, 5]],
24 [[1, 5], [1, 3]],
25 [[1, 3], [0, 3]],
26 // Black goal:
27 [[8, 3], [8, 5]],
28 [[8, 5], [7, 5]],
29 [[7, 5], [7, 3]],
30 [[7, 3], [8, 3]]
31 ];
32 }
33
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) {
41 let sumElts = 0;
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"]++;
46 sumElts++;
47 } else {
e50a8025 48 const num = parseInt(row[i], 10);
107dc1bd
BA
49 if (isNaN(num)) return false;
50 sumElts += num;
51 }
52 }
53 if (sumElts != V.size.y) return false;
54 }
55 if (Object.values(pieces).some(v => v == 0)) return false;
56 return true;
57 }
58
59 scanKings() {}
60
61 filterValid(moves) {
62 return moves;
63 }
64
65 getCheckSquares() {
66 return [];
67 }
68
69 // No variables update because no royal king + no castling
70 prePlay() {}
71 postPlay() {}
72 preUndo() {}
73 postUndo() {}
74
75 getCurrentScore() {
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 "*";
81 return "1/2";
82 }
b9ce3d0f
BA
83
84 static GenRandInitFen(randomness) {
85 if (randomness == 0)
86 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 -";
87
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'];
92 break;
93 }
94
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]];
104 }
105 }
106 for (let i = 0; i < 8; i++) pieces[c][positions[i]] = composition[i];
107 }
108 return (
109 pieces["b"].join("") +
110 "/pppppppp/8/8/8/8/PPPPPPPP/" +
111 pieces["w"].join("").toUpperCase() +
112 // En-passant allowed, but no flags
113 " w 0 -"
114 );
115 }
7e8a7ea1 116
107dc1bd 117};