dave
unread,Mar 14, 2011, 3:50:36 PM3/14/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to iLuaBox
Here is a program that I wrote using some of the commands from the new
GUI Toolbox. I'm not really that familiar with Lua programming, so
there are probably better ways to do some of what I did. It's a Sudoku
solver. You select a number from the bottom row of numbers and then
place that number in the puzzle area. Once you place all the starting
numbers in the puzzle area, press the Set button. After the Set button
is pressed, press the Solve button to start the program working on a
solution. When a solution is found, pressing the Solve button again
will have the program find the next solution if there is one. This is
a brute force method so some solutions could take time. I added an
iteration display to show the number of tries that are taking place.
That display slows the program down somewhat. Press the Close button
to exit the program. One thing I noticed, when the program is trying
to find a solution the program can't be stopped unless you stop Lua
itself. Tom, what can be added to the Solve function so that the
program can be stopped at any time without slowing it down that much.
I haven't debugged the program very much, so you may find things that
don't work as anticipated. If anyone has other example of some of the
GUI Toolbox commands, I'd love to see them. It's easier to code when
you have examples to look at.
-- sudoku.lua
tab1 = {}
tab2 = {}
tab3 = {}
set = 0
next = 0
count = 0
selected = 0
iter = 0
ilua.cls(0)
dofile ("gui.lua")
function Create81()
ct = 0
for y = 1,9 do
for z = 1,9 do
ct = ct + 1
tab1[ct] = newButton(32*z-20,32*y+80)
tab1[ct].controlEventTouchUpInside = Button81
tab1[ct].titleColor = gui.redColor
vw:add(tab1[ct])
end
end
end
function Create10()
for z = 1,10 do
tab2[z] = newButton(32*z-20,420)
tab2[z].controlEventTouchUpInside = Button10
tab2[z].titleColor = gui.grayColor
tab2[z].title = z
vw:add(tab2[z])
end
tab2[10].title = ""
end
function DrawLines()
l1=newTextField(10,110,290,2)
vw:add(l1)
l2=newTextField(10,206,290,2)
vw:add(l2)
l3=newTextField(10,301,290,2)
vw:add(l3)
l4=newTextField(10,397,290,2)
vw:add(l4)
l5=newTextField(10,110,2,289)
vw:add(l5)
l6=newTextField(106,110,2,289)
vw:add(l6)
l7=newTextField(202,110,2,289)
vw:add(l7)
l8=newTextField(298,110,2,289)
vw:add(l8)
end
function Set()
local pos
for pos = 1,81 do
tab3[pos] = 0
if tab1[pos].title ~= "" then
tab3[pos] = -1
end
end
set = 1
Button10(0) -- clear background color of selected numbers
end
function Solve()
local start = 1
local val = 1
if set == 0 then
gui.alertView("Starting values not set","Enter values then press
Set",nil,{"OK"})
return(0)
end
if count > 0 then
start = 81
val = -1
end
while start < 82 do
iter = iter + 1
s2.text = string.format("Iteration %d",iter)
if start < 1 then
s1.text = "Program complete"
return
end
if tab3[start] ~= -1 then
tab3[start] = tab3[start] + 1
if tab3[start] > 9 then
tab3[start] = 0
tab1[start].title = ""
val = -1
start = start + val
else
val = 1
selected = tab3[start]
if CheckDups(start) ~= 1 then
tab1[start].titleColor = gui.greenColor
tab1[start].title = selected
start = start + val
end
end
else
start = start + val
end
end
count = count + 1
s1.text = string.format("Solution %d", count)
end
function Dups()
gui.alertView("Duplicate in Row, Column, or Box","Make another
selection",nil,{"OK"})
end
function CheckDups(a)
if CheckRow(a) == 1 or CheckCol(a) == 1 or CheckBox(a) ==1 then
return(1)
else
return(0)
end
end
function CheckRow(a)
local start, z
start = math.floor((a-1) / 9) * 9 + 1
for z = start, start + 8 do
if tonumber(tab1[z].title) == selected then
return(1)
end
end
return(0)
end
function CheckCol(a)
local start, z
start = math.fmod(a-1,9)+1
for z = 1, 9 do
if tonumber(tab1[start].title) == selected then
return(1)
end
start = start + 9
end
return(0)
end
function CheckBox(a)
local w1, w2, w3, x, z
w1 = a - 1
w2 = math.floor(w1 / 9) * 9
w3 = math.fmod(w1,3) + w2 - math.floor(w2 / 27) * 27
x = a - w3
for z = 1,9 do
if tonumber(tab1[x].title) == selected then
return(1)
end
x = x + 1
if z == 3 or z == 6 then
x = x + 6
end
end
return(0)
end
function Button81(bt)
local a
local x
if selected == 10 then
selected = ""
end
for a = 1,81 do
if bt == tab1[a] then
x = CheckDups(a)
if x == 1 then
Dups()
else
tab1[a].title = selected
end
break
end
end
end
function Button10(bt)
local a
for a = 1,10 do
tab2[a].backgroundColor = gui.whiteColor
if bt == tab2[a] then
selected = a
tab2[a].backgroundColor = gui.redColor
end
end
end
function Close()
vw:destroy()
gui.statusBarHidden = false
collectgarbage()
end
function newButton(xval,yval)
nb = gui.newButton {title = "", frame = {origin = {x = xval, y =
yval}, size = {width = 30, height = 30}}, buttonType =
gui.buttonTypeRoundedRect}
return(nb)
end
function newTextField(xval,yval,xsize,ysize)
b5 = gui.newTextField {frame = {origin = {x = xval, y = yval}, size
= {width = xsize, height = ysize}},backgroundColor = gui.blackColor}
return(b5)
end
-- create view area
vw = gui.newView {frame = {origin = {x = 300, y = 100}, size = {width
= 340, height = 500}}, backgroundColor = gui.whiteColor}
gui._BACKGROUND:add(vw)
-- create close button
b1 = gui.newButton {title = "Close", frame = {origin = {x = 250, y
=460}, size = {width = 60, height = 30}}, buttonType =
gui.buttonTypeRoundedRect}
b1.controlEventTouchUpInside = Close
b1.titleColor = gui.redColor
vw:add(b1)
-- create set button
b2 = gui.newButton {title = "Set", frame = {origin = {x = 10, y =460},
size = {width = 40, height = 30}}, buttonType =
gui.buttonTypeRoundedRect}
b2.controlEventTouchUpInside = Set
b2.titleColor = gui.redColor
vw:add(b2)
-- create solve button
b3 = gui.newButton {title = "Solve", frame = {origin = {x = 120, y
=460}, size = {width = 60, height = 30}}, buttonType =
gui.buttonTypeRoundedRect}
b3.controlEventTouchUpInside = Solve
b3.titleColor = gui.redColor
vw:add(b3)
-- create title area1
s = gui.newTextField {frame = {origin = {x = 10, y = 10}, size =
{width = 290, height = 25}}, backgroundColor = gui.lightGrayColor}
s.textColor = gui.blueColor
s.textAlignment = gui.textAlignmentCenter
s.text = "Sudoku Solver"
s.font = {name="Courier",size=20}
vw:add(s)
-- create title area2
s1 = gui.newTextField {frame = {origin = {x = 10, y = 40}, size =
{width = 290, height = 25}}, backgroundColor = gui.lightGrayColor}
s1.textColor = gui.blueColor
s1.textAlignment = gui.textAlignmentCenter
s1.font = {name="Courier",size=20}
vw:add(s1)
-- create title area3
s2 = gui.newTextField {frame = {origin = {x = 10, y = 70}, size =
{width = 290, height = 25}}, backgroundColor = gui.lightGrayColor}
s2.textColor = gui.blueColor
s2.textAlignment = gui.textAlignmentCenter
s2.font = {name="Courier",size=15}
vw:add(s2)
-- create 81 button grid
Create81()
-- create 10 button row
Create10()
-- draw lines using text fields
DrawLines()
-- end of program