8176b957b0502eafa9fdd9a2e408fe4dbc514174
[vchess.git] / client / src / variants / Ordamirror.js
1 import { ChessRules } from "@/base_rules";
2 import { OrdaRules } from "@/variants/Orda";
3 import { ArrayFun } from "@/utils/array";
4 import { randInt } from "@/utils/alea";
5
6 export class OrdamirrorRules extends OrdaRules {
7
8 static get PawnSpecs() {
9 return Object.assign(
10 {},
11 ChessRules.PawnSpecs,
12 { promotions: [V.LANCER, V.ARCHER, V.KHESHIG, V.FALCON] }
13 );
14 }
15
16 static get HasFlags() {
17 return false;
18 }
19
20 static get HasEnpassant() {
21 return false;
22 }
23
24 getPpath(b) {
25 return "Orda/" + b;
26 }
27
28 static GenRandInitFen(randomness) {
29 if (randomness == 0)
30 return "lhafkahl/8/pppppppp/8/8/PPPPPPPP/8/LHAFKAHL w 0 ah -";
31
32 let pieces = { w: new Array(8), b: new Array(8) };
33 // Shuffle pieces on first (and last rank if randomness == 2)
34 for (let c of ["w", "b"]) {
35 if (c == 'b' && randomness == 1) {
36 pieces['b'] = pieces['w'];
37 break;
38 }
39
40 let positions = ArrayFun.range(8);
41
42 let randIndex = 2 * randInt(4);
43 const bishop1Pos = positions[randIndex];
44 let randIndex_tmp = 2 * randInt(4) + 1;
45 const bishop2Pos = positions[randIndex_tmp];
46 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
47 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
48
49 randIndex = randInt(6);
50 const knight1Pos = positions[randIndex];
51 positions.splice(randIndex, 1);
52 randIndex = randInt(5);
53 const knight2Pos = positions[randIndex];
54 positions.splice(randIndex, 1);
55
56 randIndex = randInt(4);
57 const queenPos = positions[randIndex];
58 positions.splice(randIndex, 1);
59
60 const rook1Pos = positions[0];
61 const kingPos = positions[1];
62 const rook2Pos = positions[2];
63
64 pieces[c][rook1Pos] = "l";
65 pieces[c][knight1Pos] = "h";
66 pieces[c][bishop1Pos] = "a";
67 pieces[c][queenPos] = "f";
68 pieces[c][kingPos] = "k";
69 pieces[c][bishop2Pos] = "a";
70 pieces[c][knight2Pos] = "h";
71 pieces[c][rook2Pos] = "l";
72 }
73 return (
74 pieces["b"].join("") +
75 "/8/pppppppp/8/8/PPPPPPPP/8/" +
76 pieces["w"].join("").toUpperCase() +
77 " w 0"
78 );
79 }
80
81 static get FALCON() {
82 return 'f';
83 }
84
85 static get PIECES() {
86 return [V.LANCER, V.ARCHER, V.KHESHIG, V.FALCON, V.KING];
87 }
88
89 getPotentialMovesFrom(sq) {
90 switch (this.getPiece(sq[0], sq[1])) {
91 case V.PAWN:
92 return super.getPotentialPawnMoves(sq);
93 case V.LANCER:
94 return super.getPotentialLancerMoves(sq);
95 case V.ARCHER:
96 return super.getPotentialArcherMoves(sq);
97 case V.KHESHIG:
98 return super.getPotentialKheshigMoves(sq);
99 case V.FALCON:
100 return this.getPotentialFalconMoves(sq);
101 case V.KING:
102 return super.getPotentialKingMoves(sq)
103 }
104 return []; //never reached
105 }
106
107 getPotentialFalconMoves(sq) {
108 const onlyMoves = this.getSlideNJumpMoves(
109 sq,
110 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
111 null,
112 { onlyMove: true }
113 );
114 const onlyTakes = this.getSlideNJumpMoves(
115 sq,
116 V.steps[V.KNIGHT],
117 "oneStep",
118 { onlyTake: true }
119 );
120 return onlyMoves.concat(onlyTakes);
121 }
122
123 isAttacked(sq, color) {
124 return (
125 super.isAttackedByPawn(sq, color) ||
126 super.isAttackedByLancer(sq, color) ||
127 super.isAttackedByKheshig(sq, color) ||
128 super.isAttackedByArcher(sq, color) ||
129 this.isAttackedByFalcon(sq, color) ||
130 super.isAttackedByKing(sq, color)
131 );
132 }
133
134 isAttackedByFalcon(sq, color) {
135 return this.isAttackedBySlideNJump(
136 sq, color, V.FALCON, V.steps[V.KNIGHT], "oneStep");
137 }
138
139 static get VALUES() {
140 return {
141 p: 1,
142 f: 7,
143 a: 4,
144 h: 7,
145 l: 4,
146 k: 1000
147 };
148 }
149
150 };