This commit is contained in:
Maciej Caderek
2022-01-30 15:15:47 +01:00
parent 3023329586
commit 6ca3836586
12 changed files with 250 additions and 45 deletions

View File

@@ -1,8 +1,8 @@
import { PieceType } from "./../types";
import { PieceType } from "../types";
import { Chess, ChessInstance, Move } from "chess.js";
import { cleanPGN } from "./PGNHelpers";
export type MoveWithPly = Move & { ply: number; end: number };
export type MoveWithDetails = Move & { ply: number; end: number; fen: string };
const MATERIAL_VALUE: Map<PieceType, number> = new Map([
["q", 9],
@@ -15,7 +15,7 @@ const MATERIAL_VALUE: Map<PieceType, number> = new Map([
class Game {
private game: ChessInstance;
private replay: ChessInstance;
private moves: MoveWithPly[];
private moves: MoveWithDetails[];
private currentPly: number = 0;
constructor() {
@@ -24,28 +24,40 @@ class Game {
this.moves = [];
}
getMoves() {
return this.moves.map((move) => move.san);
}
getFEN() {
return this.replay.fen();
}
loadPGN(pgn: string) {
this.game.load_pgn(cleanPGN(pgn));
this.game.delete_comments();
console.log(this.game.pgn());
const moves = this.game.history({ verbose: true });
this.moves = moves.map((item, i) => ({
...item,
ply: i + 1,
end: moves.length - 1 - i,
}));
this.currentPly = 0;
const tempGame = new Chess();
const fen = this.game.header().FEN;
if (fen) {
tempGame.load(fen);
this.replay.load(fen);
}
this.moves = moves.map((item, i) => {
tempGame.move(item);
return {
...item,
ply: i + 1,
end: moves.length - 1 - i,
fen: tempGame.fen(),
};
});
this.currentPly = 0;
return this;
}
@@ -111,7 +123,7 @@ class Game {
w: { p: 0, n: 0, b: 0, r: 0, q: 0 },
b: { p: 0, n: 0, b: 0, r: 0, q: 0 },
};
const material = {
const count = {
w: { p: 0, n: 0, b: 0, r: 0, q: 0 },
b: { p: 0, n: 0, b: 0, r: 0, q: 0 },
};
@@ -119,7 +131,7 @@ class Game {
for (const piece of pieces) {
if (piece !== null && piece.type !== "k") {
sum[piece.color] += MATERIAL_VALUE.get(piece.type) ?? 0;
material[piece.color][piece.type] += 1;
count[piece.color][piece.type] += 1;
const oppositeColor = piece.color === "b" ? "w" : "b";
@@ -131,7 +143,7 @@ class Game {
}
}
return { sum, imbalance, material, diff: sum.w - sum.b };
return { sum, imbalance, count, diff: sum.w - sum.b };
}
getBoardData() {

150
src/game/Game_x.ts Normal file
View File

@@ -0,0 +1,150 @@
// import { PieceType, BoardData } from "../types";
// import { Chess, ChessInstance, Move } from "chess.js";
// import { cleanPGN } from "./PGNHelpers";
// export type MoveWithDetails = Move & { ply: number; end: number; fen: string };
// const MATERIAL_VALUE: Map<PieceType, number> = new Map([
// ["q", 9],
// ["r", 5],
// ["b", 3],
// ["n", 3],
// ["p", 1],
// ]);
// class Game {
// private moves: MoveWithDetails[];
// private currentPly: number = 0;
// constructor() {
// this.moves = [];
// }
// getMoves() {
// return this.moves.map((move) => move.san);
// }
// loadPGN(pgn: string) {
// const game = new Chess();
// const replay = new Chess();
// game.load_pgn(cleanPGN(pgn));
// game.delete_comments();
// const moves = game.history({ verbose: true });
// const fen = game.header().FEN;
// if (fen) {
// replay.load(fen);
// }
// this.moves = moves.map((item, i) => {
// replay.move(item);
// const currentFEN = replay.fen();
// return {
// ...item,
// ply: i + 1,
// end: moves.length - 1 - i,
// fen: currentFEN,
// material: this.materialInfo(replay.board()),
// };
// });
// this.currentPly = 0;
// return this;
// }
// next() {
// // const move = this.moves[this.currentPly];
// // if (!move) {
// // return null;
// // }
// // this.currentPly++;
// // this.replay.move(move);
// // return move;
// }
// prev() {
// // const undo = Boolean(this.replay.undo());
// // if (undo) {
// // this.currentPly--;
// // } else {
// // return null;
// // }
// // const move = this.moves[this.currentPly - 1];
// // return move;
// }
// last() {
// while (this.next()) {}
// return this.moves[this.moves.length - 1];
// }
// first() {
// while (this.prev()) {}
// return this.moves[0];
// }
// goto(ply: number) {
// if (ply === this.currentPly || ply < 0 || ply > this.moves.length - 1) {
// return null;
// }
// while (this.currentPly !== ply) {
// ply > this.currentPly ? this.next() : this.prev();
// }
// return this.moves[ply - 1];
// }
// materialInfo(boardData: BoardData) {
// const pieces = boardData.flat().filter(Boolean);
// const sum = { w: 0, b: 0 };
// const imbalance = {
// w: { p: 0, n: 0, b: 0, r: 0, q: 0 },
// b: { p: 0, n: 0, b: 0, r: 0, q: 0 },
// };
// const count = {
// w: { p: 0, n: 0, b: 0, r: 0, q: 0 },
// b: { p: 0, n: 0, b: 0, r: 0, q: 0 },
// };
// for (const piece of pieces) {
// if (piece !== null && piece.type !== "k") {
// sum[piece.color] += MATERIAL_VALUE.get(piece.type) ?? 0;
// count[piece.color][piece.type] += 1;
// const oppositeColor = piece.color === "b" ? "w" : "b";
// if (imbalance[oppositeColor][piece.type] === 0) {
// imbalance[piece.color][piece.type] += 1;
// } else {
// imbalance[oppositeColor][piece.type] -= 1;
// }
// }
// }
// return { sum, imbalance, count, diff: sum.w - sum.b };
// }
// getBoardData() {
// // return this.replay.board();
// return new Chess().board();
// }
// getHeader() {
// return this.game.header();
// }
// pgn() {
// return this.game.pgn();
// }
// }
// export type Material = ReturnType<Game["materialInfo"]>;
// export default Game;