All files / piece piecesBuilder.js

100% Statements 43/43
100% Branches 11/11
100% Functions 4/4
100% Lines 43/43

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 4413x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 940x 940x 7520x 60160x 60160x 60160x 60160x 60160x 60160x 60160x 60160x 60160x 60160x 60160x 60160x 60160x 940x 13x 60449x 60449x 60449x 60449x 60449x 60449x 5322x 5322x 5322x 5322x 5322x 5322x 5322x  
import { createPiece } from './piece.js';
import { moveRuleMap } from "../moveRule/moveRuleMap.js";
import { PieceColorEnum } from "./pieceColorEnum.js";
import { PieceNameMap } from "./pieceNameMap.js";
 
function piecesBuilder(pieceStringLayout){
    let pieces = {};
 
    function buildFromLayout() {
        let stringCounter = 0;
        for (let i=8; i>0; i--){
            for (let letter = 0; letter < "abcdefgh".length; letter++) {
                let position = "abcdefgh"[letter]+i.toString();
                let stringAbbreviation = pieceStringLayout[stringCounter].trim();
                let piece = getPiece(stringAbbreviation, position);
                pieces[position] = piece;
                stringCounter++;
            }
        }
        return pieces;
    }
 
    return {
        buildFromLayout
    }
}
 
function getPiece(abbreviation, position) {
    const pawnFirstPositions = { B : '7', W : '2' };
    const color = abbreviation[0] === 'W' ? PieceColorEnum.White : PieceColorEnum.Black;
    return createPiece(
        abbreviation,
        position,
        abbreviation == '_'? undefined : moveRuleMap[PieceNameMap[abbreviation[1]]](
            position.includes(pawnFirstPositions[color.getAbbreviation()]),
            !color.isWhite()
        ));
}
 
export {
    piecesBuilder,
    getPiece
}