Submitted by MGA on

Forums:

Hi,

I am studying the Sudoku Game.

Yes, I know Chris has left a very nice game in Code Library but qbguy presented a game at QB64.net that I liked very much and wanted to figure out why!

From the beginning I liked the way qbguy hid his cells. Chris version is random and so games could get multiple solutions ie you can never solve logically all the cells, at some point you have to plug-in cell values in order to finish a puzzle. Of course some plug-ins are better than others and that is part of the Game too... But qbguys code had same problem, the hiding cells method creates multiple solutions. But qbguys dispersion of blank cells seems better, more evenly spread out... still working on this quality of the Game.

So last night I finally figured out the main reason why I liked qbguys puzzles so much. They have an underlining pattern that makes them almost trivial to solve ONCE YOU FIGURE OUT THE PATTERN! We won t say how long I ve been playing with the puzzles before I finally figured out the pattern. I had been working on alternate board making algos and discovered my first board maker had the same fatal flaw of the underlining pattern that makes solving the games trivial (once you figure out the pattern).

Here is the code for that first board maker study, so maybe you can see the problem of making a game trivial to solve:

Notice, I have employed a Solved Function so the Game will acknowledge a successful solution, shoot off fireworks (or some other flashy signal of solving the puzzle) and then offer you another game because, of course, you will want to play again, then! I have added that to Chris game but I wanted to study the hiding cell dispersion problem before I presented the Game in it

I am studying the Sudoku Game.

Yes, I know Chris has left a very nice game in Code Library but qbguy presented a game at QB64.net that I liked very much and wanted to figure out why!

From the beginning I liked the way qbguy hid his cells. Chris version is random and so games could get multiple solutions ie you can never solve logically all the cells, at some point you have to plug-in cell values in order to finish a puzzle. Of course some plug-ins are better than others and that is part of the Game too... But qbguys code had same problem, the hiding cells method creates multiple solutions. But qbguys dispersion of blank cells seems better, more evenly spread out... still working on this quality of the Game.

So last night I finally figured out the main reason why I liked qbguys puzzles so much. They have an underlining pattern that makes them almost trivial to solve ONCE YOU FIGURE OUT THE PATTERN! We won t say how long I ve been playing with the puzzles before I finally figured out the pattern. I had been working on alternate board making algos and discovered my first board maker had the same fatal flaw of the underlining pattern that makes solving the games trivial (once you figure out the pattern).

Here is the code for that first board maker study, so maybe you can see the problem of making a game trivial to solve:

*' Make Sudoku Board.bas SmallBASIC 0.12.11 (B+=MGA) 2018-01-07*

*'I don't know if this offers real variety??*

dim grid(8, 8) *'global access*

*'test grids have solutions for Sudoku Game*

while 1

makeGrid

showGrid

? : ? "Grid solve-able ? answer: ";solved()

input "Press enter for another, any else quits ";more

if len(more) then end

wend

sub makeGrid

*' create a playable Sudoku grid and then swap rows, columns or cell blocks*

*' any of 1 to 9 digits could end up in any grid(x, y) position*

local rIncrement, col, row, starter, slider

local i, j, swapMode, cellSet, rc1, rc2

local ta *'temp array*

*'to understand the following need diagram *

*'147:258:369*

*'258:369:471*

*'369:471:582*

*'471:582:693*

*'582:693:714*

*'693:714:825*

*'714:825:936*

*'825:936:147*

*'936:147:258*

*'then I accidently discovered increment 1 (above) 2, 4, 5, 7, 8 all work for same starter!*

repeat *'choose from 6 setup boards*

rIncrement = Int(rnd * 8) + 1

until rIncrement <> 3 and rIncrement <> 6

for col = 0 to 8

if starter = 0

starter = 1

elif starter = 7

starter = 2

elif starter = 8

starter = 3

else

starter = starter + 3

fi

slider = starter

for row = 0 to 8

grid(col, row) = slider

slider = slider + rIncrement

if slider > 9 then slider = slider mod 9

