Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# sudoku

christian fabiano
Greenhorn
Posts: 3
i am trying to program a game . its a sudoku game . got an idea of the algorithm but not too sure how to code it. its yo read the puzzle from a file and print out the answer. the puzzle is below. it can be saved onto a seperate file and then soleved.

640013900
100026400
029045700
002000830
860037019
700209000
001300690
936408020
005000000
the pseudocode i derived to solving it is below:

i have a logic and below is can i say a rough pseudocode;
// the sudoku is a 9x9 grid in 9 sections. i also have 10 layers with the 0 layer being my final solution layer and other layers numbered from 1-9. i thought about doing a 3 d array [9][8][8]. or something like that.
//first check if a number can enter any of the boxes starting at 1 all the way to 9.(method-number roll).
//starting at 1, while rolling...... do a section scan(method): this checks the first section to see if number being rolled is in the section.(scan thru all the sections with 1 then after last section, move to 2 then to 9)
// if in section, move to next section
//else do a cell check(method): this checks empty cells in that section.
//for each empty cell, if the number being rolled is not on the same entire row or column, enter it inot its layer eg. if 2 is being rolled, enter the number as a temp solution to the 2nd layer.
//fthe cross out method- after rolling thru with 9, after the last section, then do this method. if a number is the onoly number in its section on the same layer or all layers for a cell, then cross out all similar numbers on the same row, column or section, then move up to the 0 layer.
//print solution layer- when there is no 0/or emptyspace left in the 0 layer, print out the entire 0 layer.

If there is a simpler way of solving it i am open to ideas. as i said earlier just need a code so that i can learn from it.

thanks

Shiraz Khan
Ranch Hand
Posts: 51
U can check out the Java implementation of sudoku at my site :

I have given out the source code of the sudoku game which I had developed some days back at the above address.

Ryan McGuire
Ranch Hand
Posts: 1085
4

Layne Lund
Ranch Hand
Posts: 3061
I like that you have thought this out on your own and come up with your own algorithm. I honestly think this is a good way to approach a problem, even if it is as popular as sudoku that has been solved many times. I think that it will do more harm than good for you to look at a completed solution.

With that said, I don't quite understand some of the terminology in your pseudocode. I will ask some questions and if you can explain more clearly, perhaps that will help you see to code it. If not, we can continue to discuss it from there.

1) What do you mean by "//first check if a number can enter any of the boxes starting at 1 all the way to 9.(method-number roll)." In particular what is a "number roll"? Are you generating a random number here? (That's what I understand from the word "roll" as in "roll a dice".) If so, why are you using this approach? I'm not trying to say that it is a bad idea. Far from it. I think you should justify why it's a GOOD idea. Also, are there other approaches other than picking a random number?

2) What is a "section scan"? I guess you mean that a "section" is one of the 3x3 sub-grids where you have to use the numbers 1 through 9. For example, where the first three rows and the first three columns overlap is a "section." Is that correct? So what do you mean by "scanning" the section?

3) What does "if in section, move to next section" mean? First of all, what does it mean to "be in a section"? How do you detect this? What does it mean if you are NOT in a section?

4) What is a "cell check"? Same idea as clarifying what a section scan is.

5) I don't understand what you mean by the "cross out method". Can you explain this further?

If you are still inderested in solving this yourself, rather than looking at a finished solution, I thik it will help a lot for you to put some more detail into your pseudocode. I hope the above questions will help you do that.

Layne