UNPKG

xchess

Version:

Chess Engine

101 lines (77 loc) 1.41 kB
export {File, files} import {INVALID_FILE} from './errors.js' import {ConstNumberObject} from './ansi-style.js' import {inspect} from './inspect.js' const size = 8; const files = []; const map = new Map(); function nameAt(x){ return String.fromCharCode(x + 97); } function iccfAt(x){ return (x + 1) * 10; } function file(value){ const file = map.get(value); if(file) return file; throw INVALID_FILE(value); } class File { static from(value){ return file(value); } static is(value){ return map.has(value); } static all(){ return files; } static reverse(){ return ReverseFiles; } #x; #name; #iccf; #squares = []; constructor(x){ this.#x = x; this.#iccf = iccfAt(x); this.#name = nameAt(x); } get x(){ return this.#x; } get name(){ return this.#name; } get iccf(){ return this.#iccf; } get squares(){ return this.#squares; } toString(){ return this.name; } valueOf(){ return this.x; } [inspect.custom](depth, opts){ return ConstNumberObject(`[File ${this.name.toUpperCase()}]`); } } // generate all while(files.length < size){ const x = files.length; const file = new File(x); files.push(file); map.set(file, file); map.set(file.x, file); map.set(file.name, file); map.set(file.name.toUpperCase(), file); } const ReverseFiles = [... files]; ReverseFiles.reverse(); Object.freeze(files); Object.freeze(ReverseFiles);