1
\$\begingroup\$

I'm programming a checkers game in typescript. I'd like to explain some important details to understand how the following code works. In my game coordinates are a string, like "b4". I want to get the neighbors of a piece coordinates, but only the neighbors that make sense depending on the player and if the piece in these coordinates is a king or not. For example, if on "b4" it's player one, I want to get only "a5" and "c5" as possible neighbors.

I wrote the following code to find the neighbors of given coordinates:

    type CornerType = {
    [key: string]: string | null;
}

const getPieceCornersCoordinates = (coordinates: string, piece: Piece) => {
    const {row, col} = convertStringCoordinatesToNumberCoordinates(coordinates);
    
    const columnLeft  = ((col - 1) >= 0) ? String.fromCharCode((col - 1) + 97) : false;
    const columnRight = col +1 <= 7 ? String.fromCharCode((col + 1) + 97) : false;
    
    const rowUpper = row +1 < 9 ? row +1 : false;
    const rowLower = row -1 > 0 ? row -1 : false;
    
    let corners : CornerType = {};

    if(piece.isKing){
        corners.leftUpper  = columnLeft  !== false && rowUpper !== false ? columnLeft  + rowUpper : null;
        corners.rightUpper = columnRight !== false && rowUpper !== false ? columnRight + rowUpper : null;
        corners.leftLower  = columnLeft  !== false && rowLower !== false ? columnLeft  + rowLower : null;
        corners.rightLower = columnRight !== false && rowLower !== false ? columnRight + rowLower : null;
    } else if(piece.player === "Player One"){
            corners.leftLower  = columnLeft  !== false && rowLower !== false ? columnLeft  + rowLower : null;
            corners.rightLower = columnRight !== false && rowLower !== false ? columnRight + rowLower : null;
    } else {
            corners.leftUpper  = columnLeft  !== false && rowUpper !== false ? columnLeft  + rowUpper : null;
            corners.rightUpper = columnRight !== false && rowUpper !== false ? columnRight + rowUpper : null;
    }

    deleteNullCorners(corners);
    
    return corners;
}

const deleteNullCorners = (corners: CornerType) => {
    for(let key in corners){
        if(corners[key] === null){
            delete corners[key];
        }
    }
}

I think this code is not well written, how can I improve it?

\$\endgroup\$
1
\$\begingroup\$

First, post your entire code file (a working checkers game), not just this one function, if you want to know how to improve your code. It's hard to suggest improvements for just one function.

This function doesn't do anything clear, and should probably be renamed or split up or merged with other functions. At best, it returns all the possible moves which aren't captures. If that's what you want (unlikely) name it better to match that.

I recommend not using a string for coordinates. Just use a pair of numbers. You can convert to display it to the user, etc where needed.

Return an array of coordinates or an array of directions, not a dict of directionName -> coordinate.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.