Draft Circular chess. Move animation not per segments for now
[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 // TODO: graph with segments, as for cylindrical...
27 getX(x) {
28 let res = x % this.size.x;
29 if (res < 0)
30 res += this.size.x;
31 return res;
32 }
33
34 // TODO: rewrite in more elegant way
35 getFlagsFen() {
36 let flags = "";
37 for (let c of ["w", "b"]) {
38 for (let i = 0; i < 8; i++)
39 flags += this.pawnFlags[c][i] ? "1" : "0";
40 }
41 return flags;
42 }
43
44 setFlags(fenflags) {
45 this.pawnFlags = {
46 w: [...Array(8)], //pawns can move 2 squares?
47 b: [...Array(8)]
48 };
49 for (let c of ["w", "b"]) {
50 for (let i = 0; i < 8; i++)
51 this.pawnFlags[c][i] = fenflags.charAt((c == "w" ? 0 : 8) + i) == "1";
52 }
53 }
54
55 genRandInitBaseFen() {
56 let setupOpts = {
57 randomness: this.options["randomness"],
58 diffCol: ['b']
59 };
60 const s = FenUtil.setupPieces(
61 ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], setupOpts);
62 return {
63 fen: "8/8/pppppppp/" + s.b.join("") + "/8/8/PPPPPPPP/" +
64 s.w.join("").toUpperCase(),
65 o: {flags: "11111111"}
66 };
67 }
68
69 filterValid(moves) {
70 const filteredMoves = super.filterValid(moves);
71 // If at least one full move made, everything is allowed:
72 if (this.movesCount >= 2)
73 return filteredMoves;
74 // Else, forbid checks:
75 const oppCol = C.GetOppTurn(this.turn);
76 const oppKingPos = this.searchKingPos(oppCol);
77 return filteredMoves.filter(m => {
78 this.playOnBoard(m);
79 const res = !this.underCheck(oppKingPos, [this.turn]);
80 this.undoOnBoard(m);
81 return res;
82 });
83 }
84
85 prePlay(move) {
86 if (move.appear.length > 0 && move.vanish.length > 0) {
87 super.prePlay(move);
88 if (
89 [2, 6].includes(move.start.x) &&
90 move.vanish[0].p == 'p' &&
91 Math.abs(move.end.x - move.start.x) == 2
92 ) {
93 // This move turns off a 2-squares pawn flag
94 this.pawnFlags[move.start.x == 6 ? "w" : "b"][move.start.y] = false;
95 }
96 }
97 }
98
99 };