Fix FenUtil.setupPieces and Antiking variants
[xogo.git] / variants / Alapo / class.js
1 import ChessRules from "/base_rules.js";
2 import {ArrayFun} from "/utils/array.js";
3 import {Random} from "/utils/alea.js";
4 import {FenUtil} from "/utils/setupPieces.js";
5
6 export default class AlapoRules extends ChessRules {
7
8 static get Options() {
9 return {
10 select: C.Options.select,
11 styles: C.Options.styles.filter(s => s != "teleport")
12 };
13 }
14
15 get hasFlags() {
16 return false;
17 }
18 get hasEnpassant() {
19 return false;
20 }
21
22 getSvgChessboard() {
23 let board = super.getSvgChessboard().slice(0, -6);
24 // Add lines to delimitate goals
25 board += `
26 <line x1="0" y1="10" x2="60" y2="10" stroke="black" stroke-width="0.1"/>
27 <line x1="0" y1="50" x2="60" y2="50" stroke="black" stroke-width="0.1"/>
28 </svg>`;
29 return board;
30 }
31
32 genRandInitBaseFen() {
33 const s = FenUtil.setupPieces(
34 ['r', 'b', 'q', 'q', 'b', 'r'],
35 {
36 randomness: this.options["randomness"],
37 diffCol: ['b']
38 }
39 );
40 const piece2pawn = {
41 r: 't',
42 q: 's',
43 b: 'c'
44 };
45 const fen = (
46 s.b.join("") + "/" +
47 s.b.map(p => piece2pawn[p]).join("") +
48 "/6/6/" +
49 s.w.map(p => piece2pawn[p].toUpperCase()).join("") + "/" +
50 s.w.join("").toUpperCase()
51 );
52 return { fen: fen, o: {} };
53 }
54
55 // Triangles are rotated from opponent viewpoint (=> suffix "_inv")
56 pieces(color, x, y) {
57 const allSpecs = super.pieces(color, x, y);
58 return {
59 'r': allSpecs['r'],
60 'q': allSpecs['q'],
61 'b': Object.assign({}, allSpecs['b'],
62 {"class": "bishop" + (this.playerColor != color ? "_inv" : "")}),
63 's': { //"square"
64 "class": "babyrook",
65 both: [
66 {
67 steps: [[0, 1], [0, -1], [1, 0], [-1, 0]],
68 range: 1
69 }
70 ]
71 },
72 'c': { //"circle"
73 "class": "babyqueen",
74 both: [
75 {
76 steps: [
77 [0, 1], [0, -1], [1, 0], [-1, 0],
78 [1, 1], [1, -1], [-1, 1], [-1, -1]
79 ],
80 range: 1
81 }
82 ]
83 },
84 't': { //"triangle"
85 "class": "babybishop" + (this.playerColor != color ? "_inv" : ""),
86 both: [
87 {
88 steps: [[1, 1], [1, -1], [-1, 1], [-1, -1]],
89 range: 1
90 }
91 ]
92 }
93 };
94 }
95
96 get size() {
97 return {
98 x: 6,
99 y: 6
100 };
101 }
102
103 filterValid(moves) {
104 return moves;
105 }
106
107 getCurrentScore() {
108 // Try both colors (to detect potential suicides)
109 let won = {};
110 for (let c of ['w', 'b']) {
111 const oppCol = C.GetOppCol(c);
112 const goal = (c == 'w' ? 0 : 5);
113 won[c] = this.board[goal].some((b,j) => {
114 return (
115 this.getColor(goal, j) == c &&
116 !this.findCapturesOn(
117 [goal, j],
118 {
119 one: true,
120 oppCol: oppCol,
121 segments: this.options["cylinder"]
122 }
123 )
124 );
125 });
126 }
127 if (won['w'] && won['b'])
128 return "?"; //no idea who won, not relevant anyway :)
129 return (won['w'] ? "1-0" : (won['b'] ? "0-1" : "*"));
130 }
131
132 };