6a66a068d4542e789a969206f328680d48895b48
[xogo.git] / variants / Suction / class.js
1 import ChessRules from "/base_rules.js";
2 import {FenUtil} from "/utils/setupPieces.js";
3 import PiPo from "/utils/PiPo.js";
4 import Move from "/utils/Move.js";
5
6 export default class SuctionRules extends ChessRules {
7
8 static get Options() {
9 return {
10 select: C.Options.select,
11 styles: [
12 "balance",
13 "capture",
14 "cylinder",
15 "dark",
16 "doublemove",
17 "madrasi",
18 "progressive",
19 "teleport"
20 ]
21 };
22 }
23
24 get pawnPromotions() {
25 return ['p']; //no promotions
26 }
27
28 get hasFlags() {
29 return false;
30 }
31
32 setOtherVariables(fenParsed) {
33 super.setOtherVariables(fenParsed);
34 this.cmove = null;
35 const cmove_str = fenParsed.cmove;
36 if (cmove_str != "-") {
37 this.cmove = {
38 start: C.SquareToCoords(cmove_str.substr(0, 2)),
39 end: C.SquareToCoords(cmove_str.substr(2))
40 };
41 }
42 }
43
44 genRandInitBaseFen() {
45 const s = FenUtil.setupPieces(
46 ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], {diffCol: ['b']});
47 return {
48 fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" +
49 s.w.join("").toUpperCase(),
50 o: {}
51 };
52 }
53
54 getPartFen(o) {
55 let parts = super.getPartFen(o);
56 const cmoveFen = o.init || !this.cmove
57 ? "-"
58 : C.CoordsToSquare(this.cmove.start) + C.CoordsToSquare(this.cmove.end);
59 parts["cmove"] = cmoveFen;
60 return parts;
61 }
62
63 getBasicMove([sx, sy], [ex, ey]) {
64 let move = super.getBasicMove([sx, sy], [ex, ey]);
65 if (move.vanish.length == 2) {
66 move.appear.push(
67 new PiPo({
68 x: sx,
69 y: sy,
70 c: move.vanish[1].c,
71 p: move.vanish[1].p
72 })
73 );
74 }
75 return move;
76 }
77
78 canIplay(x, y) {
79 return this.getPiece(x, y) != 'k' && super.canIplay(x, y);
80 }
81
82 // Does m2 un-do m1 ? (to disallow undoing captures)
83 oppositeMoves(m1, m2) {
84 return (
85 !!m1 &&
86 m2.vanish.length == 2 &&
87 m1.start.x == m2.start.x &&
88 m1.end.x == m2.end.x &&
89 m1.start.y == m2.start.y &&
90 m1.end.y == m2.end.y
91 );
92 }
93
94 filterValid(moves) {
95 return moves.filter(m => !this.oppositeMoves(this.cmove, m));
96 }
97
98 postPlay(move) {
99 super.postPlay(move);
100 this.cmove =
101 (move.vanish.length == 2 ? {start: move.start, end: move.end} : null);
102 }
103
104 atLeastOneMove() {
105 return true;
106 }
107
108 getCurrentScore() {
109 const color = this.turn;
110 const kingPos = super.searchKingPos(color);
111 if (color == "w" && kingPos[0][0] == 0) return "0-1";
112 if (color == "b" && kingPos[0][0] == this.size.x - 1) return "1-0";
113 // King is not on the opposite edge: game not over
114 return "*";
115 }
116
117 // Better animation for swaps
118 customAnimate(move, segments, cb) {
119 if (move.vanish.length < 2)
120 return 0;
121 super.animateMoving(move.end, move.start, null,
122 segments.reverse().map(s => s.reverse()), cb);
123 return 1;
124 }
125
126 };