GUI Toolbox example program

36 views
Skip to first unread message

dave

unread,
Mar 14, 2011, 3:50:36 PM3/14/11
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



Tom Skwara

unread,
Mar 14, 2011, 6:04:31 PM3/14/11
to ilu...@googlegroups.com
Nice example - I love Sudoku!

Just a quick thought on your desire to 'break'.  You could use a segmented control:

  • Three positions: 'Solve', 'Idle', and 'Set'.
  • Create a switchboard function that calls 'Set' or 'Solve' (no idle function) based on segmentedControl.selectedSegmentIndex'.
  • Set the 'segmentedControl.controlEventValueChanged value to the new switchboard function.
  • In the 'Solve' loop add a check for the  'segmentedControl.selectedSegmentIndex' to be 'Idle', and break if so.
  • In both 'Set' and 'Solve' functions, add some code to set the segmented control back to 'Idle' when complete.


Tom Skwara
MobileApp Systems



dave

unread,
Mar 15, 2011, 3:24:32 PM3/15/11
to iLuaBox
Thanks Tom.

I played around with the segmentedControl commands in a small program
and they turned out to be exactly what I was looking for.

Dave.
> > -- end of program- Hide quoted text -
>
> - Show quoted text -

Eduardo Carrión

unread,
Apr 5, 2011, 7:12:42 PM4/5/11
to ilu...@googlegroups.com
Hi Dave:

What happened To the GUI in the latest release. Will it come back in future releases? What happens to those who paid for it?

Saludos cordiales,

Eduardo Carrión L.

Tom Skwara

unread,
Apr 5, 2011, 8:03:04 PM4/5/11
to ilu...@googlegroups.com
Eduardo-

Unfortunately, Apple recently decided the Device and GUI toolboxes can no longer be offered for sale. There's a more detailed explanation on the support forum:

http://www.mobileappsystems.com/forum/viewtopic.php?f=2&t=24

Essentially, you can still use iLuaBox 1.2, and instructions explain how, or you can request a refund for your purchase(s) - and perhaps try out the new toolboxes.

Sorry for any inconvenience. We are very disappointed with this development, and hope you continue to use iLuaBox.

Tom Skwara
MobileApp Systems


Sent from my iPad

Eduardo Carrión

unread,
Apr 6, 2011, 12:51:20 AM4/6/11
to ilu...@googlegroups.com
Hi Tom:

Too bad, but I am not surprised. apple won't let users have any control or programing power outside their way of doing and controlling things. This almost killed the Macintosh years ago. Looks that the same arrogance is still present. Some day they will pay dearly for this as users will eventually migrate to less closed systems.

Saludos cordiales,

Eduardo Carrión L.

Eduardo Carrión

unread,
May 17, 2011, 9:29:40 PM5/17/11
to ilu...@googlegroups.com
Hi Tom:

After today's update hello.lua does not longer run.

Saludos cordiales,

Eduardo Carrión L.

Eduardo Carrión

unread,
May 17, 2011, 9:31:04 PM5/17/11
to ilu...@googlegroups.com
This is the message I get:

lua: hello.lua:4: attempt to index global 'ilua' (a nil value)
stack traceback:
hello.lua:4: in main chunk
[C]: in function 'dofile'
(command line):1: in main chunk

Saludos cordiales,

Eduardo Carrión L.

Jock Murphy

unread,
May 17, 2011, 10:04:38 PM5/17/11
to ilu...@googlegroups.com
1) You need to go to iPhone settings and select iLuaBox, then turn "reload examples" to ON
2) This google group really isn't active anymore please go to http://www.mobileappsystems.com/forum/

I have nothing to do with the company, it just seems like it might a while since they might get to this, considering they don't use the google group for support any more :)

2011/5/17 Eduardo Carrión <ecarr...@gmail.com>



--
Jock Murphy
http://www.jockmurphy.com
Photographer * Writer * Storyteller

Eduardo Carrion

unread,
May 17, 2011, 10:34:06 PM5/17/11
to ilu...@googlegroups.com
Thank you very much.

Sent from my iPhone

Tom Skwara

unread,
May 17, 2011, 11:46:22 PM5/17/11
to ilu...@googlegroups.com, ilu...@googlegroups.com
iLuaBox 1.3 and later uses a new 'module' mechanism to help keep code a bit more modular.  As such, all libraries outside of Lua's standard libraries must be 'required' before they are available.  The previous version of iLuaBox (including it's 'hello.lua' file) relied on the iLua Toolbox being preloaded, which is no longer the case.  The extra step Jock mentioned makes overwriting example files a deliberate action that can help prevent accidentally overwriting files you may have modified.

Thanks for helping out Jock!

-TS

Sent from my iPad

Eduardo Carrion

unread,
May 17, 2011, 11:51:32 PM5/17/11
to ilu...@googlegroups.com
Thanks

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages