Finish code refactoring to generate initial positions (untested)
[xogo.git] / variants / Arena / class.js
1 import ChessRules from "/base_rules.js";
2
3 export default class ArenaRules extends ChessRules {
4
5 static get Options() {
6 return {}; //TODO
7 }
8
9 get hasFlags() {
10 return false;
11 }
12
13 getSvgChessboard() {
14 let board = super.getSvgChessboard().slice(0, -6);
15 // Add lines to delimitate the central area
16 board += `
17 <line x1="0" y1="20" x2="80" y2="20" stroke="black" stroke-width="0.15"/>
18 <line x1="0" y1="60" x2="80" y2="60" stroke="black" stroke-width="0.15"/>
19 </svg>`;
20 return board;
21 }
22
23 pieces(color, x, y) {
24 let allSpecs = super.pieces(color, x, y);
25 let pawnSpec = allSpecs['p'],
26 queenSpec = allSpecs['q'],
27 kingSpec = allSpecs['k'];
28 const pawnShift = (color == "w" ? -1 : 1);
29 Array.prototype.push.apply(pawnSpec.attack[0].steps,
30 [[-pawnShift, 1], [-pawnShift, -1]]);
31 queenSpec.both[0].range = 3;
32 kingSpec.both[0].range = 3;
33 return Object.assign({},
34 allSpecs,
35 {
36 'p': pawnSpec,
37 'q': queenSpec,
38 'k': kingSpec
39 }
40 );
41 }
42
43 static InArena(x) {
44 return Math.abs(3.5 - x) <= 1.5;
45 }
46
47 getPotentialMovesFrom([x, y]) {
48 const moves = super.getPotentialMovesFrom([x, y]);
49 // Eliminate moves which neither enter the arena or capture something
50 return moves.filter(m => {
51 const startInArena = V.InArena(m.start.x);
52 const endInArena = V.InArena(m.end.x);
53 return (
54 (startInArena && endInArena && m.vanish.length == 2) ||
55 (!startInArena && endInArena)
56 );
57 });
58 }
59
60 filterValid(moves) {
61 // No check conditions
62 return moves;
63 }
64
65 getCurrentScore() {
66 const color = this.turn;
67 if (!this.atLeastOneMove(color))
68 // I cannot move anymore
69 return color == 'w' ? "0-1" : "1-0";
70 // Win if the opponent has no more pieces left (in the Arena),
71 // (and/)or if he lost both his dukes.
72 let someUnitRemain = false,
73 atLeastOneDuke = false,
74 somethingInArena = false;
75 outerLoop: for (let i=0; i<this.size.x; i++) {
76 for (let j=0; j<this.size.y; j++) {
77 if (this.getColor(i,j) == color) {
78 someUnitRemain = true;
79 if (this.movesCount >= 2 && V.InArena(i)) {
80 somethingInArena = true;
81 if (atLeastOneDuke)
82 break outerLoop;
83 }
84 if (['q', 'k'].includes(this.getPiece(i, j))) {
85 atLeastOneDuke = true;
86 if (this.movesCount < 2 || somethingInArena)
87 break outerLoop;
88 }
89 }
90 }
91 }
92 if (
93 !someUnitRemain ||
94 !atLeastOneDuke ||
95 (this.movesCount >= 2 && !somethingInArena)
96 ) {
97 return color == 'w' ? "0-1" : "1-0";
98 }
99 return "*";
100 }
101
102 };