xchess
Version:
Chess Engine
352 lines (268 loc) • 5.05 kB
JavaScript
export {Game}
import {GameContext} from './game-context.js'
import {BoardView} from './board-view.js'
import {PieceSet} from './piece-set.js'
import {GameLog} from './log.js'
import {MetaMap} from './meta-map.js'
import {Emitter} from './emitter.js'
import {INVALID_GAME_VALUE} from './errors.js'
import {IS_EMPTY, IS_STRING, IS_OBJECT} from './types.js'
class Game extends Emitter {
static is(game){
try {
return Boolean(game.#context);
} catch {
return false;
}
}
static from(game){
if(IS_EMPTY(game))
return new this();
if(IS_STRING(game))
return new this(game);
if(Game.is(game))
return game;
if(IS_OBJECT(game))
return new this(game);
throw INVALID_GAME_VALUE(game);
}
static setup(config){
const game = this.from(config);
game.board.setup();
return game;
}
static play(config){
const game = this.from(config);
game.play();
return game;
}
static setupAndPlay(config){
const game = this.from(config);
game.board.setup();
game.play();
return game;
}
#context;
#board;
#log;
#meta = new MetaMap();
constructor(config){
super();
this.#context = new GameContext(this);
this.#board = new BoardView(this.#context);
this.#log = new GameLog(this.#context);
this.#context.setAll(config);
}
get fen(){
return this.#context.fen;
}
get board(){
return this.#board;
}
get captureList(){
return new PieceSet(this.#context.captureList);
}
get log(){
return this.#log;
}
get meta(){
return this.#meta;
}
get stateCounter(){
return new Map(this.#context.stateCounter);
}
get current(){
return this.#context.current;
}
get time(){
return this.#context.time;
}
get count(){
return this.#context.count;
}
get fullmoveNumber(){
return this.#context.fullmoveNumber;
}
get halfmoveClock(){
return this.#context.halfmoveClock;
}
get color(){
return this.#context.color;
}
get doubleMovePawn(){
return this.#context.doubleMovePawn;
}
get enPassantTarget(){
return this.#context.enPassantTarget;
}
get wk(){
return this.#context.wk;
}
get wq(){
return this.#context.wq;
}
get bk(){
return this.#context.bk;
}
get bq(){
return this.#context.bq;
}
get lastMove(){
return this.#context.lastMove;
}
get moves(){
return this.#context.moves;
}
get isCheck(){
return this.#context.isCheck;
}
get checkKing(){
return this.#context.checkKing;
}
get checkKingTarget(){
return this.#context.checkKingTarget;
}
get stateHash(){
return this.#context.hash;
}
get repetition(){
return this.#context.repetition;
}
get ownRepetition(){
return this.#context.ownRepetition;
}
get newRepetition(){
return this.#context.newRepetition;
}
// Setup
set fen(fen){
this.#context.fen = fen;
}
set board(board){
this.#context.board = board;
}
set captureList(pieceList){
this.#context.captureList = pieceList;
}
set count(count){
this.#context.count = count;
}
set fullmoveNumber(value){
this.#context.fullmoveNumber = value;
}
set halfmoveClock(value){
this.#context.halfmoveClock = value;
}
set color(color){
this.#context.color = color;
}
set doubleMovePawn(pawn){
this.#context.doubleMovePawn = pawn;
}
set enPassantTarget(square){
this.#context.enPassantTarget = square;
}
set wk(value){
this.#context.wk = value;
}
set wq(value){
this.#context.wq = value;
}
set bk(value){
this.#context.bk = value;
}
set bq(value){
this.#context.bq = value;
}
// Stat
get status(){
return this.#context.status;
}
get isSetup(){
return this.#context.isSetup;
}
get isMovement(){
return this.#context.isMovement;
}
get isPromotion(){
return this.#context.isPromotion;
}
get isDrawOffer(){
return this.#context.isDrawOffer;
}
get isEnd(){
return this.#context.isEnd;
}
get isWin(){
return this.#context.isWin;
}
get isDraw(){
return this.#context.isDraw;
}
get isImmediate(){
return this.#context.isImmediate;
}
get isCheckmate(){
return this.#context.isCheckmate;
}
get isStalemate(){
return this.#context.isStalemate;
}
get winner(){
return this.#context.winner;
}
get loser(){
return this.#context.loser;
}
get subject(){
return this.#context.subject;
}
get result(){
return this.#context.result;
}
// I/O
get fen(){
return this.#context.fen;
}
set fen(fen){
this.#context.fen = fen;
}
toString(){
return this.fen;
}
// Events
play(){
return this.#context.play();
}
forfeit(color){
return this.#context.forfeit(color);
}
resign(color){
return this.#context.resign(color);
}
draw(color){
return this.#context.draw(color);
}
flagFall(){
return this.#context.flagFall();
}
move(move){
return this.#context.move(move);
}
promote(piece){
return this.#context.promote(piece);
}
// Log Events
redo(){
return this.#context.redo();
}
undo(){
return this.#context.undo();
}
go(count){
return this.#context.go(count);
}
goto(offset){
return this.#context.goto(offset);
}
}