xchess
Version:
Chess Engine
70 lines (64 loc) • 1.39 kB
JavaScript
import {Color} from './color.js'
import {King, Queen, Rook, Bishop, Knight, Pawn} from './piece.js'
export function Weight(pieceList){
let weight = 0;
for(const piece of pieceList)
weight += piece.weight;
return weight;
}
export function Advantage(pieceList, colorArg = Color.white){
const color = Color.from(colorArg);
let advantage = 0;
for(const piece of pieceList){
if(color.eq(piece.color))
advantage += piece.weight;
else
advantage -= piece.weight;
}
return advantage;
}
function CreateStat(){
return {
count: 0,
weight: 0,
kings: 0,
queens: 0,
rooks: 0,
bishops: 0,
knights: 0,
pawns: 0,
};
}
function AddToStat(stat, piece){
stat.count ++;
stat.weight += piece.weight;
if(King.is(piece))
stat.kings ++;
if(Queen.is(piece))
stat.queens ++;
if(Rook.is(piece))
stat.rooks ++;
if(Bishop.is(piece))
stat.bishops ++;
if(Knight.is(piece))
stat.knights ++;
if(Pawn.is(piece))
stat.pawns ++;
}
export function PiecesStat(pieceList){
const white = CreateStat();
const black = CreateStat();
const advantage = 0;
const stat = {... CreateStat(), advantage, white, black};
for(const piece of pieceList){
AddToStat(stat, piece);
if(Color.isWhite(piece.color)){
AddToStat(white, piece);
stat.advantage += piece.weight;
} else {
AddToStat(black, piece);
stat.advantage -= piece.weight;
}
}
return stat;
}