xchess
Version:
Chess Engine
396 lines (293 loc) • 4.29 kB
JavaScript
export {GameState}
import {EmptyMoveMap} from './move-map.js'
import {stateToFEN} from './fen.js'
import {StateHandler} from './state-handler.js'
import {
GAME_READONLY,
PROMOTION_NOT_AVAILABLE,
DRAW_NOT_AVAILABLE,
} from './errors.js'
class GameState {
#context;
#prev;
#next = null;
#time = new Date();
#handler = new StateHandler(this);
constructor(context, prev = null){
this.#context = context;
this.#prev = prev;
}
get context(){
return this.#context;
}
get game(){
return this.#context.game;
}
// State
get state(){
return this.#context.state;
}
set state(state){
this.#context.state = state;
}
get first(){
return this.#context.first;
}
set first(state){
this.#context.first = state;
}
get target(){
return this;
}
get prev(){
return this.#prev;
}
get next(){
return this.#next;
}
set next(state){
this.#next = state;
}
get time(){
return this.#time;
}
get handler(){
return this.#handler;
}
// Context
get board(){
return this.#context.board;
}
get captureList(){
return this.#context.captureList;
}
get stateCounter(){
return this.#context.stateCounter;
}
// Config
get color(){
return null;
}
get gameColor(){
return null;
}
get count(){
return 0;
}
get fullmoveNumber(){
return ((this.count) >> 1) + 1;
}
get halfmoveClock(){
return 0;
}
get doubleMovePawn(){
return null;
}
get enPassantTarget(){
return this.board.find(this.doubleMovePawn);
}
get castling(){
return null;
}
get prevCastling(){
return null;
}
get wk(){
return true;
}
get wq(){
return true;
}
get bk(){
return true;
}
get bq(){
return true;
}
get lastMove(){
return null;
}
get moves(){
return EmptyMoveMap;
}
get isCheck(){
return false;
}
get checkKing(){
return null;
}
get checkKingTarget(){
return null;
}
get hash(){
return null;
}
get repetition(){
return 1;
}
get ownRepetition(){
return 1;
}
get newRepetition(){
return 0;
}
get drawOffer(){
return null;
}
setDrawOffer(drawOffer){
throw DRAW_NOT_AVAILABLE();
}
// Setup
access(){
throw GAME_READONLY();
}
setAll(config){
throw GAME_READONLY();
}
setBoard(board){
throw GAME_READONLY();
}
setCaptureList(pieceList){
throw GAME_READONLY();
}
setCount(count){
this.context.goto(count);
}
setFullmoveNumber(value){
throw GAME_READONLY();
}
setHalfmoveClock(value){
throw GAME_READONLY();
}
setColor(color){
throw GAME_READONLY();
}
setDoubleMovePawn(pawn){
throw GAME_READONLY();
}
setEnPassantTarget(square){
throw GAME_READONLY();
}
setWK(value){
throw GAME_READONLY();
}
setWQ(value){
throw GAME_READONLY();
}
setBK(value){
throw GAME_READONLY();
}
setBQ(value){
throw GAME_READONLY();
}
// Stat
get status(){
return 'unknown';
}
get isProxy(){
return false;
}
get isSetup(){
return false;
}
get isMovement(){
return false;
}
get isPromotion(){
return false;
}
get isDrawOffer(){
return false;
}
get isEnd(){
return false;
}
get isWin(){
return false;
}
get isDraw(){
return false;
}
get isImmediate(){
return false;
}
get isCheckmate(){
return false;
}
get isStalemate(){
return false;
}
get winner(){
return null;
}
get loser(){
return null;
}
get subject(){
return null;
}
get result(){
return '*';
}
// I/O
get fen(){
return stateToFEN(this);
}
setFEN(fen){
throw GAME_READONLY();
}
// Dispatch Events
dispatch(event){
this.#context.dispatch(event);
}
emit(type){
this.#context.emit(type);
}
// Log Event
undo(){
// do nothing
}
redo(){
// do nothing
}
trigger(){
// do nothing
}
push(state){
this.state = state;
this.next = state;
this.state.trigger();
}
// Rules
isDeadPosition(){
return this.context.isDeadPosition();
}
isCheckmateChance(color){
return this.context.isCheckmateChance(color);
}
// Game Events
play(){
return false;
}
forfeit(color){
return false;
}
resign(color){
return false;
}
draw(color){
return false;
}
toDraw(color){
return false;
}
flagFall(){
return false;
}
move(move){
return false;
}
promote(piece){
throw PROMOTION_NOT_AVAILABLE();
}
}