Fix FenUtil
[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 =
34 FenUtil.setupPieces(['r', 'b', 'q', 'q', 'b', 'r'], {diffCol: ['b']});
35 const piece2pawn = {
36 r: 't',
37 q: 's',
38 b: 'c'
39 };
40 const fen = (
41 s.b.join("") + "/" +
42 s.b.map(p => piece2pawn[p]).join("") +
43 "/6/6/" +
44 s.w.map(p => piece2pawn[p].toUpperCase()).join("") + "/" +
45 s.w.join("").toUpperCase()
46 );
47 return { fen: fen, o: {} };
48 }
49
50 // Triangles are rotated from opponent viewpoint (=> suffix "_inv")
51 pieces(color, x, y) {
52 const allSpecs = super.pieces(color, x, y);
53 return {
54 'r': allSpecs['r'],
55 'q': allSpecs['q'],
56 'b': Object.assign({}, allSpecs['b'],
57 {"class": "bishop" + (this.playerColor != color ? "_inv" : "")}),
58 's': { //"square"
59 "class": "babyrook",
60 both: [
61 {
62 steps: [[0, 1], [0, -1], [1, 0], [-1, 0]],
63 range: 1
64 }
65 ]
66 },
67 'c': { //"circle"
68 "class": "babyqueen",
69 both: [
70 {
71 steps: [
72 [0, 1], [0, -1], [1, 0], [-1, 0],
73 [1, 1], [1, -1], [-1, 1], [-1, -1]
74 ],
75 range: 1
76 }
77 ]
78 },
79 't': { //"triangle"
80 "class": "babybishop" + (this.playerColor != color ? "_inv" : ""),
81 both: [
82 {
83 steps: [[1, 1], [1, -1], [-1, 1], [-1, -1]],
84 range: 1
85 }
86 ]
87 }
88 };
89 }
90
91 get size() {
92 return {
93 x: 6,
94 y: 6
95 };
96 }
97
98 filterValid(moves) {
99 return moves;
100 }
101
102 getCurrentScore() {
103 // Try both colors (to detect potential suicides)
104 let won = {};
105 for (let c of ['w', 'b']) {
106 const oppCol = C.GetOppCol(c);
107 const goal = (c == 'w' ? 0 : 5);
108 won[c] = this.board[goal].some((b,j) => {
109 return (
110 this.getColor(goal, j) == c &&
111 !this.findCapturesOn(
112 [goal, j],
113 {
114 one: true,
115 oppCol: oppCol,
116 segments: this.options["cylinder"]
117 }
118 )
119 );
120 });
121 }
122 if (won['w'] && won['b'])
123 return "?"; //no idea who won, not relevant anyway :)
124 return (won['w'] ? "1-0" : (won['b'] ? "0-1" : "*"));
125 }
126
127 };