xchess
Version:
Chess Engine
65 lines (51 loc) • 1.07 kB
JavaScript
export {PieceSet}
import {Piece} from './piece.js'
import {Weight, Advantage, PiecesStat} from './piece-stat.js'
import {IS_EMPTY, IS_STRING, IS_ITERABLE} from './types.js'
import {INVALID_PIECE_LIST} from './errors.js'
class PieceSet extends Set {
add(piece){
super.add(Piece.from(piece));
}
#setAsList(pieces){
const pieceList = [];
for(const piece of pieces)
pieceList.push(Piece.from(piece));
this.clear();
for(const piece of pieceList)
this.add(piece);
}
setAll(pieceList){
if(IS_EMPTY(pieceList))
this.clear();
else if(IS_STRING(pieceList))
this.fen = pieceList;
else if(IS_ITERABLE(pieceList))
this.#setAsList(pieceList);
else
throw INVALID_PIECE_LIST(pieceList);
}
// Stat
get weight(){
return Weight(this);
}
advantage(color){
return Advantage(this, color);
}
stat(){
return PiecesStat(this);
}
// I/O
get fen(){
const fen = [];
for(const piece of this)
fen.push(piece.fen);
return fen.join('');
}
set fen(fen){
this.#setAsList(String(fen));
}
toString(){
return this.fen;
}
}