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;
41 return (b
[1] == V
.KNIGHT
? "Enpassant/" : "") + b
;
44 canIplay(side
, [x
, y
]) {
45 const xBounds
= side
== 'w' ? [4,7] : [0,3];
46 return this.turn
== side
&& x
>= xBounds
[0] && x
<= xBounds
[1];
49 canTake([x1
, y1
], [x2
, y2
]) {
50 // Capture in other half-board
51 return ((x1
<= 3 && x2
>= 4) || (x1
>= 4 && x2
<= 3));
54 // Trim all non-capturing moves
55 static KeepCaptures(moves
) {
56 return moves
.filter(m
=> m
.vanish
.length
== 2);
59 getPotentialKnightMoves(sq
) {
60 // Knight becomes knightrider:
61 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
]);
64 getPotentialKingMoves(sq
) {
67 this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]))
71 getAllPotentialMoves() {
72 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
73 let potentialMoves
= [];
74 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
75 for (let j
= 0; j
< V
.size
.y
; j
++) {
76 if (this.board
[i
][j
] != V
.EMPTY
) {
77 Array
.prototype.push
.apply(
79 this.getPotentialMovesFrom([i
, j
])
84 if (potentialMoves
.some(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1))
85 return V
.KeepCaptures(potentialMoves
);
86 return potentialMoves
;
90 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
91 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
92 for (let j
= 0; j
< V
.size
.y
; j
++) {
94 this.board
[i
][j
] != V
.EMPTY
&&
95 this.getPotentialMovesFrom([i
, j
]).length
> 0
104 // Stop at the first capture found (if any)
105 atLeastOneCapture() {
106 const xBounds
= this.turn
== 'w' ? [4,7] : [0,3];
107 for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
108 for (let j
= 0; j
< V
.size
.y
; j
++) {
110 this.board
[i
][j
] != V
.EMPTY
&&
111 this.getPotentialMovesFrom([i
, j
]).some(m
=> m
.vanish
.length
== 2)
120 getPossibleMovesFrom(sq
) {
121 let moves
= this.getPotentialMovesFrom(sq
);
122 const captureMoves
= V
.KeepCaptures(moves
);
123 if (captureMoves
.length
> 0) return captureMoves
;
124 if (this.atLeastOneCapture()) return [];
141 // Is there anything in opponent's half board?
142 const color
= V
.GetOppCol(this.turn
);
143 const xBounds
= color
== 'w' ? [4,7] : [0,3];
144 let nothingHere
= true;
145 outerLoop: for (let i
= xBounds
[0]; i
<= xBounds
[1]; i
++) {
146 for (let j
= 0; j
< V
.size
.y
; j
++) {
147 if (this.board
[i
][j
] != V
.EMPTY
) {
153 if (nothingHere
) return color
== 'w' ? "0-1" : "1-0";
154 if (this.atLeastOneMove()) return '*';
158 static GenRandInitFen(randomness
) {
159 // Remove the en-passant + castle part of the FEN
160 let fen
= ChessRules
.GenRandInitFen(randomness
).slice(0, -6);
161 // Replace kings with queens
162 fen
= fen
.replace("k", "q").replace("K", "Q");
164 fen
= fen
.replace("pppppppp/8","8/pppppppp")
165 .replace("8/PPPPPPPP","PPPPPPPP/8");
166 const firstSpace
= fen
.indexOf(' ');
169 fen
.substr(0, firstSpace
).replace(/[A-Z]/g, (c
) => c
.toLowerCase()) +
170 fen
.substr(firstSpace
);
174 static get SEARCH_DEPTH() {
180 for (let i
= 0; i
< 8; i
++) {
181 for (let j
= 0; j
< V
.size
.y
; j
++) {
182 if (this.board
[i
][j
] != V
.EMPTY
) {
183 const sign
= (i
<= 3 ? -1 : 1);
184 // I don't think taking pieces' values into account would help
185 evaluation
+= sign
; //* V.VALUES[this.getPiece(i, j)];
193 // Translate initial square (because pieces may fly unusually!)
194 const initialSquare
= V
.CoordsToSquare(move.start
);
196 // Translate final square
197 const finalSquare
= V
.CoordsToSquare(move.end
);
200 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
201 if (piece
== V
.PAWN
) {
202 // pawn move (TODO: enPassant indication)
203 if (move.vanish
.length
== 2) {
205 notation
= initialSquare
+ "x" + finalSquare
;
207 else notation
= finalSquare
;
208 if (piece
!= move.appear
[0].p
)
210 notation
+= "=" + move.appear
[0].p
.toUpperCase();
214 notation
= piece
.toUpperCase();
215 if (move.vanish
.length
> 1) notation
+= initialSquare
+ "x";
216 notation
+= finalSquare
;