All files / piece piece.js

100% Statements 111/111
100% Branches 29/29
100% Functions 19/19
100% Lines 111/111

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 11213x 13x 13x 13x 13x 13x 13x 60512x 60512x 5364x 2273x 2273x 60512x 60512x 60512x 60512x 5364x 5364x 5364x 60512x 60512x 60512x 60512x 60512x 1548x 60512x 2x 2x 2x 2x 2x 60512x 10x 60512x 2x 2x 60512x 128x 128x 60512x 5x 60512x 1x 1x 60512x 46x 46x 60512x 35x 60512x 7x 7x 5x 5x 5x 5x 5x 5x 5x 5x 60512x 30x 30x 30x 60512x 671x 671x 671x 671x 60512x 5x 5x 60512x 3897x 3897x 60512x 10633x 10633x 10633x 60512x 6159x 6159x 6159x 60512x 1x 60512x 60512x 60512x 60512x 60512x 60512x 60512x 60512x 60512x 60512x 60512x 60512x 60512x 60512x 60512x 60512x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x  
import { PieceColorEnum } from "./pieceColorEnum.js";
import { PieceNameMap } from "./pieceNameMap.js";
 
function createPiece(pieceAbbreviation, piecePosition, pieceMovementRule) {
 
    let abbreviation = pieceAbbreviation;
    const color = abbreviation === '_' ? PieceColorEnum.Empty : abbreviation[0] === 'W' ? PieceColorEnum.White : PieceColorEnum.Black;
    let fullName = color === PieceColorEnum.Empty ? 'empty' : color.getLiteral() + ' ' + PieceNameMap[abbreviation[1]];
    let position = piecePosition;
    let movement = pieceMovementRule;
 
    function getAbbreviation() {
        return abbreviation;
    }
 
    function setAbbreviation(newAbbreviation) {
        abbreviation = newAbbreviation;
    }
 
    function getFullName() {
        return fullName;
    }
 
    function setFullName(newFullName) {
        fullName = newFullName;
    }
 
    function getPosition() {
        return position;
    }
 
    function getMovementRule() {
        return movement;
    }
 
    function setMovementRule(newMovementRule) {
        movement = newMovementRule;
    }
 
    function setPosition(positionPiece) {
        position = positionPiece;
    }
 
    function updateCurrentPosition(position, pieces) {
        movement.updateCurrentPosition(position, pieces);
    }
 
    function isPossibleMove(destination, pieces) {
        let res = movement.isPossibleMove(position, destination, pieces);
        if (res) {
            updateCurrentPosition(destination, pieces);
            let nextMoveRule = movement.getNextMoveRule(abbreviation[1]);
            movement = nextMoveRule.moveRule;
            abbreviation = abbreviation[0] + nextMoveRule.abbreviation;
            fullName = color.getLiteral() + ' ' + PieceNameMap[abbreviation[1]]
        }
        return res;
    }
 
    function getPossibleMovements(pieces) {
        return movement.getPossibleMovements(position, pieces).map(mv => mv.getPosition());
    }
 
    function getAttackPositions(pieces) {
        return movement.getAttackMovements(position, pieces);
    }
 
    function isWhite() {
        return color.isWhite();
    }
 
    function isOpposingColor(piece) {
        return !piece.isOfColor(color) && !piece.isEmpty();
    }
 
    function isOfColor(colorParam) {
        return color === colorParam;
    }
 
    function isEmpty() {
        return color.isEmpty();
    }
 
    function getMovementError() {
        return movement.getErrorMessages();
    }
 
    return {
        getAbbreviation,
        setAbbreviation,
        getFullName,
        setFullName,
        getMovementRule,
        setMovementRule,
        getPosition,
        setPosition,
        isPossibleMove,
        getPossibleMovements,
        getAttackPositions,
        isWhite,
        isOpposingColor,
        isOfColor,
        isEmpty,
        getMovementError,
        updateCurrentPosition
    };
}
 
export {
    createPiece,
}