There Has to be a better way to do this...

Hello, I am currently creating and app that will create a table, number it 1-100 and then place a random character next to each number. It should look something like this
Image Hosted by ImageShack.us

As for now here is my code. It will work but I am basically just doing the same process over and over.

  
module(..., package.seeall)  
  
function new()  
 local localGroup = display.newGroup()  
  
 local random = math.random  
 --\> This is how we start every single file or "screen" in our folder, except for main.lua  
 -- and director.lua  
 --\> director.lua is NEVER modified, while only one line in main.lua changes, described in that file  
------------------------------------------------------------------------------  
------------------------------------------------------------------------------  
local background = display.newImage ("blackbackground.png")  
localGroup:insert(background)  
--\> This sets the background  
local textfield = display.newText("", 25, 125, native.systemFontBold, 14)  
localGroup:insert(textfield)  
  
local textfield1 = display.newText("", 50, 125, native.systemFontBold, 14)  
localGroup:insert(textfield1)  
  
local textfield2 = display.newText("", 75, 125, native.systemFontBold, 14)  
localGroup:insert(textfield2)  
  
local textfield3 = display.newText("", 100, 125, native.systemFontBold, 14)  
localGroup:insert(textfield3)  
  
local textfield4 = display.newText("", 125, 125, native.systemFontBold, 14)  
localGroup:insert(textfield4)  
  
local textfield5 = display.newText("", 150, 125, native.systemFontBold, 14)  
localGroup:insert(textfield5)  
  
local textfield6 = display.newText("", 175, 125, native.systemFontBold, 14)  
localGroup:insert(textfield6)  
  
local textfield7 = display.newText("", 200, 125, native.systemFontBold, 14)  
localGroup:insert(textfield7)  
  
local textfield8 = display.newText("", 225, 125, native.systemFontBold, 14)  
localGroup:insert(textfield8)  
  
local textfield9 = display.newText("", 250, 125, native.systemFontBold, 14)  
localGroup:insert(textfield9)  
  
local textfield10 = display.newText("", 275, 125, native.systemFontBold, 14)  
localGroup:insert(textfield10)  
  
local textfield11 = display.newText("", 25, 150, native.systemFontBold, 14)  
localGroup:insert(textfield11)  
  
local textfield12 = display.newText("", 50, 150, native.systemFontBold, 14)  
localGroup:insert(textfield12)  
  
local textfield13 = display.newText("", 75, 150, native.systemFontBold, 14)  
localGroup:insert(textfield13)  
  
local textfield14 = display.newText("", 100, 150, native.systemFontBold, 14)  
localGroup:insert(textfield14)  
  
local textfield15 = display.newText("", 125, 150, native.systemFontBold, 14)  
localGroup:insert(textfield15)  
  
local textfield16 = display.newText("", 150, 150, native.systemFontBold, 14)  
localGroup:insert(textfield16)  
  
local textfield17 = display.newText("", 175, 150, native.systemFontBold, 14)  
localGroup:insert(textfield17)  
  
local textfield18 = display.newText("", 200, 150, native.systemFontBold, 14)  
localGroup:insert(textfield18)  
  
local textfield19 = display.newText("", 225, 150, native.systemFontBold, 14)  
localGroup:insert(textfield19)  
  
local textfield20 = display.newText("", 250, 150, native.systemFontBold, 14)  
localGroup:insert(textfield20)  
  
local textfield21 = display.newText("", 275, 150, native.systemFontBold, 14)  
localGroup:insert(textfield21)  
  
local myArray = {}local options = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L","M", "N", "O" , "P", "Q", "R", "S", "T", "U",  
 "V", "W","X","Y","Z","œ", "©", "¡", "®", "¾", "Â", "µ", "Á", "¥", "‡", "ß" }   
  
  
