Checking if a checker can move Imagine a game of Checkers, but you can move left right up down instead of diagonally. I have made an array to store liberties (available moves) and use if statements to update if a checker can move there.

Checkers can share liberties:



So the little black squares are liberties.

Here's my ugly if statement to update:

function updateLiberties(x, y, color)
{
    liberties[x][y] = 'None';

    if (color == 'white')
    {
        if (liberties[x + 1][y] == 'clear')
        {
            liberties[x + 1][y] = color;
        }
        else if (liberties[x + 1][y] == 'black')
        {
            liberties[x + 1][y] = 'both';
        }

        if (liberties[x - 1][y] == 'clear')
        {
            liberties[x - 1][y] = color;
        }
        else if (liberties[x - 1][y] == 'black')
        {
            liberties[x - 1][y] = 'both';
        }

        if (liberties[x][y + 1] == 'clear')
        {
            liberties[x][y + 1] = color;
        }
        else if (liberties[x][y + 1] == 'black')
        {
            liberties[x][y + 1] = 'both';
        }

        if (liberties[x][y - 1] == 'clear')
        {
            liberties[x][y - 1] = color;
        }
        else if (liberties[x][y - 1] == 'black')
        {
            liberties[x][y - 1] = 'both';
        }
    }

    if (color == 'black')
    {
        if (liberties[x + 1][y] == 'clear')
        {
            liberties[x + 1][y] = color;
        }
        else if (liberties[x + 1][y] == 'white')
        {
            liberties[x + 1][y] = 'both';
        }

        if (liberties[x - 1][y] == 'clear')
        {
            liberties[x - 1][y] = color;
        }
        else if (liberties[x - 1][y] == 'white')
        {
            liberties[x - 1][y] = 'both';
        }

        if (liberties[x][y + 1] == 'clear')
        {
            liberties[x][y + 1] = color;
        }
        else if (liberties[x][y + 1] == 'white')
        {
            liberties[x][y + 1] = 'both';
        }

        if (liberties[x][y - 1] == 'clear')
        {
            liberties[x][y - 1] = color;
        }
        else if (liberties[x][y - 1] == 'white')
        {
            liberties[x][y - 1] = 'both';
        }
    }
}


So a liberty can either be None (a checker is on it), clear (nothing is on it), black, white or both.

Any chance I make this look a little neater?
Well, to start with, there is 99% the same code, repeated twice. This is a very bad code smell. You need to turn the repeated code part into a function, like

function checkLiberties (color, otherColor) {
    if (liberties[x + 1][y] == 'clear') {
        liberties[x + 1][y] = color;
    } else if (liberties[x + 1][y] == otherColor) {
        liberties[x + 1][y] = 'both';
    }

    if (liberties[x - 1][y] == 'clear') {
        liberties[x - 1][y] = color;
    } else if (liberties[x - 1][y] == otherColor) {
        liberties[x - 1][y] = 'both';
    }

    if (liberties[x][y + 1] == 'clear') {
        liberties[x][y + 1] = color;
    } else if (liberties[x][y + 1] == otherColor) {
        liberties[x][y + 1] = 'both';
    }

    if (liberties[x][y - 1] == 'clear') {
        liberties[x][y - 1] = color;
    } else if (liberties[x][y - 1] == otherColor) {
        liberties[x][y - 1] = 'both';
    }   
}


and then just call it twice, as

checkLiberties("black", "white");
checkLiberties("white", "black");


After this step, you need to check the same thing inside the function. Where you can spot a pattern in the code, try to turn into a function. I am sure the four chunks inside can become one.