All files / moveRule pawnMoveRule.js

100% Statements 94/94
100% Branches 35/35
100% Functions 11/11
100% Lines 94/94

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 9513x 13x 13x 13x 13x 13x 13x 13x 3536x 3536x 3536x 3536x 3536x 3536x 35x 35x 35x 35x 35x 3536x 5x 5x 2x 2x 2x 3x 3x 3536x 5x 5x 5x 1x 4x 5x 5x 3536x 336x 336x 336x 336x 3536x 35x 35x 35x 21x 21x 16x 16x 16x 3536x 371x 371x 371x 371x 371x 371x 371x 3536x 70x 70x 70x 50x 70x 20x 3536x 371x 371x 371x 29x 358x 342x 3536x 371x 371x 29x 358x 342x 3536x 3536x 3536x 3536x 3536x 3536x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x  
import { createPieceMoveRule } from "./pieceMoveRule.js";
import { getQueenMoveRule } from "./queenMoveRule.js"
import { DirectionEnum } from "./directionEnum.js"
 
function getPawnMoveRule(isFirstMovementP, isFromNorthSideP) {
    let moveRule = createPieceMoveRule();
    let isFirstMovement = isFirstMovementP;
    let isFromNorthSide = isFromNorthSideP;
 
    function getPossibleMovements () {
        let possibleMovements = [];
        possibleMovements.push(...getForwardMovements());
        possibleMovements.push(...getEatingMovements());
        return possibleMovements;
    }
 
    function getNextMoveRule(currentAbbreviation) {
        isFirstMovement = false;
        if (shouldTurnToQueen())
            return { moveRule: getQueenMoveRule(), abbreviation: 'Q' }
        else
            return { moveRule: this, abbreviation: currentAbbreviation }
    }
 
    function shouldTurnToQueen () {
        let coordinate = moveRule.getCurrentCoordinate();
        if (isFromNorthSide && coordinate.getRow() <= 1)
            return true;
        if (!isFromNorthSide && isFromNorthSide === false && coordinate.getRow() >= 8)
            return true;
        return false;
    }
 
    function getAttackMovements (position, pieces) {
        moveRule.updateCurrentPosition(position, pieces);
        return getEatingMovements().map(mv => mv.getPosition());
    }
 
    function getForwardMovements() {
        let movements = [];
        let nextSquare = getForwardSquare(moveRule.getCurrentCoordinate());
        if (moveRule.isEmptyCoordinate(nextSquare)) {
            movements.push(nextSquare);
            if (isFirstMovement && moveRule.isEmptyCoordinate(getForwardSquare(nextSquare)))
                movements.push(getForwardSquare(nextSquare));
        }
        return movements;
    }
 
    function getEatingMovements() {
        let movements = [];
        let rightDiagonal = getDiagonalRightSquare(moveRule.getCurrentCoordinate());
        let leftDiagonal = getDiagonalLeftSquare(moveRule.getCurrentCoordinate());
        if (moveRule.isOpposingColor(rightDiagonal))
            movements.push(rightDiagonal);
        if (moveRule.isOpposingColor(leftDiagonal))
            movements.push(leftDiagonal);
        return movements;
    }
 
    function getForwardSquare(originCoordinate) {
        if (isFromNorthSide)
            return originCoordinate.getNextCoordinate(DirectionEnum.SOUTH);
        else
            return originCoordinate.getNextCoordinate(DirectionEnum.NORTH);
    }
 
    function getDiagonalRightSquare(origin) {
        if (isFromNorthSide)
            return origin.getNextCoordinate(DirectionEnum.SOUTHEAST);
        else
            return origin.getNextCoordinate(DirectionEnum.NORTHEAST);
    }
 
    function getDiagonalLeftSquare(origin) {
        if (isFromNorthSide)
            return origin.getNextCoordinate(DirectionEnum.SOUTHWEST);
        else
            return origin.getNextCoordinate(DirectionEnum.NORTHWEST);
    }
 
    return {
        ...moveRule,
        ...{
            getPossibleMovements,
            getAttackMovements,
            getNextMoveRule
        }
    };
}
 
export {
    getPawnMoveRule
}