local backbutton = display.newImage ("return.png")  
backbutton.x = 160  
backbutton.y = 350  
localGroup:insert(backbutton)  
--\> This places our "back" button  
local ranbutton = display.newImage ("random.png")  
ranbutton.x = 160  
ranbutton.y = 100  
localGroup:insert(ranbutton)  
--\> this adds our random button  
  
local function ranChoose(event)  
 if "ended" == event.phase then  
 textfield.text = options[math.random(1, 37)]  
 end  
 end  
  
local function ranChoose1(event)  
 if "ended" == event.phase then  
 textfield1.text = options[math.random(1, 37)]  
 end  
 end  
local function ranChoose2(event)  
 if "ended" == event.phase then  
 textfield2.text = options[math.random(1, 37)]  
 end  
 end  
local function ranChoose3(event)  
 if "ended" == event.phase then  
 textfield3.text = options[math.random(1, 37)]  
 end  
 end  
local function ranChoose4(event)  
 if "ended" == event.phase then  
 textfield4.text = options[math.random(1, 37)]  
 end  
 end  
local function ranChoose5(event)  
 if "ended" == event.phase then  
 textfield5.text = options[math.random(1, 37)]  
 end  
 end  
local function ranChoose6(event)  
 if "ended" == event.phase then  
 textfield6.text = options[math.random(1, 37)]  
 end  
 end  
local function ranChoose7(event)  
 if "ended" == event.phase then  
 textfield7.text = options[math.random(1, 37)]  
 end  
 end  
local function ranChoose8(event)  
 if "ended" == event.phase then  
 textfield8.text = options[math.random(1, 37)]  
 end  
 end  
local function ranChoose9(event)  
 if "ended" == event.phase then  
 textfield9.text = options[math.random(1, 37)]  
 end  
 end  
local function ranChoose10(event)  
 if "ended" == event.phase then  
 textfield10.text = options[math.random(1, 37)]  
 end  
 end  
  
local function ranChoose11(event)  
 if "ended" == event.phase then  
 textfield11.text = options[math.random(1, 37)]  
 end  
 end  
  
local function ranChoose12(event)  
 if "ended" == event.phase then  
 textfield12.text = options[math.random(1, 37)]  
 end  
 end  
  
local function ranChoose13(event)  
 if "ended" == event.phase then  
 textfield13.text = options[math.random(1, 37)]  
 end  
 end  
  
local function ranChoose14(event)  
 if "ended" == event.phase then  
 textfield14.text = options[math.random(1, 37)]  
 end  
 end  
  
 local function ranChoose15(event)  
 if "ended" == event.phase then  
 textfield15.text = options[math.random(1, 37)]  
 end  
 end  
  
local function ranChoose16(event)  
 if "ended" == event.phase then  
 textfield16.text = options[math.random(1, 37)]  
 end  
 end  
  
local function ranChoose17(event)  
 if "ended" == event.phase then  
 textfield17.text = options[math.random(1, 37)]  
 end  
 end  
  
local function ranChoose18(event)  
 if "ended" == event.phase then  
 textfield18.text = options[math.random(1, 37)]  
 end  
 end  
  
 local function ranChoose19(event)  
 if "ended" == event.phase then  
 textfield19.text = options[math.random(1, 37)]  
 end  
 end  
  
local function ranChoose20(event)  
 if "ended" == event.phase then  
 textfield20.text = options[math.random(1, 37)]  
 end  
 end  
  
local function ranChoose21(event)  
 if "ended" == event.phase then  
 textfield21.text = options[math.random(1, 37)]  
 end  
 end  
  