next

next

*'potentialy shuffling 9 rows, 9 cols, 3 vertical cell blocks or 3 horizontals*

for i = 0 to 23

swapMode = int(rnd * 24)

cellSet = int(rnd * 3) *' first, second, third*

rc1 = int(rnd * 3) *' 0, 1, 2*

repeat

rc2 = int(rnd * 3) *' to swap with rc1 0, 1, 2 Not = rc1*

until rc2 <> rc1

for slider = 0 to 8 *' reusing a variable*

if swapMode < 9 then *' swap rows*

swap grid(slider, cellSet * 3 + rc1), grid(slider, cellSet * 3 + rc2)

elif swapMode < 18 *' swap columns*

swap grid(cellSet * 3 + rc1, slider), grid(cellSet * 3 + rc2, slider)

elif swapMode < 21 *' swap cell block rows *

for j = 0 to 2

swap grid(slider, rc1*3 + j), grid(slider, rc2*3 + j)

next

elif swapMode < 24 *' swap cell block columns*

for j = 0 to 2

swap grid(rc1*3 + j, slider), grid(rc2*3 + j, slider)

next

fi

next

next

*'for 9! permutations of number substitutes or codes*

*'OK code the numbers st 1 is made another number 1-9, 2... *

dim ta(1 to 9)

for i = 1 to 9 : ta(i) = i : next

for i = 9 to 2 step -1 *'shuffle*

swap ta(i), ta(int(rnd*i) + 1)

next

for col = 0 to 8

for row = 0 to 8

grid(col, row) = ta( grid(col, row))

next

next

end

sub showGrid

local r, c

cls

for r = 0 to 8

for c = 0 to 8

locate int(r/3) + r + 2 , int(c/3) * 2 + (c + 2) * 3, : ? grid(c, r)

next

next

?

end

*' add solved function*

func solved()

local n, col, row, cell, cellrow, cellcol, found

solved = 0 *'n must be found in every column, row and 3x3 cell*

FOR n = 1 TO 9

*'check columns for n*

FOR col = 0 TO 8

found = 0

FOR row = 0 TO 8

IF abs(grid(col, row)) = n THEN found = 1: EXIT FOR

NEXT

IF found = 0 THEN EXIT FUNC

NEXT

*'check rows for n*

FOR row = 0 TO 8

found = 0

FOR col = 0 TO 8

IF abs(grid(col, row)) = n THEN found = 1: EXIT FOR

NEXT

IF found = 0 THEN EXIT FUNC

NEXT

*'check 3x3 cells for n*

FOR cell = 0 TO 8

cellcol = cell MOD 3

cellrow = INT(cell / 3)

found = 0

FOR col = 0 TO 2

FOR row = 0 TO 2

IF abs(grid(cellcol * 3 + col, cellrow * 3 + row)) = n THEN found = 1: EXIT FOR

NEXT

IF found = 1 THEN EXIT FOR

NEXT

IF found = 0 THEN EXIT FUNC

NEXT

NEXT

solved = 1

end

Notice, I have employed a Solved Function so the Game will acknowledge a successful solution, shoot off fireworks (or some other flashy signal of solving the puzzle) and then offer you another game because, of course, you will want to play again, then! I have added that to Chris game but I wanted to study the hiding cell dispersion problem before I presented the Game in it

*'s more perfected state in SmallBASIC.*- Log in to post comments

johnno56 replied on Permalink

## No! A solver you say? No! A

No! A solver you say? No! A pattern? I almost break my brain trying to figure out these things and now you have taken away the pain and struggle... COOL... A job well done. I cannot pretend to know how this beastie works but I'm glad it does!! Well done, sir! Well done!

J

ps: You mentioned 'fireworks', and as such, I was expecting the incorporation of a particle system... Now THAT would have been 'icing on the cake'... Nudge. Nudge. Wink. Wink. lol

MGA replied on Permalink

## Hi Johnno

Hi J,

I hope you understand the code posted above is just code for making and testing a Sudoku Game board, that's it!

