UNPKG

xchess

Version:

Chess Engine

100 lines (76 loc) 1.34 kB
export {Rank, ranks} import {INVALID_RANK} from './errors.js' import {ConstNumberObject} from './ansi-style.js' import {inspect} from './inspect.js' const size = 8; const ranks = []; const map = new Map(); function nameAt(y){ return String(8 - y); } function iccfAt(y){ return 8 - y; } function rank(value){ const rank = map.get(value); if(rank) return rank; throw INVALID_RANK(value); } class Rank { static from(value){ return rank(value); } static is(value){ return map.has(value); } static all(){ return ranks; } static reverse(){ return ReverseRanks; } #y; #iccf; #name; #squares = []; constructor(y){ this.#y = y; this.#iccf = iccfAt(y); this.#name = nameAt(y); } get y(){ return this.#y; } get iccf(){ return this.#iccf; } get name(){ return this.#name; } get squares(){ return this.#squares; } toString(){ return this.name; } valueOf(){ return this.index; } [inspect.custom](depth, opts){ return ConstNumberObject(`[Rank ${this.name}]`); } } // generate all while(ranks.length < size){ const index = ranks.length; const rank = new Rank(index); ranks.push(rank); map.set(rank, rank); map.set(rank.y, rank); map.set(rank.name, rank); } const ReverseRanks = [... ranks]; ReverseRanks.reverse(); Object.freeze(ranks); Object.freeze(ReverseRanks);