FEN parser
This commit is contained in:
parent
fbadf1d436
commit
3d7802fa6e
1 changed files with 11 additions and 1 deletions
|
|
@ -5,6 +5,7 @@ import { Piece } from "./types";
|
||||||
|
|
||||||
export const startFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0";
|
export const startFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0";
|
||||||
const CHAR_TO_PIECE: Record<string, number> = {
|
const CHAR_TO_PIECE: Record<string, number> = {
|
||||||
|
"-": Piece.EMPTY,
|
||||||
"K": Piece.WHITE | Piece.KING,
|
"K": Piece.WHITE | Piece.KING,
|
||||||
"Q": Piece.WHITE | Piece.QUEEN,
|
"Q": Piece.WHITE | Piece.QUEEN,
|
||||||
"R": Piece.WHITE | Piece.ROOK,
|
"R": Piece.WHITE | Piece.ROOK,
|
||||||
|
|
@ -31,13 +32,22 @@ export class Board {
|
||||||
halfMove: 0,
|
halfMove: 0,
|
||||||
fullMove: 0,
|
fullMove: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.loadFEN();
|
this.loadFEN();
|
||||||
}
|
}
|
||||||
|
|
||||||
loadFEN(fen = startFEN) {
|
loadFEN(fen = startFEN) {
|
||||||
const [position, turn, castling, enPassant, halfMove, fullMove] = fen.split(" ");
|
const [position, turn, castling, enPassant, halfMove, fullMove] = fen.split(" ");
|
||||||
|
this.board.fill(Piece.EMPTY);
|
||||||
|
|
||||||
|
position.split("/").forEach((row, rank) => {
|
||||||
|
let file = 0;
|
||||||
|
row.split("").forEach((char) => {
|
||||||
|
if (!isNaN(parseInt(char))) return file += parseInt(char);
|
||||||
|
this.board[rank * 8 + file] = CHAR_TO_PIECE[char]
|
||||||
|
file++;
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue