Commit | Line | Data |
---|---|---|
6b7b2cf7 BA |
1 | import { ChessRules } from "@/base_rules"; |
2 | import { ArrayFun } from "@/utils/array"; | |
32f6285e | 3 | import { shuffle } from "@/utils/alea"; |
6b7b2cf7 | 4 | |
32f6285e | 5 | export class SuicideRules extends ChessRules { |
6b7b2cf7 BA |
6 | static get HasFlags() { |
7 | return false; | |
8 | } | |
9 | ||
32f6285e BA |
10 | static get PawnSpecs() { |
11 | return Object.assign( | |
12 | {}, | |
13 | ChessRules.PawnSpecs, | |
14 | { promotions: ChessRules.PawnSpecs.promotions.concat([V.KING]) } | |
15 | ); | |
6b7b2cf7 BA |
16 | } |
17 | ||
6f2f9437 BA |
18 | static IsGoodPosition(position) { |
19 | if (position.length == 0) return false; | |
20 | const rows = position.split("/"); | |
21 | if (rows.length != V.size.x) return false; | |
22 | // Just check that at least one piece of each color is there: | |
23 | let pieces = { "w": 0, "b": 0 }; | |
24 | for (let row of rows) { | |
25 | let sumElts = 0; | |
26 | for (let i = 0; i < row.length; i++) { | |
27 | const lowerRi = row[i].toLowerCase(); | |
28 | if (V.PIECES.includes(lowerRi)) { | |
29 | pieces[row[i] == lowerRi ? "b" : "w"]++; | |
30 | sumElts++; | |
31 | } else { | |
32 | const num = parseInt(row[i]); | |
33 | if (isNaN(num)) return false; | |
34 | sumElts += num; | |
35 | } | |
36 | } | |
37 | if (sumElts != V.size.y) return false; | |
38 | } | |
39 | if (Object.values(pieces).some(v => v == 0)) return false; | |
40 | return true; | |
41 | } | |
42 | ||
43 | scanKings() {} | |
44 | ||
6b7b2cf7 BA |
45 | // Trim all non-capturing moves (not the most efficient, but easy) |
46 | static KeepCaptures(moves) { | |
47 | return moves.filter(m => m.vanish.length == 2); | |
48 | } | |
49 | ||
50 | // Stop at the first capture found (if any) | |
51 | atLeastOneCapture() { | |
52 | const color = this.turn; | |
53 | const oppCol = V.GetOppCol(color); | |
54 | for (let i = 0; i < V.size.x; i++) { | |
55 | for (let j = 0; j < V.size.y; j++) { | |
56 | if ( | |
57 | this.board[i][j] != V.EMPTY && | |
58 | this.getColor(i, j) != oppCol && | |
59 | this.getPotentialMovesFrom([i, j]).some(m => m.vanish.length == 2) | |
60 | ) { | |
61 | return true; | |
62 | } | |
63 | } | |
64 | } | |
65 | return false; | |
66 | } | |
67 | ||
68 | getPossibleMovesFrom(sq) { | |
69 | let moves = this.getPotentialMovesFrom(sq); | |
70 | const captureMoves = V.KeepCaptures(moves); | |
71 | if (captureMoves.length > 0) return captureMoves; | |
72 | if (this.atLeastOneCapture()) return []; | |
73 | return moves; | |
74 | } | |
75 | ||
76 | filterValid(moves) { | |
77 | return moves; | |
78 | } | |
79 | ||
80 | getAllValidMoves() { | |
81 | const moves = super.getAllValidMoves(); | |
82 | if (moves.some(m => m.vanish.length == 2)) return V.KeepCaptures(moves); | |
83 | return moves; | |
84 | } | |
85 | ||
86 | atLeastOneMove() { | |
87 | const color = this.turn; | |
88 | for (let i = 0; i < V.size.x; i++) { | |
89 | for (let j = 0; j < V.size.y; j++) { | |
90 | if ( | |
91 | this.getColor(i, j) == color && | |
92 | this.getPotentialMovesFrom([i, j]).length > 0 | |
93 | ) { | |
94 | return true; | |
95 | } | |
96 | } | |
97 | } | |
98 | return false; | |
99 | } | |
100 | ||
101 | getCheckSquares() { | |
102 | return []; | |
103 | } | |
104 | ||
105 | // No variables update because no royal king + no castling | |
3a2a7b5f BA |
106 | prePlay() {} |
107 | postPlay() {} | |
108 | preUndo() {} | |
109 | postUndo() {} | |
6b7b2cf7 BA |
110 | |
111 | getCurrentScore() { | |
112 | if (this.atLeastOneMove()) return "*"; | |
113 | // No valid move: the side who cannot move wins | |
114 | return this.turn == "w" ? "1-0" : "0-1"; | |
115 | } | |
116 | ||
117 | static get VALUES() { | |
118 | return { | |
119 | p: 1, | |
120 | r: 7, | |
121 | n: 3, | |
122 | b: 3, | |
123 | q: 5, | |
124 | k: 5 | |
125 | }; | |
126 | } | |
127 | ||
128 | static get SEARCH_DEPTH() { | |
129 | return 4; | |
130 | } | |
131 | ||
132 | evalPosition() { | |
133 | // Less material is better: | |
134 | return -super.evalPosition(); | |
135 | } | |
136 | ||
137 | static GenRandInitFen(randomness) { | |
138 | if (randomness == 0) | |
139 | return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 -"; | |
140 | ||
141 | let pieces = { w: new Array(8), b: new Array(8) }; | |
6b7b2cf7 BA |
142 | for (let c of ["w", "b"]) { |
143 | if (c == 'b' && randomness == 1) { | |
144 | pieces['b'] = pieces['w']; | |
145 | break; | |
146 | } | |
147 | ||
32f6285e BA |
148 | // Get random squares for every piece, totally freely |
149 | let positions = shuffle(ArrayFun.range(8)); | |
150 | const composition = ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q']; | |
151 | const rem2 = positions[0] % 2; | |
152 | if (rem2 == positions[1] % 2) { | |
153 | // Fix bishops (on different colors) | |
154 | for (let i=2; i<8; i++) { | |
155 | if (positions[i] % 2 != rem2) | |
156 | [positions[1], positions[i]] = [positions[i], positions[1]]; | |
157 | } | |
158 | } | |
159 | for (let i = 0; i < 8; i++) pieces[c][positions[i]] = composition[i]; | |
6b7b2cf7 BA |
160 | } |
161 | return ( | |
162 | pieces["b"].join("") + | |
163 | "/pppppppp/8/8/8/8/PPPPPPPP/" + | |
164 | pieces["w"].join("").toUpperCase() + | |
165 | // En-passant allowed, but no flags | |
166 | " w 0 -" | |
167 | ); | |
168 | } | |
169 | }; |