function playMove(){
    var cell = $(this);

    // Check if we're deleting the last entry.
    // If so, roll back the position of guess and clear game-board.
    if (cell.attr('id') == "delete") {
        $("#answer").children("."+(i)).text(0);
        if (i != 0) {
            i--;
        }
        $("#"+guess[i]).bind("click", playMove);
        guess[i] = 0;

    } else {
        // Get the number clicked from button ID.
        guess[i] = cell.attr('id').split(" ")[0];

        // Reset the game-board if this is start of a new guess.
        if (i == 0) {
            $(".game-board").empty().append(0);
        }

        // Update game-board with the number entered.
        // Also disable click on this number as duplicates are not allowed.
        $("#answer").children("."+(i+1)).text(guess[i]);
        cell.unbind("click");
        i++;

        // When all digits are entered, disable all future clicks.
        // Then process bulls and cows.
        // Update the input entry.
        if (i == len) {
            cleanupBoard();

            var census = countBovine(num, guess);
            showScore(census.bulls, census.cows);

            // FIXME: Should this be in a processMove() function?
            // Correct number guessed, update the game-board to success.
            if (census.bulls == len) {
                showFinalResult(nGuesses, true);

                // If not then reset everything for next guess.
            } else {
                if (!checkMaxGuess()) {
                    i = 0;
                    nGuesses++;
                    guess = [0, 0, 0, 0];
                    initBoard();
                }
            }
        }
    }
}
/* Decode which button was pressed based on button ID.
 * - Also handles the delete event.
 * TODO: Tidy up the function. It's too long and does things which
 * are not "playMove"
 * ------------------------------------------------------------------------- */
Decode which button was pressed based on button ID.
- Also handles the delete event.
TODO: Tidy up the function. It's too long and does things which
are not "playMove"
Decode which button was pressed based on button ID . - Also handles the delete event . TODO : Tidy up the function . It ' s too long and does things which are not " playMove "
function playMove(){
    var cell = $(this);
    if (cell.attr('id') == "delete") {
        $("#answer").children("."+(i)).text(0);
        if (i != 0) {
            i--;
        }
        $("#"+guess[i]).bind("click", playMove);
        guess[i] = 0;
    } else {
        guess[i] = cell.attr('id').split(" ")[0];
        if (i == 0) {
            $(".game-board").empty().append(0);
        }
        $("#answer").children("."+(i+1)).text(guess[i]);
        cell.unbind("click");
        i++;
        if (i == len) {
            cleanupBoard();
            var census = countBovine(num, guess);
            showScore(census.bulls, census.cows);
            if (census.bulls == len) {
                showFinalResult(nGuesses, true);
            } else {
                if (!checkMaxGuess()) {
                    i = 0;
                    nGuesses++;
                    guess = [0, 0, 0, 0];
                    initBoard();
                }
            }
        }
    }
}
function playMove ( ) { var cell = $ ( this ) ; if ( cell . attr ( 'id' ) == "delete" ) { $ ( "#answer" ) . children ( "." + ( i ) ) . text ( 0 ) ; if ( i != 0 ) { i -- ; } $ ( "#" + guess [ i ] ) . bind ( "click" , playMove ) ; guess [ i ] = 0 ; } else { guess [ i ] = cell . attr ( 'id' ) . split ( " " ) [ 0 ] ; if ( i == 0 ) { $ ( ".game-board" ) . empty ( ) . append ( 0 ) ; } $ ( "#answer" ) . children ( "." + ( i + 1 ) ) . text ( guess [ i ] ) ; cell . unbind ( "click" ) ; i ++ ; if ( i == len ) { cleanupBoard ( ) ; var census = countBovine ( num , guess ) ; showScore ( census . bulls , census . cows ) ; if ( census . bulls == len ) { showFinalResult ( nGuesses , true ) ; } else { if ( ! checkMaxGuess ( ) ) { i = 0 ; nGuesses ++ ; guess = [ 0 , 0 , 0 , 0 ] ; initBoard ( ) ; } } } } }
Decode which button was pressed based on button ID.
Decode which button was pressed based on button ID .
// Check if we're deleting the last entry. // If so, roll back the position of guess and clear game-board. // Get the number clicked from button ID. // Reset the game-board if this is start of a new guess. // Update game-board with the number entered. // Also disable click on this number as duplicates are not allowed. // When all digits are entered, disable all future clicks. // Then process bulls and cows. // Update the input entry. // FIXME: Should this be in a processMove() function? // Correct number guessed, update the game-board to success. // If not then reset everything for next guess.