UNPKG

xchess

Version:

Chess Engine

182 lines (153 loc) 3.15 kB
export {geo as default} import {Square} from './square.js' import {Move, Capture} from './move.js' let board, piece, from, to, moves; function crossInit(PIECE, TO, BOARD){ piece = PIECE; board = BOARD; from = board.find(piece); to = Square.from(TO); } function traceInit(PIECE, BOARD){ piece = PIECE; board = BOARD; from = board.find(piece); moves = []; } const geo = { crossT(piece, to, board){ crossInit(piece, to, board); if(from) return crossT(); return false; }, crossX(piece, to, board){ crossInit(piece, to, board); if(from) return crossX(); return false; }, crossAny(piece, to, board){ crossInit(piece, to, board); if(from) return crossAny(); return false; }, isNear(piece, to, board){ const from = board.find(piece); if(from) return from.isNear(to); return false; }, isL(piece, to, board){ const from = board.find(piece); if(from) return from.isL(to); return false; }, traceT(piece, board){ traceInit(piece, board); if(from) traceT(); return moves; }, traceX(piece, board){ traceInit(piece, board); if(from) traceX(); return moves; }, traceAll(piece, board){ traceInit(piece, board); if(from) traceAll(); return moves; }, near(piece, board){ traceInit(piece, board); if(from) search(from.near); return moves; }, L(piece, board){ traceInit(piece, board); if(from) search(from.L); return moves; }, }; function crossAny(){ return crossT() || crossX(); } function crossX(){ if(from.a === to.a) return crossZ(); if(from.b === to.b) return crossN(); return false; } function crossT(){ if(from.x === to.x) return crossV(); if(from.y === to.y) return crossH(); return false; } function crossV(){ if(from.y < to.y) return gap(from.walkBottom()); if(from.y > to.y) return gap(from.walkTop()); return false; } function crossH(){ if(from.x < to.x) return gap(from.walkRight()) if(from.x > to.x) return gap(from.walkLeft()) return false; } function crossZ(){ if(from.b < to.b) return gap(from.walkTopRight()) if(from.b > to.b) return gap(from.walkBottomLeft()) return false; } function crossN(){ if(from.a < to.a) return gap(from.walkBottomRight()) if(from.a > to.a) return gap(from.walkTopLeft()) return false; } function gap(squares){ for(const square of squares){ if(square == to) return true; if(board.has(square)) return false; } return false; } function traceT(){ for(const squareSeq of from.walkT()) trace(squareSeq); } function traceX(){ for(const squareSeq of from.walkX()) trace(squareSeq); } function traceAll(){ for(const squareSeq of from.walkAll()) trace(squareSeq); } function trace(squareSeq){ for(const to of squareSeq){ const capture = board.get(to); if(capture){ if(piece.opposite(capture)) moves.push(new Capture(piece, from, to, capture)); break; } moves.push(new Move(piece, from, to)); } } function search(squares){ for(const to of squares){ const capture = board.get(to); if(capture){ if(piece.opposite(capture)) moves.push(new Capture(piece, from, to, capture)); continue; } moves.push(new Move(piece, from, to)); } }