UNPKG

xchess

Version:

Chess Engine

144 lines (108 loc) 1.94 kB
export {Piece} import {Color} from './color.js' import {INVALID_PIECE} from './errors.js' import {all} from './index.js' class Piece { static is(value){ return value instanceof this; } static validate(value){ if(this.is(value)) return value; throw INVALID_PIECE(value); } static from(value){ return all.from(value); } static fromColor(value, color){ return all.fromColor(value, color); } static rand(){ return RandPiece(); } static get code(){ return NaN; } static get iccf(){ return ''; } static get char(){ return '�'; } static get signs(){ return '��'; } static get weight(){ return 0; } #color; constructor(color){ this.#color = Color.from(color); } get color(){ return this.#color; } get code(){ return this.color.codeOf(this.constructor.code); } get iccf(){ return this.constructor.iccf; } get name(){ return this.constructor.name; } get sign(){ return this.constructor.signs[+ this.color]; } get char(){ return this.constructor.char; } get fen(){ return this.color.fenOf(this.char); } get moveLetter(){ return this.char; } get weight(){ return this.constructor.weight; } valueOf(){ return this.code; } toString(){ return this.sign; } toJSON(){ return this.fen; } clone(){ return new this.constructor(this.color); } // rules advantage(color = Color.white){ return this.color.eq(color) ? this.weight : - this.weight; } opposite(piece){ const target = Piece.from(piece); return this.color.opposite(target.color); } isCapturable(color, board){ return this.color.opposite(color); } isPassable(color, board){ return false; } moves(context){ const moves = []; const {board} = context; for(const move of this.allMoves(context)) if(!move.check(board)) moves.push(move); return moves; } allMoves(context){ return []; } test(target, board){ return false; } }