# Sudoku Layout

Here is a brief description of the program and how it works.

if this if first run,
setupBoard()
else
get the values of the cells from what was submitted
calculatePossibles()
attemptToSolve()
checkBoard()
if no solution solution possible give message "Unable to solve as this Sudoku has no solution"
if solved give message "Successfully Solved!"
if we haven't been able to solve give message "Unable to solve using smart logic."
displayBoard()
exit program

function attemptToSolve()
{
displayBoard()
if not yet solved, test1()
if not yet solved, test2r()
if not yet solved, test2c()
if not yet solved, test2s()
if not yet solved, test3r()
if not yet solved, test3c()
if not yet solved, test4r()
if not yet solved, test4c()
}

function checkBoard()
{
if all cells are filled, return "finished"
loop through all cells
if cell is empty and possible is empty return "UNABLE TO PROCEED: Found that unsolved cell has no POSSIBLES"
return "not finished, and we can continue working"
}

function setupBoard()
{
if the size of the board is not valid (ie. it is not a square of a smaller number)
echo "<p>The number of digits must be a square of a smaller number. Valid selections are 4 (the square of 2), 9 (the square of 3), 16 (the square of 4), etcetera.</p>";
else
setup the grid
Draw the grid and allow the user to enter some numbers
displayBoard()
}

function displayBoard()
{
loop through all the rows
loop through all the columns
draw a square and put in the value of that cell
}

function countArray(\$arrayToCount)
{
loop through all the rows
loop through all the columns
if cell is not empty, count it
return count;
}

function calculatePossibles()
{
Initially a cell can be any number, we build a string of all the possible values that looks like this "123456789" for a 9x9 puzzle, or "123456789abcdefg" for a 16x16 puzzle
create an array called 'possibles' and put the string in each cell
Now we start looking through the grid and if there is a number filled in, we remove it from the possibles
loop through all the rows
loop through all the columns
if cells has a value
possibles for this cell is emptied
removePossible()
}

function removePossible()
{
remove this number from the rest of the possibles in this COLUMN
remove this number from the rest of the possibles in this ROW
remove this number from the rest of the possibles in this SQUARE
first we figure out which square we are in.
}

function setCell()
{
check for a match in this column
check for a match in this row
check for a match in this square
if no matches were found
set cell value
removePossible()
}

function test1()
{
// Finds cells that have only a single possible answer. If that's the only possible number for that cell, then it must be the number for that cell.
loop through all the rows
loop through all the columns
if possibles is only 1 digit
setCell()
recursively attemptToSolve()
}

function test2r()
{
// finds where a number is only possible once in a ROW
// if that's the only place in the column it COULD go, then that's where it MUST go
loop through all the rows
loop through all the numbers (1 to 9)
if there's only one place it can be
setCell()
recursively attemptToSolve()
}

function test2c()
{
// finds where a number is only possible once in a COLUMN
// if that's the only place in the column it COULD go, then that's where it MUST go
loop through all the columns
loop through all the numbers (1 to 9)
if there's only one place it can be
setCell()
recursively attemptToSolve()
}

function test2s()
{
// finds where a number is only possible once in a SQUARE
// if that's the only place in the square it COULD go, then that's where it MUST go
}

function test3r()
{
// Looks for sets of two possibles in a row
// then will remove those digits from other possibles along the same row
}

function test3c()
{
// Looks for sets of two possibles in a column,
// then will remove those digits from other possibles along the same column
}

function test4r()
{
// Looks for sets of 3 possibles that match in a row,
// then will remove possibles along the same row
// Will also match a set of 3, set of 3 and set of 2
// eg. "156", "15", "156 would be a match and remove 1, 5, & 6 from the other possibles
}

function test4c()
{
// Looks for sets of 3 possibles that match in a column,
// then will remove possibles along the same column
// Will also match a set of 3, set of 3 and set of 2
// eg. "156", "15", "156 would be a match and remove 1, 5, & 6 from the other possibles
}