Fix bug when editing a problem
[vchess.git] / client / src / variants / Twokings.js
1 import { ChessRules } from "@/base_rules";
2 import { CoregalRules } from "@/variants/Coregal";
3
4 export class TwokingsRules extends CoregalRules {
5 static get PawnSpecs() {
6 return Object.assign(
7 {},
8 ChessRules.PawnSpecs,
9 { promotions: ChessRules.PawnSpecs.promotions.concat([V.KING]) }
10 );
11 }
12
13 static IsGoodPosition(position) {
14 if (position.length == 0) return false;
15 const rows = position.split("/");
16 if (rows.length != V.size.x) return false;
17 let kings = { "w": 0, "b": 0 };
18 for (let row of rows) {
19 let sumElts = 0;
20 for (let i = 0; i < row.length; i++) {
21 if (['K','k'].includes(row[i])) kings[row[i]]++;
22 if (V.PIECES.includes(row[i].toLowerCase())) sumElts++;
23 else {
24 const num = parseInt(row[i]);
25 if (isNaN(num)) return false;
26 sumElts += num;
27 }
28 }
29 if (sumElts != V.size.y) return false;
30 }
31 // Two kings (at least) per side should be present:
32 if (Object.values(kings).some(v => v < 2)) return false;
33 return true;
34 }
35
36 // Not scanning king positions. In this variant, scan the board everytime.
37 scanKings(fen) {}
38
39 getCheckSquares(color) {
40 let squares = [];
41 const oppCol = V.GetOppCol(color);
42 for (let i=0; i<V.size.x; i++) {
43 for (let j=0; j<V.size.y; j++) {
44 if (
45 this.getColor(i, j) == color &&
46 this.getPiece(i, j) == V.KING &&
47 this.isAttacked([i, j], oppCol)
48 ) {
49 squares.push([i, j]);
50 }
51 }
52 }
53 return squares;
54 }
55
56 static GenRandInitFen(randomness) {
57 const fen = CoregalRules.GenRandInitFen(randomness);
58 return fen.replace("q", "k").replace("Q", "K");
59 }
60
61 underCheck(color) {
62 const oppCol = V.GetOppCol(color);
63 for (let i=0; i<V.size.x; i++) {
64 for (let j=0; j<V.size.y; j++) {
65 if (
66 this.getColor(i, j) == color &&
67 this.getPiece(i, j) == V.KING &&
68 this.isAttacked([i, j], oppCol)
69 ) {
70 return true;
71 }
72 }
73 }
74 return false;
75 }
76
77 postPlay(move) {
78 const piece = move.vanish[0].p;
79 super.updateCastleFlags(move, piece, "twoKings");
80 }
81
82 postUndo() {}
83 };