Sudoku Code [was: python code I'm trying to copy over to LC]

Want to move your code and projects to LiveCode but don't know where to start?

Moderators: FourthWorld, heatherlaine, Klaus, robinmiller

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Python code I'm trying to copy over to LC

Post by Thierry » Wed Feb 12, 2020 8:09 am

Opaquer wrote: That is an impressively fast Sudoku Solver! I'm definitely going to have to try convert this to LC and see how it compares, but I think this is the next step to making the perfect app!
Hi Opaquer,

I believe you choose the right track,
but be ready for some slowdown.
Anyway will be much better than the previous one.

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Python code I'm trying to copy over to LC

Post by Opaquer » Wed Feb 12, 2020 8:38 am

Absolutely - even if it's 20 times slower, it can still solve the impossible one in a second rather than 7! Now I've just got to get my head around the theory of it and see if I can make it work with sudoku variants rather than just normal ones!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Python code I'm trying to copy over to LC

Post by Thierry » Thu Feb 13, 2020 3:55 pm

Hi,

Here is a tiny stack containing 3 different sudoku solvers.
It's more or less a recap of our sudoku's discoveries from
Opaquer, Hermann and myself.

sunnYscreenshot.png

All the scripts are in the card.
I've tried to organized the code so
it's easy to follow and to compare the 3 solutions.


Enjoy.

Thierry
Attachments
sudoku_hh_tdz.livecode.zip
(6.31 KiB) Downloaded 331 times
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Python code I'm trying to copy over to LC

Post by [-hh] » Thu Feb 13, 2020 3:57 pm

Here are some more sudokus for testing, see https://attractivechaos.github.io/plb/kudoku.html .
"Hardest 20"

Code: Select all

000000000000003085001020000000507000004000100090000000500000073002010000000040009
000000012000000003002300400001800005060070800000009000008500000900040500470006000
020050700400100006800003000200008003040020500000600010002090000090000005704000900
000000003001005600090040070000009050700000008050402000080020090003500100600000000
120300004350000100004000000005400200600070000000008090003100500000009070000060008
100000002090400050006000700050903000000070000000850040700000600030009080002000001
000000039000001005003050800008090006070002000100400000009080050020000600400700000
120300000400000300003050000004200500000080009060005070001500200000090060000007008
003006080000100200000070004009008060030040001070200000300005000005000600980000050
100000009006700020080000400000075030005002000060300000090000800600040001002500060
009000400070300020800060007100800006000010070000056000300005001040000090002000700
000090050010000030002300700004500070800000200000006400090010000080060000005400007
400030000000600800000000001000050090080000600070200000000102700503000040900000000
708000300000201000500000000040000026300080000000100090090600004000070500000000000
307040000000000091800000000400000700000160000000250000000000380090000500020600000
000000008003000400090020060000079000000061200060502070008000500010000020405000003
000000010400000000020000000000050407008000300001090000300400200050100000000806000
000000012000035000000600070700000300000400800100000000000120000080000040050000600
100000002090400050006000700050304000000060000000058040002000600030009080700000001
000001020300040500000600007002000001080090030400000800500002000090030400006700000
and "Topn87"

Code: Select all

400030000000600800000000001000050090080000600070200000000102700503000040900000000
708000300000201000500000000040000026300080000000100090090600004000070500000000000
307040000000000091800000000400000700000160000000250000000000380090000500020600000
708000300000601000500000000040000026300080000000100090090200004000070500000000000
500700600003800000000000200620400000000000091700000000000035080400000100000090000
400700600003800000000000200620500000000000091700000000000043080500000100000090000
040010200000009070010000000000430600800000050000200000705008000000600300900000000
705000002000401000300000000010600400200050000000000090000370000080000600090000080
080010000005000030000000400000605070890000200000300000200000109006700000000400000
000000410900300000300050000048007000000000062010000000600200005070000800000090000
705000002000401000300000000010600400200050000000000090000370000090000800080000060
809000300000701000500000000070000026300090000000100040060200004000080500000000000
100048000050000900006000300000570200803000000000900000000000041670000000000200000
609000008000701000400000000000060004020000030030000500010500070800090000000000200
805000002000901000300000000060700400200050000000000060000380000010000900040000070
000000410900300000300020000048007000000000052010000000500200006070000800000090000
403000002000601000800000000000500970200030000010000000000840000090000600070000050
001000007000890000000000600260030000000500074900000000000104050830000000000000200
307004020000100800900000000000030090050800000040600000000000501200070000000000600
000000410900300000300050000048007000000000052010000000600200005070000800000090000
403000002000601000800000000000500790200030000010000000000840000090000600070000050
000020040070006000010500000200000080000300700409000000000600103800090000000000500
708000300000601000400000000060000025300080000000100090090500002000070400000000000
805000002000901000300000000060700400200050000000000060000380000040000700010000090
805000002000901000300000000060700400200050000000000060000380000010000700040000090
200040500010000030000000000006000802070309000000100000400050600000700090008000000
000000071020800000000503000709060000000200800100000000030002500600010040000000000
704000002000801000300000000050600100200040000000000090000370000080000600090000050
000040001030600000800000000109005000000000870000200000070000260500094000000000300
805000002000401000300000000060700400200050000000000090000380000010000700090000060
010620000500000043000090000700000080005000004000100000000003600090000200800007000
704000002000801000300000000050600100200040000000000090000370000090000500080000060
000309070800400000100000000200500600030000040000010000500000800000020100000700009
003600000040000080900000700860400000000000105020000000500017000100090000000000020
000000091070030000820000000000105000300000700000900000001600050004020000700000800
080000063000040200000000000010803500700000900000600000209070000000000035400000000
805000002000901000300000000060700400200050000000000060000380000040000600090000070
050409000000600001200000300703000200000500090100000000068000040000080000000070000
300801000050000609000000400500700080040060000000000020200300000000090100007000000
040700060003900000000000057000000030200080000019000570600040000050100000020006084
704000002000801000300000000050600100200040000000000050000370000090000600080000090
500603000020000980000000100010090000000300006700000004000080250400700000000000000
208050000000700040300000000500020900000001000000600000070104060000000302010000000
000903100507000080200000000040000600000050020010000000800070000000600400000300009
000000410600300000300020000049008000000000052010000000500600007080000900000030000
700000480000601000000000020000300605200080000000000000053000001060100000000040700
508000007000901000400000000000050004060000030090000600020300010700080000000000200
200060008074300000000020000620000001000400500800000000000500340000001000000000070
609000008000301000400000000000060004020000030070000500010500070800090000000000200
060050403020100000000000700403000600700500000000200000000080050600040000000000010
507000030000061000108000000620040000000700080000000000010000604300500000000000200
403000002000601000800000000000500970200030000070000000000840000090000600010000050
805000002000401000300000000060700400200050000000000060000380000090000700010000090
000001008090000300200000000500000084070630000000900000104000050000070600000200000
000000410900200000300050000048007000000000062010000000600500003070000800000090000
060020001000300070010000000304900000700000200000000508000058600000000040900000000
700000400020070080003008009000500300060020090001007006000300900030040060009001005
000903100607000080200000000050000400000060020010000000800070000000300500000400009
600100080053000000000000040000800060090000700002400000000070309000020500100000000
403000002000701000900000000000500810200030000080000000000940000070000600060000050
403000002000701000900000000000500180200030000080000000000940000070000600060000050
100460005020000700000090000030708000000000091000200000000300840600000000500000000
403500020000061000700000000000089500000300800200000000000400070090000600010000000
060020001000300070010000000304900000700000200000000508000085600000000040900000000
307004020000100500900000000000030090050800000040600000000000801200070000000000600
401060000300000200000000800150200000600000010000900000020708000000000043070000000
000800030005000700000100000000050900180000000300400000007002006000070500040000010
700000480000601000000000020000300605200080000000000000063000001050100000000040700
480300000000000071020000000705000060000200800000000000001076000300000400000050000
403000002000601000800000000000500790200030000070000000000840000090000600010000050
050070083004000060000050000830600000000900100000000000507000400000302000100000000
000030071500402000000000000200600400038070000000000000070080010600500200000000000
070300060000080500010000000809600400000102000500000000000700032400090000000000000
560020000003000900000000000000070056100000020008400000000308400710000000000900000
090300020000070500010000000708600400000902000500000000000100063400080000000000000
708020000000000091300000000046000000000030700000500000050906000000400010200000800
700030000000050600004000090200000701000908000000400000530000200000100080060000000
003000670500000300040000000600300000080000004000700001200000050000098000000041000
403500020000016000700000000000089500000300800200000000000400070090000600010000000
020300060000070500010000000708600400000902000500000000000100039400080000000000000
000000410900300000300020000048007000000000062010000000500200006070000800000090000
600000705030800000000000000005203080109000000000400000420000000000090100000070600
050108070400300000200000000010700080900000400000000000030000010000040200000050600
000600902308700000400000000000095001700000080000000000020060500000400030010000000
805000002000401000300000000060700400200050000000000060000380000010000900090000070
000603700051000000000002000000010054600700000000000080140580000300000200000000000
001000008000900200000003000000015040060000700300000000000040085720600000900000000
shiftLock happens

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Python code I'm trying to copy over to LC

Post by Opaquer » Tue Feb 18, 2020 10:21 am

Hey Hermann

Thank you again for providing the code - it's been a lifesaver! I managed to incorporate it into my code and it's almost working a treat!

I just had a quick question regarding Javascript - I can get the program to run on my laptop, but when I put it onto my phone, it doesn't actually seem like it does anything. I have an answer dialogue that I've set up to go off once it gets a solved sudoku, which works on my laptop, but on my phone, it just acts as if I don't even have the code there? Would you happen to know if there's any differences with Javascript for Android compared to computer, or if there's a setting I'm missing when testing it?

Look forward to hearing from you soon :)