ranbutton:addEventListener("touch", ranChoose)  
ranbutton:addEventListener("touch", ranChoose1)  
ranbutton:addEventListener("touch", ranChoose2)  
ranbutton:addEventListener("touch", ranChoose3)  
ranbutton:addEventListener("touch", ranChoose4)  
ranbutton:addEventListener("touch", ranChoose5)  
ranbutton:addEventListener("touch", ranChoose6)  
ranbutton:addEventListener("touch", ranChoose7)  
ranbutton:addEventListener("touch", ranChoose8)  
ranbutton:addEventListener("touch", ranChoose9)  
ranbutton:addEventListener("touch", ranChoose10)  
ranbutton:addEventListener("touch", ranChoose11)  
ranbutton:addEventListener("touch", ranChoose12)  
ranbutton:addEventListener("touch", ranChoose13)  
ranbutton:addEventListener("touch", ranChoose14)  
ranbutton:addEventListener("touch", ranChoose15)  
ranbutton:addEventListener("touch", ranChoose16)  
ranbutton:addEventListener("touch", ranChoose17)  
ranbutton:addEventListener("touch", ranChoose18)  
ranbutton:addEventListener("touch", ranChoose19)  
ranbutton:addEventListener("touch", ranChoose20)  
ranbutton:addEventListener("touch", ranChoose21)  
   
  
local function pressBack (event)  
if event.phase == "ended" then  
director:changeScene ("menu")  
end  
end  
backbutton:addEventListener ("touch", pressBack)  
--\> This adds the function and listener to the "back" button  
  
------------------------------------------------------------------------------  
------------------------------------------------------------------------------  
 return localGroup  
end  
--\>  
  

as you can see the code is very, very redundant. There must be some way to mach the function loop. Any advice would be helpful. Thankyou in advance [import]uid: 10724 topic_id: 19609 reply_id: 319609[/import]

can’t understand exactly but it can give you quick idea

[lua]local mySymbolTable = {“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”,“i”,“j”,“k”,“l”,“m”,“n”,“o”,“p”,“q”}
print(#mySymbolTable)
local rand = math.random(#mySymbolTable)
local myfavChar = mySymbolTable[rand]

local k = 0
for j=1,10 do
for i=1,10 do
k = k+1
local myNum = display.newText(k,(i-1)*100 + 20,j*80,nil,18)
if k%9 == 0 then
local myText = display.newText(mySymbolTable[rand],(i-1)*100 + 60,j*80,nil,18)
else
local rand1 = math.random(#mySymbolTable)
local myText = display.newText(mySymbolTable[rand1],(i-1)*100 + 60,j*80,nil,18)
end
end
end[/lua]

view it with following config file and in landscape mode

[lua]application =
{
content =
{
width = 768,
height = 1024,
scale = “letterbox”, --scale = “zoomeven”
},

} [/lua]
:slight_smile: [import]uid: 12482 topic_id: 19609 reply_id: 75752[/import]

It appears that you are adding lots of listeners to a single button: ranButton.
If that Back button simply allocates new values to each of the 100, you only need one listener.
A note on ‘setting up a screen’

Since creating the screen is a process of allocating random values, and going back is the same, you should put the ‘create 100 items’ code into a function so that it can be re-used.

To keep things tidy, create a

local allMyStuff = display.newGroup()   

Add the items except the button to allMyStuff in that setup function.
eg

 display.newText( allMyStuff, mySymbolTable[rand]...  

The first line of the function should be display.remove(allMyStuff)
(This will do nothing if allMyStuff is empty, but it will dispose of the existing stuff if it exists)

Then when the screen is first shown, run the function.
If the back button is pressed, call the setup function again.

======================
Aesthetically, I would make the numbers a different colour to the characters: the current mockup screen is ‘difficult’ to look at.

======================

Finally, if there is a chance that you may want to tap an individual item for any reason, you could make these items all buttons sharing one listener, or you could use newText items as currently, but add a listener to the display, and work out which item had been selected based upon a bit of maths

eg: column = math.floor(display.contentWidth/10)
row = math.floor(display.contentHeight/10)
local item = row * 10 + column

–or since in your case the numbers are reversed, probably 100 -(row * 10 + column)

[import]uid: 108660 topic_id: 19609 reply_id: 75768[/import]