2eeeaeaf8be2fd356fbac89c6056b7a4bd351619
[vchess.git] / client / src / variants / Makruk.js
1 import { ChessRules, Move, PiPo } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3 import { randInt, shuffle } from "@/utils/alea";
4
5 export class MakrukRules extends ChessRules {
6 static get HasFlags() {
7 return false;
8 }
9
10 static get HasEnpassant() {
11 return false;
12 }
13
14 static get PawnSpecs() {
15 return Object.assign(
16 {},
17 ChessRules.PawnSpecs,
18 { promotions: [V.QUEEN] }
19 );
20 }
21
22 static get PIECES() {
23 return ChessRules.PIECES.concat(V.PROMOTED);
24 }
25
26 static get PROMOTED() {
27 return 'f';
28 }
29
30 static GenRandInitFen(randomness) {
31 if (randomness == 0)
32 return "rnbqkbnr/8/pppppppp/8/8/PPPPPPPP/8/RNBKQBNR w 0";
33
34 let pieces = { w: new Array(8), b: new Array(8) };
35 for (let c of ["w", "b"]) {
36 if (c == 'b' && randomness == 1) {
37 pieces['b'] = pieces['w'];
38 break;
39 }
40
41 // Get random squares for every piece, totally freely (no castling)
42 let positions = shuffle(ArrayFun.range(8));
43 const composition = ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q'];
44 for (let i = 0; i < 8; i++) pieces[c][positions[i]] = composition[i];
45 }
46 return (
47 pieces["b"].join("") +
48 "/8/pppppppp/8/8/PPPPPPPP/8/" +
49 pieces["w"].join("").toUpperCase() +
50 " w 0"
51 );
52 }
53
54 getPpath(b) {
55 return "Makruk/" + b;
56 }
57
58 getPotentialMovesFrom([x, y]) {
59 if (this.getPiece(x, y) == V.PROMOTED)
60 return this.getPotentialQueenMoves([x, y]);
61 return super.getPotentialMovesFrom([x, y]);
62 }
63
64 getPotentialPawnMoves([x, y]) {
65 const color = this.turn;
66 const shiftX = V.PawnSpecs.directions[color];
67 const sixthRank = (color == 'w' ? 2 : 5);
68 const tr = (x + shiftX == sixthRank ? { p: V.PROMOTED, c: color } : null);
69 let moves = [];
70 if (this.board[x + shiftX][y] == V.EMPTY)
71 // One square forward
72 moves.push(this.getBasicMove([x, y], [x + shiftX, y], tr));
73 // Captures
74 for (let shiftY of [-1, 1]) {
75 if (
76 y + shiftY >= 0 && y + shiftY < 8 &&
77 this.board[x + shiftX][y + shiftY] != V.EMPTY &&
78 this.canTake([x, y], [x + shiftX, y + shiftY])
79 ) {
80 moves.push(this.getBasicMove([x, y], [x + shiftX, y + shiftY], tr));
81 }
82 }
83 return moves;
84 }
85
86 getPotentialBishopMoves(sq) {
87 const forward = (this.turn == 'w' ? -1 : 1);
88 return this.getSlideNJumpMoves(
89 sq,
90 V.steps[V.BISHOP].concat([ [forward, 0] ]),
91 "oneStep"
92 );
93 }
94
95 getPotentialQueenMoves(sq) {
96 return this.getSlideNJumpMoves(
97 sq,
98 V.steps[V.BISHOP],
99 "oneStep"
100 );
101 }
102
103 isAttackedByBishop(sq, color) {
104 const forward = (color == 'w' ? 1 : -1);
105 return this.isAttackedBySlideNJump(
106 sq,
107 color,
108 V.BISHOP,
109 V.steps[V.BISHOP].concat([ [forward, 0] ]),
110 "oneStep"
111 );
112 }
113
114 isAttackedByQueen(sq, color) {
115 return this.isAttackedBySlideNJump(
116 sq,
117 color,
118 V.QUEEN,
119 V.steps[V.BISHOP],
120 "oneStep"
121 );
122 }
123
124 static get VALUES() {
125 return {
126 p: 1,
127 r: 5,
128 n: 3,
129 b: 3,
130 q: 2,
131 f: 2,
132 k: 1000
133 };
134 }
135 };