xchess
Version:
Chess Engine
199 lines (144 loc) • 2.26 kB
JavaScript
export {King, Queen, Rook, Bishop, Knight, Pawn}
import {Piece} from './piece.js'
import * as pawn from './pawn.js'
class King extends Piece {
static get code(){
return 5;
}
static get char(){
return 'K';
}
static get signs(){
return '♔♚';
}
// rules
allMoves(context){
return [
... context.board.near(this),
... context.castling.moves(context),
];
}
test(target, board){
return board.isNear(this, target);
}
}
class Queen extends Piece {
static get code(){
return 4;
}
static get iccf(){
return '1';
}
static get char(){
return 'Q';
}
static get signs(){
return '♕♛';
}
static get weight(){
return 9;
}
// rules
allMoves(context){
return context.board.traceAll(this);
}
test(target, board){
return board.crossAny(this, target);
}
}
class Rook extends Piece {
static get code(){
return 3;
}
static get iccf(){
return '2';
}
static get char(){
return 'R';
}
static get signs(){
return '♖♜';
}
static get weight(){
return 5;
}
// rules
allMoves(context){
return context.board.traceT(this);
}
test(target, board){
return board.crossT(this, target);
}
}
class Bishop extends Piece {
static get code(){
return 2;
}
static get iccf(){
return '3';
}
static get char(){
return 'B';
}
static get signs(){
return '♗♝';
}
static get weight(){
return 3;
}
// rules
allMoves(context){
return context.board.traceX(this);
}
test(target, board){
return board.crossX(this, target);
}
}
class Knight extends Piece {
static get code(){
return 1;
}
static get iccf(){
return '4';
}
static get char(){
return 'N';
}
static get signs(){
return '♘♞';
}
static get weight(){
return 3;
}
// rules
allMoves(context){
return context.board.L(this);
}
test(target, board){
return board.isL(this, target);
}
}
class Pawn extends Piece {
static get code(){
return 0;
}
static get char(){
return 'P';
}
static get signs(){
return '♙♟';
}
get moveLetter(){
return '';
}
static get weight(){
return 1;
}
// rules
allMoves(context){
return [... pawn.allMoves(this, context)];
}
test(target, board){
return pawn.test(this, target, board);
}
}