Finish code refactoring to generate initial positions (untested)
[xogo.git] / variants / Ambiguous / class.js
1 import ChessRules from "/base_rules.js";
2 import {FenUtil} from "/utils/setupPieces.js";
3
4 export default class AmbiguousRules extends ChessRules {
5
6 static get Options() {
7 return {
8 select: C.Options.select,
9 styles: ["cylinder"]
10 };
11 }
12
13 get hasFlags() {
14 return false;
15 }
16
17 setOtherVariables(fenParsed) {
18 super.setOtherVariables(fenParsed);
19 if (this.movesCount == 0)
20 this.subTurn = 2;
21 else
22 this.subTurn = 1;
23 }
24
25 genRandInitBaseFen() {
26 const s = FenUtil.setupPieces(
27 ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], {diffCol: ['b']});
28 return {
29 fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" +
30 s.w.join("").toUpperCase(),
31 o: {}
32 };
33 }
34
35 canStepOver(x, y) {
36 return this.board[x][y] == "" || this.getPiece(x, y) == V.GOAL;
37 }
38
39 // Subturn 1: play a move for the opponent on the designated square.
40 // Subturn 2: play a move for me (which just indicate a square).
41 getPotentialMovesFrom([x, y]) {
42 const color = this.turn;
43 const oppCol = C.GetOppCol(color);
44 if (this.subTurn == 2) {
45 // Just play a normal move (which in fact only indicate a square)
46 let movesHash = {};
47 return (
48 super.getPotentialMovesFrom([x, y])
49 .filter(m => {
50 // Filter promotions: keep only one, since no choice for now.
51 if (m.appear[0].p != m.vanish[0].p) {
52 const hash = C.CoordsToSquare(m.start) + C.CoordsToSquare(m.end);
53 if (!movesHash[hash]) {
54 movesHash[hash] = true;
55 return true;
56 }
57 return false;
58 }
59 return true;
60 })
61 .map(m => {
62 if (m.vanish.length == 1)
63 m.appear[0].p = V.GOAL;
64 else {
65 m.appear[0].p = V.TARGET_CODE[m.vanish[1].p];
66 m.appear[0].c = m.vanish[1].c;
67 }
68 m.vanish.shift();
69 return m;
70 })
71 );
72 }
73 // At subTurn == 1, play a targeted move for the opponent.
74 // Search for target:
75 let target = {x: -1, y: -1};
76 outerLoop: for (let i = 0; i < this.size.x; i++) {
77 for (let j = 0; j < this.size.y; j++) {
78 if (this.board[i][j] != "") {
79 const piece = this.getPiece(i, j);
80 if (
81 piece == V.GOAL ||
82 Object.keys(V.TARGET_DECODE).includes(piece)
83 ) {
84 target = {x: i, y:j};
85 break outerLoop;
86 }
87 }
88 }
89 }
90 const moves = super.getPotentialMovesFrom([x, y], oppCol);
91 return moves.filter(m => m.end.x == target.x && m.end.y == target.y);
92 }
93
94 canIplay(x, y) {
95 const color = this.getColor(x, y);
96 return (
97 (this.subTurn == 1 && ![this.turn, this.playerColor].includes(color)) ||
98 (this.subTurn == 2 && super.canIplay(x, y))
99 );
100 }
101
102 // Code for empty square target
103 static get GOAL() {
104 return 'g';
105 }
106
107 static get TARGET_DECODE() {
108 return {
109 's': 'p',
110 't': 'q',
111 'u': 'r',
112 'o': 'n',
113 'c': 'b',
114 'l': 'k'
115 };
116 }
117
118 static get TARGET_CODE() {
119 return {
120 'p': 's',
121 'q': 't',
122 'r': 'u',
123 'n': 'o',
124 'b': 'c',
125 'k': 'l'
126 };
127 }
128
129 pieces(color, x, y) {
130 const targets = {
131 's': {"class": "target-pawn"},
132 'u': {"class": "target-rook"},
133 'o': {"class": "target-knight"},
134 'c': {"class": "target-bishop"},
135 't': {"class": "target-queen"},
136 'l': {"class": "target-king"}
137 };
138 return Object.assign({ 'g': {"class": "target"} },
139 targets, super.pieces(color, x, y));
140 }
141
142 atLeastOneMove() {
143 // Since there are no checks this seems true (same as for Magnetic...)
144 return true;
145 }
146
147 filterValid(moves) {
148 return moves;
149 }
150
151 isKing(x, y, p) {
152 if (!p)
153 p = this.getPiece(x, y);
154 return ['k', 'l'].includes(p);
155 }
156
157 getCurrentScore() {
158 // This function is only called at subTurn 1
159 const color = C.GetOppCol(this.turn);
160 if (this.searchKingPos(color).length == 0)
161 return (color == 'w' ? "0-1" : "1-0");
162 return "*";
163 }
164
165 postPlay(move) {
166 const color = this.turn;
167 if (this.subTurn == 2 || this.searchKingPos(color).length == 0) {
168 this.turn = C.GetOppCol(color);
169 this.movesCount++;
170 }
171 this.subTurn = 3 - this.subTurn;
172 }
173
174 };