Sudoku Solver – 3rd 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
Click here to see the second post

The reason I’m using functions, especially the cell function, was the initial reason I started to learn about php. The ability to use one piece of code many times in one document.

Ok, the real reason I started to use php was I coded an html website and didn’t want to update the menu on each page manually, so I used a simple ‘include’ to include just the menu code.

Back to the code.

I’m going to have a few ‘states’ or modes for the page. The initial state will allow you to input numbers in to the puzzle to solve. The second state will be for displaying the working/final display.

So, I’ll use a variable called $mode, it will have 3 starts:

  1. entry
  2. working
  3. solved

Updating the building_cell function:

I’ll populate the code for working and solved later.

I’ve wrapped the entire grid in a form which will submit the results to itself, which once I’ve worked out how to store the input, will be displayed in the working output.

The grid generator is in a separate file, so I’m also creating a separate file to deal with the input and the data that will be being stored in an array.

I’ll generate the array on the first submit and any further submits I’ll just update the array along the way, so the first submit will generate an array based on $_POST data for each cell and subsequent submits will pass on a serialised array in the POST data field called ‘last_state’, which will be where the solve is at.

In the table generator, I’m adding this to the bottom to handle form submitting:

And also preliminary state settings and the start of the form at the top.  I’ll post the full code in the next example.

I’ve also had to add in some basic CSS so the grid display correctly.

Next session, I’ll start to think about how I’m going to work and solve the puzzle.

The biggest issue is I had hoped to make a ‘step through’ so ‘click and see what was solved this time’ type scenario, which would mean upwards of 60 clicks… alternatively, just providing a solution. I still need to work out a middle ground.

Leave a Reply