Fix Circular moves animation
[xogo.git] / variants / Circular / class.js
1 import ChessRules from "/base_rules.js";
2 import {FenUtil} from "/utils/setupPieces.js";
3
4 export default class CircularRules extends ChessRules {
5
6 get hasCastle() {
7 return false;
8 }
9 get hasEnpassant() {
10 return false;
11 }
12
13 // Everypawn is going up!
14 getPawnShift(color) {
15 return -1;
16 }
17 isPawnInitRank(x, color) {
18 return (color == 'w' && x == 6) || (color == 'b' && x == 2);
19 }
20
21 get flippedBoard() {
22 return false;
23 }
24
25 // Circular board:
26 getX(x) {
27 let res = x % this.size.x;
28 if (res < 0)
29 res += this.size.x;
30 return res;
31 }
32
33 // TODO: rewrite in more elegant way
34 getFlagsFen() {
35 let flags = "";
36 for (let c of ["w", "b"]) {
37 for (let i = 0; i < 8; i++)
38 flags += this.pawnFlags[c][i] ? "1" : "0";
39 }
40 return flags;
41 }
42
43 setFlags(fenflags) {
44 this.pawnFlags = {
45 w: [...Array(8)], //pawns can move 2 squares?
46 b: [...Array(8)]
47 };
48 for (let c of ["w", "b"]) {
49 for (let i = 0; i < 8; i++)
50 this.pawnFlags[c][i] = fenflags.charAt((c == "w" ? 0 : 8) + i) == "1";
51 }
52 }
53
54 genRandInitBaseFen() {
55 let setupOpts = {
56 randomness: this.options["randomness"],
57 diffCol: ['b']
58 };
59 const s = FenUtil.setupPieces(
60 ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], setupOpts);
61 return {
62 fen: "8/8/pppppppp/" + s.b.join("") + "/8/8/PPPPPPPP/" +
63 s.w.join("").toUpperCase(),
64 o: {flags: "11111111"}
65 };
66 }
67
68 filterValid(moves) {
69 const filteredMoves = super.filterValid(moves);
70 // If at least one full move made, everything is allowed:
71 if (this.movesCount >= 2)
72 return filteredMoves;
73 // Else, forbid checks:
74 const oppCol = C.GetOppTurn(this.turn);
75 const oppKingPos = this.searchKingPos(oppCol);
76 return filteredMoves.filter(m => {
77 this.playOnBoard(m);
78 const res = !this.underCheck(oppKingPos, [this.turn]);
79 this.undoOnBoard(m);
80 return res;
81 });
82 }
83
84 prePlay(move) {
85 if (move.appear.length > 0 && move.vanish.length > 0) {
86 super.prePlay(move);
87 if (
88 [2, 6].includes(move.start.x) &&
89 move.vanish[0].p == 'p' &&
90 Math.abs(move.end.x - move.start.x) == 2
91 ) {
92 // This move turns off a 2-squares pawn flag
93 this.pawnFlags[move.start.x == 6 ? "w" : "b"][move.start.y] = false;
94 }
95 }
96 }
97
98 };