UNPKG

xchess

Version:

Chess Engine

51 lines (45 loc) 1.08 kB
import {Color} from './color.js' import {King, Queen, Rook, Bishop, Knight, Pawn} from './standart-piece.js' export function Weight(pieceList){ let weight = 0; for(const piece of pieceList) weight += piece.weight; return weight; } export function Advantage(pieceList, color = Color.white){ let advantage = 0; for(const piece of pieceList) advantage += piece.advantage(color); return advantage; } function CreateStat(){ return { count: 0, weight: 0, advantage: 0, pieces: {}, }; } function AddToStat(stat, piece){ stat.count ++; stat.weight += piece.weight; stat.advantage += piece.advantage(); const name = piece.name.toLowerCase(); const count = stat.pieces[name] ?? 0; stat.pieces[name] = count + 1; } export function PiecesStat(pieceList){ const white = CreateStat(); const black = CreateStat(); const advantage = 0; const stat = {... CreateStat(), white, black}; for(const piece of pieceList){ AddToStat(stat, piece); if(Color.isWhite(piece.color)){ AddToStat(white, piece); } else { AddToStat(black, piece); } } return stat; }