Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Sudoku - Checking Rules

Tags:

javascript

I'm working on a javascript sudoku, and as far as validation of input goes, I'm stumped. As each value is entered into the sudoku table, I have an onkeyup triggering a function which I am attempting to use to make sure that no value enters a row/column/box where such a value already exists. This is my first time attempting such a project, so I apologize in advance for the noob code.

Here's what I've got so far:

function myFunction(row) {
var x = 0;
var arraySolution = [
    [x, x, 4, 5, x, 3, x, 7, x],
    [x, x, x, x, x, x, 3, 1, x],
    [3, 5, x, x, x, 9, 2, x, x],
    [x, x, x, x, 2, x, 9, 3, 7],
    [6, x, 9, x, x, x, 4, x, 8],
    [4, 7, 2, x, x, x, x, x, x],
    [x, x, 1, x, x, x, x, 5, 2],
    [x, 4, 5, x, x, x, x, x, x],
    [x, 6, x, 8, x, 1, 7, x, x]
];

var num = row.innerHTML; //get its innerHTML
num = parseInt(num, 10);
if (num < 1 || num > 9) {
    row.innerHTML = "";//clear it if num is not what you expect
    alert("Please Enter a Number Between 1 and 9.");
}


//Checks Row
for (var b = 0; b < 9; b++) {
    if (num != arraySolution[0][b]) {
        console.log("This is a valid input");
        b++;
    }
    else if (num = arraySolution[0][b]) {
        alert("this is an invalid input");

        row.innerHTML = "";
    }
}
}

I know that using x as a placeholder is probably not the best plan, but at this point it is mostly for testing purposes so values being input wont exist before the testing starts. Where I attempt to check row, I'm obviously trying to loop through the array at indexes (0,0) -> (0,8) and check that no value exists twice in that row. I had this working to check a single row, but seem to have broken it while attempting to add more rows to check.

A brief example of what I was trying to check multiple rows (I completely understand what I need to be doing as far as actual sudoku logic, but I cannot get this thing to validate)

    //Checks Row
for (var b = 0; b < 9; b++) {
    if ((num != arraySolution[0][b]) || (num != arraySolution[1][b])) {
        console.log("This is a valid input");
        b++;
    }
    else if ((num = arraySolution[0][b]) || (num != arraySolution[1][b])){
        alert("this is an invalid input");
        row.innerHTML = "";
    }
}
like image 578
Jeremy Stone Avatar asked Jan 21 '26 15:01

Jeremy Stone


1 Answers

You can use this function to validate a solution :

// returns true if arraySolution is valid, false otherwise
function valid(arraySolution) {
    for (var y = 0; y < 9; ++y) {
        for (var x = 0; x < 9; ++x) {
            var value = arraySolution[y][x];

            if (value) {
                // Check the line
                for (var x2 = 0; x2 < 9; ++x2) {
                    if (x2 != x && arraySolution[y][x2] == value) {
                        return false;
                    } 
                }

                // Check the column
                for (var y2 = 0; y2 < 9; ++y2) {
                    if (y2 != y && arraySolution[y2][x] == value) {
                        return false;
                    } 
                }

                // Check the square
                var startY = Math.floor(y/3)*3;
                for (var y2 = startY; y2 < startY + 3; ++y2) {
                    var startX = Math.floor(x/3)*3;
                    for (x2 = startX; x2 < startX + 3; ++x2) {
                        if ((x2 != x || y2 != y) && arraySolution[y2][x2] == value) {
                            return false;
                        }
                    }
                }
            }
        }
    }

    return true;
}
like image 162
Magus Avatar answered Jan 24 '26 06:01

Magus