Michael

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Python code I'm trying to copy over to LC

Post by [-hh] » Tue Feb 18, 2020 12:16 pm

This has nothing to do with javascript: Alerts on mobile are different from alerts on desktop.
To have a unique code for the platforms you could use (instead of "answer ..."):

Code: Select all

put tLevel & "-Puzzle solved." into tA
do "alert('" &tA& "');" in widget "browser"
This javascript dialog works also with a hidden browser widget.

p.s. To make this thread better searchable for others you could change the title of the thread in your first post to (for example):
Sudoku code [was: Python code I'm trying to copy over to LC]
shiftLock happens

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Sudoku Code [was: python code I'm trying to copy over to LC]

Post by Opaquer » Tue Feb 18, 2020 1:00 pm

Hey Hermann - post title changed :)

That's really handy to know for the alerts - I just assumed answer was answer!

I did some digging around though and something weird is happening - I decided to try changing the text of a field when it gets to a certain point in the code to see where exactly it's failing. I have this code:

Code: Select all

   do "var g=" & sudoku4JS & ";var r=solve_sudoku(g);liveCode.JSresult(g," & T0 & ");" in widget "Browser"
Which, if I've understood this right, runs the javascript code at the beginning and then runs the JSresult command in LC. I've put the changing text for my field in my JSresult command, as so:

Code: Select all

command JSresult gridR,T0
   set the text of field "TestLabel" to "HELLO!"
   DrawSolution gridR
end JSresult
(I just got rid of the code with the T0, but it shouldn't make a difference if it's not being used yet, right?)

On my computer, after pressing my solve button, TestLabel's text changes to HELLO!, but on mobile it doesn't! That said, I've put another "set the text" thing just before the first bit of code (and just after for testing purposes), and *both* work on my mobile! It gets to that stage, and for some reason just doesn't get to the JSresult bit at all?

I feel like I'm staring into a wall and the answer is super simple, but I just can't see it :(

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Sudoku Code [was: python code I'm trying to copy over to LC]

Post by [-hh] » Tue Feb 18, 2020 5:42 pm

Your code

Code: Select all

do "var g=" & sudoku4JS & ";var r=solve_sudoku(g);liveCode.JSresult(g," & T0 & ");" in widget "Browser"
fails, if T0 is undefined. Just write

Code: Select all

liveCode.JSresult(g," & 0 & ");
instead or enclose T0 with single quotes

Code: Select all

liveCode.JSresult(g,'" & T0 & "');
A possible problem with mobile is generally, that its CPU is too slow.
Because JavaScript is multi-threaded, it then possibly comes to the callback before the solution is ready.

But you could try this variant:

In the htmltext for the browser widget

1. change the line "function solve_sudoku(grid)" to

Code: Select all

function solve_sudoku(grid,m1)
2. insert right before "return solutions.length; "

Code: Select all

liveCode.JSresult(grid,m1);
The callback to LC is now part of the solve_sudoku function. Then call the solution from LC with

Code: Select all

do "var g=" & sudoku4JS & ";var r=solve_sudoku(g," &m1& ");" in widget "Browser"
If that doesn't help, then there remains (more complicated) to write the code with JS-promises...
[Edit: Corrected an error above in 2.]
shiftLock happens

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Sudoku Code [was: python code I'm trying to copy over to LC]

Post by Opaquer » Wed Feb 19, 2020 1:49 pm

Hey Hermann

So, I was struggling to get it to work, so I decided to get rid of T0, because in my app I won't care how fast it solves sudokus. Unfortunately this javascript thing is messing with my mind (which is a problem because my next step once this worked was to edit the javascript to make it do things I want it to do :P )

Basically, in my htmlText for the browser, I have this code for solve_sudoku:

Code: Select all

function solve_sudoku(grid) {
var mat = []; var rinfo = [];
for (var i = 0; i < 9; i++) { for (var j = 0; j < 9; j++) {
var g = grid[i][j] - 1; if (g >= 0) { var row = new Array(324);
row[i*9+j] = 1; row[9*9+i*9+g] = 1; row[9*9*2+j*9+g] = 1;
row[9*9*3+(Math.floor(i/3)*3+Math.floor(j/3))*9+g] = 1;
mat.push(row); rinfo.push({'row': i, 'col': j, 'n': g+1});
} else { for (var n = 0; n < 9; n++) { var row = new Array(324);
row[i*9+j] = 1; row[9*9+i*9+n] = 1; row[9*9*2+j*9+n] = 1;
row[9*9*3+(Math.floor(i/3)*3+Math.floor(j/3))*9+n] = 1;
mat.push(row); rinfo.push({'row': i, 'col': j, 'n': n+1});}}}}
var solutions = dlx_solve(mat, 0, 2);
if (solutions.length > 0) { var r = solutions[0];
for (var i = 0; i < r.length; i++) {
grid[rinfo[r[i]]['row']][rinfo[r[i]]['col']] = rinfo[r[i]]['n']; }
liveCode.JSresult(grid);
return solutions.length;} return 0;
}
I then have this code in my button script:

Code: Select all

do "var g=" & sudoku4JS & ";var r=solve_sudoku(g);" in widget "Browser"
And finally I have my JSresult in my card script:

command JSresult gridR
set the text of field "TestLabel" to "HELLO!" -- just using this as a test to see if it just doesn't get to this stage on my phone or if it does but doesn't go to the next stage (turns out it's the former)
DrawSolution gridR
end JSresult

As far as I can tell (with my 0 experience in javascript!) this should work - perhaps then as you said there may be a deeper problem in it rather than the javascript itself? Is it a possibility there's some part of it that can run on computer but not on Android?

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Sudoku Code [was: python code I'm trying to copy over to LC]

Post by [-hh] » Wed Feb 19, 2020 3:31 pm

Whatever else you changed in the javascript, what I can see here is correct and it doesn't use special new features of javascript.

You could check again:

1. At what point do you set the javascriptHandlers and the htmltext of the widget?
This should be done on openstack. Mobile is slow and needs some time for that.
2. You could use more test-specifically the callback

Code: Select all

on JSresult gridR
   put gridR[0][0] &": "& word 5 of the internet date into fld "TestLabel"
   DrawSolution gridR
end JSresult
3. Runs the stack without problems in the IDE on your desktop machine?
Try to use LC 9.0.5 for the test and standalone building (the most stable LC 9 version, TMHO).

Limitations of the browser widget on Android:
As the browser widget uses a native browser object on Android, there may arise some limitations from an older Android version.
I use mobile as less as possible, but you could try to ask the specialized Android-LiveCoders in subforum "Android Deployment".
shiftLock happens

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Sudoku Code [was: python code I'm trying to copy over to LC]

Post by Opaquer » Thu Feb 20, 2020 1:00 am

Hey Hermann.

Thanks again for the feedback. I put the setup into OpenStack this time and gave it a shot - still no luck :(. I also changed my JSResult code to what you had, and same thing happened - works on computer but not my phone. I've got LC 9.6 dp2 - I'll give 9.0.5 a shot tonight, but in the meantime I've also posted on the Android Deployment subforum.

Here's to hoping!

Thanks again for all your help with everything!

Opaquer

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Sudoku Code [was: python code I'm trying to copy over to LC]

Post by Thierry » Fri Feb 21, 2020 8:57 am

Hi sudoku masters,

Out of curiosity, I wanted to benchmark the same algorithm
in pure LC script versus Lua script...

For that, I've updated my Lua external to be 64 bits
plus running the latest Lua engine (5.3)

Code: Select all

function check(  N, R, C)
   for i = 1 , 9 do
      if puzzle[ R][ i] == N  then return false end
      if puzzle[ i][ C] == N  then return false end
   end
   for i = 0 , 8 do
      if puzzle[3*((R-1)//3)+(i//3)+1][3*((C-1)//3)+(i%3)+1] == N then
      	return false
      end
   end
   return true
end

function candidate( N )
   local R, C
   if N >= 81 then return true end
   R = 1 + N // 9
   C = 1 + N % 9
   if puzzle[R][C] ~= 0 then return candidate( N + 1) end
   for Z=1,9 do
      if check( Z, R, C) then
         puzzle[R][C] = Z
         if candidate( N + 1) then return true end
      end
   end
   puzzle[R][C] = 0
   return false
end
The result is 10 times faster with Lua.

If I find the time, I'll code the dancing links algorithm ( the one in JS from Hermann)
and will compare JS and Lua speed.

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply

Return to “Converting to LiveCode”