1 import { ChessRules
} from "@/base_rules";
3 export class MonochromeRules
extends ChessRules
{
4 static get HasEnpassant() {
5 // Pawns would be on the same side
9 static get HasFlags() {
14 return [ [[4, 0], [4, 8]] ];
21 static IsGoodPosition(position
) {
22 if (position
.length
== 0) return false;
23 const rows
= position
.split("/");
24 if (rows
.length
!= V
.size
.x
) return false;
25 for (let row
of rows
) {
27 for (let i
= 0; i
< row
.length
; i
++) {
28 if (V
.PIECES
.includes(row
[i
])) sumElts
++;
30 const num
= parseInt(row
[i
], 10);
31 if (isNaN(num
)) return false;
35 if (sumElts
!= V
.size
.y
) return false;
40 canIplay(side
, [x
, y
]) {
41 const xBounds
= side
== 'w' ? [4,7] : [0,3];
42 return this.turn
== side
&& x
>= xBounds
[0] && x
<= xBounds
[1];
45 canTake([x1
, y1
], [x2
, y2
]) {
46 // Capture in other half-board
47 return ((x1
<= 3 && x2
>= 4) || (x1
>= 4 && x2
<= 3));
50 // Trim all non-capturing moves
51 static KeepCaptures(moves
) {
52 return moves
.filter(m
=> m
.vanish
.length
== 2);
55 getPotentialKnightMoves(sq
) {
56 // Knight becomes knightrider:
57 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
]);
60 getPotentialKingMoves(sq
) {
63 this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]))
67 getAllPotentialMoves() {
68 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
69 let potentialMoves
= [];
70 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
71 for (let j
= 0; j
< V
.size
.y
; j
++) {
72 if (this.board
[i
][j
] != V
.EMPTY
) {
73 Array
.prototype.push
.apply(
75 this.getPotentialMovesFrom([i
, j
])
80 if (potentialMoves
.some(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1))
81 return V
.KeepCaptures(potentialMoves
);
82 return potentialMoves
;
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
]).length
> 0
100 // Stop at the first capture found (if any)
101 atLeastOneCapture() {
102 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
103 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
104 for (let j
= 0; j
< V
.size
.y
; j
++) {
106 this.board
[i
][j
] != V
.EMPTY
&&
107 this.getPotentialMovesFrom([i
, j
]).some(m
=> m
.vanish
.length
== 2)
116 getPossibleMovesFrom(sq
) {
117 let moves
= this.getPotentialMovesFrom(sq
);
118 const captureMoves
= V
.KeepCaptures(moves
);
119 if (captureMoves
.length
> 0) return captureMoves
;
120 if (this.atLeastOneCapture()) return [];
137 // Is there anything in opponent's half board?
138 const color
= V
.GetOppCol(this.turn
);
139 const xBounds
= color
== 'w' ? [4,7] : [0,3];
140 let nothingHere
= true;
141 outerLoop: for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
142 for (let j
= 0; j
< V
.size
.y
; j
++) {
143 if (this.board
[i
][j
] != V
.EMPTY
) {
149 if (nothingHere
) return color
== 'w' ? "0-1" : "1-0";
150 if (this.atLeastOneMove()) return '*';
154 static GenRandInitFen(randomness
) {
155 // Remove the en-passant + castle part of the FEN
156 let fen
= ChessRules
.GenRandInitFen(randomness
).slice(0, -6);
158 fen
= fen
.replace("pppppppp/8","8/pppppppp")
159 .replace("8/PPPPPPPP","PPPPPPPP/8");
160 const firstSpace
= fen
.indexOf(' ');
163 fen
.substr(0, firstSpace
).replace(/[A-Z]/g, (c
) => c
.toLowerCase()) +
164 fen
.substr(firstSpace
);
168 static get SEARCH_DEPTH() {
174 for (let i
= 0; i
< 8; i
++) {
175 for (let j
= 0; j
< V
.size
.y
; j
++) {
176 if (this.board
[i
][j
] != V
.EMPTY
) {
177 const sign
= (i
<= 3 ? -1 : 1);
178 // I don't think taking pieces' values into account would help
179 evaluation
+= sign
; //* V.VALUES[this.getPiece(i, j)];
187 // Translate initial square (because pieces may fly unusually!)
188 const initialSquare
= V
.CoordsToSquare(move.start
);
190 // Translate final square
191 const finalSquare
= V
.CoordsToSquare(move.end
);
194 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
195 if (piece
== V
.PAWN
) {
196 // pawn move (TODO: enPassant indication)
197 if (move.vanish
.length
== 2) {
199 notation
= initialSquare
+ "x" + finalSquare
;
201 else notation
= finalSquare
;
202 if (piece
!= move.appear
[0].p
)
204 notation
+= "=" + move.appear
[0].p
.toUpperCase();
208 notation
= piece
.toUpperCase();
209 if (move.vanish
.length
> 1) notation
+= initialSquare
+ "x";
210 notation
+= finalSquare
;