FEN parser

This commit is contained in:
Satria 2026-07-25 16:09:26 +07:00
commit 3d7802fa6e

View file

@ -5,6 +5,7 @@ import { Piece } from "./types";
export const startFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0";
const CHAR_TO_PIECE: Record<string, number> = {
"-": Piece.EMPTY,
"K": Piece.WHITE | Piece.KING,
"Q": Piece.WHITE | Piece.QUEEN,
"R": Piece.WHITE | Piece.ROOK,
@ -31,13 +32,22 @@ export class Board {
halfMove: 0,
fullMove: 0,
};
constructor() {
this.loadFEN();
}
loadFEN(fen = startFEN) {
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++;
});
});
}
}