All files / moveRule kingMoveRule.js

100% Statements 35/35
100% Branches 10/10
100% Functions 4/4
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 3613x 13x 13x 13x 13x 13x 13x 13x 13x 13x 245x 75x 75x 75x 75x 600x 600x 600x 600x 245x 600x 600x 600x 600x 600x 600x 600x 600x 384x 384x 248x 248x 248x 600x 352x  
import { createPieceMoveRule } from "./pieceMoveRule.js";
import { DirectionEnum } from "./directionEnum.js"
 
function getKingMoveRule() {
    let moveRule = createPieceMoveRule();
 
    function getPossibleMovements () {
        let possibleMovements = [];
        for(let prop in DirectionEnum) {
            let movement = getMovement(DirectionEnum[prop]);
            if(movement !== undefined) {
                possibleMovements.push(movement);
            }
        }
        return possibleMovements;
    }
 
    function getMovement(direction) {
        let nextSquare = moveRule.getCurrentCoordinate().getNextCoordinate(direction);
        if(moveRule.isEmptyCoordinate(nextSquare) || moveRule.isOpposingColor(nextSquare))
            return nextSquare;
        return undefined;
    }
 
    return {
        ...moveRule,
        ...{
            getPossibleMovements,
        }
    }
}
 
export {
    getKingMoveRule
}