1 import { ChessRules
} from "@/base_rules";
3 export class MonochromeRules
extends ChessRules
{
4 static get HasEnpassant() {
5 // Pawns would be on the same side
10 return [ [[4, 0], [4, 8]] ];
17 static IsGoodPosition(position
) {
18 if (position
.length
== 0) return false;
19 const rows
= position
.split("/");
20 if (rows
.length
!= V
.size
.x
) return false;
21 for (let row
of rows
) {
23 for (let i
= 0; i
< row
.length
; i
++) {
24 if (V
.PIECES
.includes(row
[i
])) sumElts
++;
26 const num
= parseInt(row
[i
]);
27 if (isNaN(num
)) return false;
31 if (sumElts
!= V
.size
.y
) return false;
36 canIplay(side
, [x
, y
]) {
37 const xBounds
= side
== 'w' ? [4,7] : [0,3];
38 return this.turn
== side
&& x
>= xBounds
[0] && x
<= xBounds
[1];
41 canTake([x1
, y1
], [x2
, y2
]) {
42 // Capture in other half-board
43 return ((x1
<= 3 && x2
>= 4) || (x1
>= 4 && x2
<= 3));
46 // Trim all non-capturing moves
47 static KeepCaptures(moves
) {
48 return moves
.filter(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1);
51 getAllPotentialMoves() {
52 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
53 let potentialMoves
= [];
54 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
55 for (let j
= 0; j
< V
.size
.y
; j
++) {
56 if (this.board
[i
][j
] != V
.EMPTY
) {
57 Array
.prototype.push
.apply(
59 this.getPotentialMovesFrom([i
, j
])
64 if (potentialMoves
.some(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1))
65 return V
.KeepCaptures(potentialMoves
);
66 return potentialMoves
;
70 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
71 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
72 for (let j
= 0; j
< V
.size
.y
; j
++) {
74 this.board
[i
][j
] != V
.EMPTY
&&
75 this.getPotentialMovesFrom([i
, j
]).length
> 0
84 // Stop at the first capture found (if any)
86 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
87 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
88 for (let j
= 0; j
< V
.size
.y
; j
++) {
90 this.board
[i
][j
] != V
.EMPTY
&&
91 this.getPotentialMovesFrom([i
, j
]).some(m
=>
92 // Warning: discard castle moves
93 m
.vanish
.length
== 2 && m
.appear
.length
== 1)
102 getPossibleMovesFrom(sq
) {
103 let moves
= this.getPotentialMovesFrom(sq
);
104 const captureMoves
= V
.KeepCaptures(moves
);
105 if (captureMoves
.length
> 0) return captureMoves
;
106 if (this.atLeastOneCapture()) return [];
123 // Is there anything in my half board?
124 const color
= V
.GetOppCol(this.turn
);
125 const xBounds
= color
== 'w' ? [4,7] : [0,3];
126 let nothingHere
= true;
127 outerLoop: for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
128 for (let j
= 0; j
< V
.size
.y
; j
++) {
129 if (this.board
[i
][j
] != V
.EMPTY
) {
135 if (nothingHere
) return color
== 'w' ? "0-1" : "1-0";
136 if (this.atLeastOneMove()) return '*';
140 static GenRandInitFen(randomness
) {
141 // Remove the en-passant part of the FEN
142 const fen
= ChessRules
.GenRandInitFen(randomness
).slice(0, -2);
143 const firstSpace
= fen
.indexOf(' ');
145 fen
.substr(0, firstSpace
).replace(/[A-Z]/g, (c
) => c
.toLowerCase()) +
146 fen
.substr(firstSpace
)
150 static get SEARCH_DEPTH() {
156 for (let i
= 0; i
< 8; i
++) {
157 for (let j
= 0; j
< V
.size
.y
; j
++) {
158 if (this.board
[i
][j
] != V
.EMPTY
) {
159 const sign
= (i
<= 3 ? -1 : 1);
160 // I don't think taking pieces' values into account would help
161 evaluation
+= sign
; //* V.VALUES[this.getPiece(i, j)];