5bd2ce00ecdef8e9e09de386ec552f3766de1446
1 import { ChessRules
} from "@/base_rules";
3 export class MonochromeRules
extends ChessRules
{
4 static get HasEnpassant() {
5 // Pawns would be on the same side
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 for (let row
of rows
) {
19 for (let i
= 0; i
< row
.length
; i
++) {
20 if (V
.PIECES
.includes(row
[i
])) sumElts
++;
22 const num
= parseInt(row
[i
]);
23 if (isNaN(num
)) return false;
27 if (sumElts
!= V
.size
.y
) return false;
32 canIplay(side
, [x
, y
]) {
33 const xBounds
= side
== 'w' ? [4,7] : [0,3];
34 return this.turn
== side
&& x
>= xBounds
[0] && x
<= xBounds
[1];
37 canTake([x1
, y1
], [x2
, y2
]) {
38 // Capture in other half-board
39 return ((x1
<= 3 && x2
>= 4) || (x1
>= 4 && x2
<= 3));
42 // Trim all non-capturing moves
43 static KeepCaptures(moves
) {
44 return moves
.filter(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1);
47 getAllPotentialMoves() {
48 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
49 let potentialMoves
= [];
50 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
51 for (let j
= 0; j
< V
.size
.y
; j
++) {
52 if (this.board
[i
][j
] != V
.EMPTY
) {
53 Array
.prototype.push
.apply(
55 this.getPotentialMovesFrom([i
, j
])
60 if (potentialMoves
.some(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1))
61 return V
.KeepCaptures(potentialMoves
);
62 return potentialMoves
;
66 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
67 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
68 for (let j
= 0; j
< V
.size
.y
; j
++) {
70 this.board
[i
][j
] != V
.EMPTY
&&
71 this.getPotentialMovesFrom([i
, j
]).length
> 0
80 // Stop at the first capture found (if any)
82 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
83 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
84 for (let j
= 0; j
< V
.size
.y
; j
++) {
86 this.board
[i
][j
] != V
.EMPTY
&&
87 this.getPotentialMovesFrom([i
, j
]).some(m
=>
88 // Warning: discard castle moves
89 m
.vanish
.length
== 2 && m
.appear
.length
== 1)
98 getPossibleMovesFrom(sq
) {
99 let moves
= this.getPotentialMovesFrom(sq
);
100 const captureMoves
= V
.KeepCaptures(moves
);
101 if (captureMoves
.length
> 0) return captureMoves
;
102 if (this.atLeastOneCapture()) return [];
119 // Is there anything in my half board?
120 const color
= V
.GetOppCol(this.turn
);
121 const xBounds
= color
== 'w' ? [4,7] : [0,3];
122 let nothingHere
= true;
123 outerLoop: for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
124 for (let j
= 0; j
< V
.size
.y
; j
++) {
125 if (this.board
[i
][j
] != V
.EMPTY
) {
131 if (nothingHere
) return color
== 'w' ? "0-1" : "1-0";
132 if (this.atLeastOneMove()) return '*';
136 static GenRandInitFen(randomness
) {
137 // Remove the en-passant part of the FEN
138 const fen
= ChessRules
.GenRandInitFen(randomness
).slice(0, -2);
139 const firstSpace
= fen
.indexOf(' ');
141 fen
.substr(0, firstSpace
).replace(/[A-Z]/g, (c
) => c
.toLowerCase()) +
142 fen
.substr(firstSpace
)
146 static get SEARCH_DEPTH() {
152 for (let i
= 0; i
< 8; i
++) {
153 for (let j
= 0; j
< V
.size
.y
; j
++) {
154 if (this.board
[i
][j
] != V
.EMPTY
) {
155 const sign
= (i
<= 3 ? -1 : 1);
156 // I don't think taking pieces' values into account would help
157 evaluation
+= sign
; //* V.VALUES[this.getPiece(i, j)];