All files / moveRule horseMoveRule.js

100% Statements 55/55
100% Branches 16/16
100% Functions 6/6
100% Lines 55/55

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 5613x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 298x 298x 109x 109x 218x 218x 218x 218x 218x 218x 436x 436x 436x 298x 872x 872x 872x 872x 872x 872x 2100x 2100x 2100x 2100x 872x 422x 422x 872x 298x 2100x 2100x 2100x 2100x 2100x 2100x 298x 298x 298x 298x 298x 298x 298x  
import { createPieceMoveRule } from "./pieceMoveRule.js";
import { DirectionEnum } from "./directionEnum.js"
 
 
function getHorseMoveRule() {
    let moveRule = createPieceMoveRule();
 
    function getPossibleMovements () {
        return getLShapes();
    };
 
    function getLShapes () {
        let movements = [];
        for(let vertical of [DirectionEnum.NORTH, DirectionEnum.SOUTH]){
            for(let horizontal of [DirectionEnum.EAST, DirectionEnum.WEST]){
                movements.push(...getLShapeMovement(vertical, horizontal));
                movements.push(...getLShapeMovement(horizontal, vertical));
            }
        }
        return movements;
    }
 
    function getLShapeMovement(singleSquareDirection, doubleSquareDirection) {
        let movements = [];
        let origin = moveRule.getCurrentCoordinate();
 
        let directions = [singleSquareDirection, doubleSquareDirection, doubleSquareDirection];
 
        for(let direction of directions) {
            let partMovement = origin.getNextCoordinate(direction);
            if(isInvalidPartMovements(partMovement, origin))
                return movements;
            origin = partMovement;
        }
 
        if (moveRule.isEmptyCoordinate(origin) || moveRule.isOpposingColor(origin))
                movements.push(origin);
        return movements;
    }
 
    function isInvalidPartMovements(origin, destination){
        return origin.getPosition() === destination.getPosition();
    }
 
    return {
        ...moveRule,
        ...{
            getPossibleMovements,
        }
    }
}
 
export {
    getHorseMoveRule
}