Fix parseInt() usage, rename Doubleorda --> Ordamirror, implement Clorange variant
[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 {
6 static get HasFlags() {
7 return false;
8 }
9
10 static get PawnSpecs() {
11 return Object.assign(
12 {},
13 ChessRules.PawnSpecs,
14 { promotions: ChessRules.PawnSpecs.promotions.concat([V.KING]) }
15 );
16 }
17
18 static get Lines() {
19 return [
20 // White goal:
21 [[0, 3], [0, 5]],
22 [[0, 5], [1, 5]],
23 [[1, 5], [1, 3]],
24 [[1, 3], [0, 3]],
25 // Black goal:
26 [[8, 3], [8, 5]],
27 [[8, 5], [7, 5]],
28 [[7, 5], [7, 3]],
29 [[7, 3], [8, 3]]
30 ];
31 }
32
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) {
40 let sumElts = 0;
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"]++;
45 sumElts++;
46 } else {
e50a8025 47 const num = parseInt(row[i], 10);
107dc1bd
BA
48 if (isNaN(num)) return false;
49 sumElts += num;
50 }
51 }
52 if (sumElts != V.size.y) return false;
53 }
54 if (Object.values(pieces).some(v => v == 0)) return false;
55 return true;
56 }
57
58 scanKings() {}
59
60 filterValid(moves) {
61 return moves;
62 }
63
64 getCheckSquares() {
65 return [];
66 }
67
68 // No variables update because no royal king + no castling
69 prePlay() {}
70 postPlay() {}
71 preUndo() {}
72 postUndo() {}
73
74 getCurrentScore() {
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 "*";
80 return "1/2";
81 }
b9ce3d0f
BA
82
83 static GenRandInitFen(randomness) {
84 if (randomness == 0)
85 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 -";
86
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'];
91 break;
92 }
93
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]];
103 }
104 }
105 for (let i = 0; i < 8; i++) pieces[c][positions[i]] = composition[i];
106 }
107 return (
108 pieces["b"].join("") +
109 "/pppppppp/8/8/8/8/PPPPPPPP/" +
110 pieces["w"].join("").toUpperCase() +
111 // En-passant allowed, but no flags
112 " w 0 -"
113 );
114 }
107dc1bd 115};