Sudoku Solver – 2nd Session

This is just a stream of thought blog post as I’m coding a Sudoku solving program.  I’m a beginner coder, so the code won’t be the best, but I’m learning as I go.

Click here to see the first post

Last thing I said the the last post was to use 3 digits to reference the cell. A short thought later and I’m thinking 4 would be easier.

[column][row][cell number in sub-grid][sub-grid]

Why 4?

  • I can query a row by looping through the column number
  • I can query a column by looping through the row number
  • I can check for a unique entry in a single sub-grid
  • Cell number will come in to play when solving more complex puzzles

Sounds simple right?  I’m writing this after I’ve done the work.

There are 2 ways to do this. A straight up 9×9 grid.  The row and column numbers are easy.  Adding in a sub-grid number and a cell number would make it a bit harder.

I ended up working out the logic by working on the smallest component, a cell, then building a 3×3 sub-grid (calling the cell function for each cell), then finally building the entirety of the grid.

Below is what I ended up coding.  I know they say good code can be read well and doesn’t need comments. Below has a comment for almost every line.

<?php
$grid = "<table border='2'>\n";     //	Grid table
	// Set working variables
	$subgrid_number=1;  // 1-9 sub-grids
	$cell_working=1;    // 1-9 cells per sub-grid
	$x_start=1;         // Setting outside of loop for initial value
	$y_start=1;         // Setting outside of loop for initial value
	
	for($a = 1; $a <=3; $a++){
			$grid .= "  <tr>\n";                // Start sub-grid row
				for($b = 1; $b <=3; $b++){      // Each loop builds a row of 3 sub-grids
					$grid .= build_one_subgrid($x_start,$y_start);
					$subgrid_number++;          // After each sub-grid is created, increase number
					$x_start = $x_start+3;      // In the sub-grid loop, x starts at 1,3,7
				}
			
			$grid .= "  </tr>\n";               // End sub-grid row
			$y_start = $y_start+3;              // Set next set of y_starts to 3 for the sub-grid row
			$x_start=1;                         // Set x_start back to 1 as the colums start again
			}

$grid .= "</table></td>\n";         //	End Grid table
		
//	build_one_subgrid - used to build a 9x9 sub-grid
//                      $x_start = Starting column
//                      $y_start = Starting row 
function build_one_subgrid($x_start,$y_start){
	
	global $grid;               // So we can add to the grid output
	global $subgrid_number;     // Sub-grid number to add to table
	$cell_working = 1;          // Start value for cell
	$x_working = $x_start;      // Using a working variable to build subgrid
	$y_working = $y_start;      // Using a working variable to build subgrid
	
	$grid .= "    <td><table border='1'>\n";    //Starting sub-grid table
	
	for($a = 1; $a <=3; $a++){                        // Loop for row
			$grid .= "      <tr>\n";
				for($b = 1; $b <=3; $b++){            // Loop for column
					$grid .= build_cell($x_working,$y_working,$cell_working,$subgrid_number);
					$cell_working++;                  // Increase cell_working (rest next time it's run)
					$x_working++;                     // Increase column number
				}
			$x_working = $x_start;                    // Colums in one sub-grid all start at the same number
			$y_working++;                             // Increase row number
			$grid .= "      </tr>\n";
			}
	$grid .= "    </td></table>\n";             //End sub-grid table
}

// build_cell - code to build a single cell
//              $x - column number
//              $y - row number
//              $cellno - cell number
//              $subgrid_number - sub-gridnumber
function build_cell($x,$y,$cellno,$subgrid_number){
	return "        <td><input type='text' name='$x$y$cellno$subgrid_number' size='4' value='$x$y$cellno$subgrid_number'></input></td>\n";
}
echo $grid;
?>

Produces this output:

 

Next ‘Session’ – Entering a puzzle to be solved.

Leave a Reply