And it makes trivially easy to solve Game Boards, it's an example of what I don't want in my top quality SmallBASIC Sudoku Game.

As to fireworks, there is this:

http://qb64.thejoyfulprogrammer.com/showthread.php?tid=1200&pid=5690&rnd...

I don't know if SmallBASIC interprets code fast enough to run a translated version, I am guessing not which is why I hadn't translated it.

It would also help if I could draw on top of an image which I don't think I can in SmallBASIC.

johnno56 replied on Permalink

## Cool fireworks! Reflection as

Cool fireworks! Reflection as well. Impressive. It's very 'busy' and caused a slight flicker running it on qb64 but still impressive. I think even sdlbasic would probably struggle with that one as well... (you will probably guess that I may try and convert it don't you? lol)

There are a few noticeable difficulties: It looks like it's layered. DEST land and DEST 0? Also RANDOMIZE USING is something that I have never come across. Sounds like a challenge to me... Anyway, it's just gone 8am, and it too early for so much brain drain... Ooo... coffee might help...

J

MGA replied on Permalink

## Randomize

Yes! Randomize Using, I forgot about that.

You'd think RND returns... well random numbers but in fact they aren't at all random, they are a repeatable sequences, all you need is different seeds and to start the unfolding from the beginning. Each firework flame was rebuilt from scratch from the seed assigned to the flame, it's color, it's path, it's speed along x and y directions, nothing was saved in arrays but the seed number ...each screen shot!

And each screen shot reads the points in the sky and reflects them in lake along the shore and each screen shot alters alittle or allot the pixels or color in the hugely magnified text.

MGA replied on Permalink

## Make #2 Sudoku Board

These Sudoku Boards are good because they don t repeat 3 sets of digits over and over (in different permutations) throughout the board.

If I am not mistaken, this method might be a little quicker than the one used by Chris because it s not checking to whole board at each round but only for the last number to finish the grid.

A modification of this method might be used for a random solver though I think I d prefer a more systematic approach that does t repeat taking bad paths.

MGA replied on Permalink

## Aha, Hiding cells algo!

It is my conjecture that if you leave a cell in every row and column of a box (3 minimum), you will more likely create a puzzle with one unique solution, not guaranteed just more likely. I have noticed that such a homogeneous distribution is also pleasing to the eye and invites this person to play Sudoku more than a pure random scattering of hidden cells. Such an arrangement also leaves 3 cells in every row and column of the grid for the hardest level with 33% of cells showing clues. It doesnt look so hopelessly impossible.

BTW there are puzzles that show only 18 clues! that supposedly have unique solutions. That is 2 cells per box on average!

I hope to modify this code to target certain number clues for each box as it is also my conjecture that unique solutions will have at least each of the 9 numbers showing somewhere in the puzzle (It may be that they each have to show twice. I will bet the study and science of Sudoku Puzzles is out there somewhere already.)

MGA replied on Permalink

## Sudoku Game update

I hope to get a Solver working so I can check puzzle boards for unique solutions or not.

I also hope to return cell notes to the program to help with advanced level boards.

EDIT: tiny change to delay in main loop after inner wend, delay 300 NOT delay .1

EDIT 2018-01-17: Some more minor edits, better quit from Game Level selection, debug code removal and more efficient code in loadCell.

MGA replied on Permalink

## Sudoku Game Solver Editor

I never even tried to code that.

The Solver does not solve everything specially ambiguity between 2 numbers where either would work and the other in the other place. Still haven

't figured out how to make a puzzle with 1 solution.What you can do with mouse, you can also do with keyboard and vice versa.

MGA replied on Permalink

## Number Candidates!

Oh hey! Check this out:

http://qb64.thejoyfulprogrammer.com/showthread.php?tid=1207&pid=5806&rnd...

I can display all the number candidates in the empty cells of puzzle. So now it does't just beep when you guess a bad number, it shows you which numbers currently satisfy Sudoku rules (doesn't mean they are right if you guessed wrong in another cell).