This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Neekow's profile picture

Beginner in Lua and Table (?)

Started by Neekow, 20 February 2013 - 03:56 AM
Neekow #1
Posted 20 February 2013 - 04:56 AM
[Title] Beginner in Lua and Table (?).


NB: I'm french, and my english isn't perfect … hope you will understand me ^^.

Hi everyone, i'm doing a digicode with touch screen, but i need a bit help to clean the code ^^.
Here's a screen of the final result:


and here's the code for that:
Spoiler

local m = peripheral.wrap("top")
m.clear()
local couleurtableau = colors.blue
local couleurchiffre = colors.gray
local couleurBG = colors.black
local couleurcroix = colors.orange
local pin = "2468V"
local code1 = 0
local code2 = 0
local code3 = 0
local code4 = 0



function scanner()
  local scannerFrame = {
  '	   ',
  ' _____ ',
  '| | | |',
  '|_|_|_|',
  '| | | |',
  '|_|_|_|',
  '| | | |',
  '|_|_|_|',
  '| | | |',
  '|_|_|_|',
  ' | | | ',
  '_|_|_|_'}


for i = 1, #scannerFrame do
	 m.setTextColor(couleurtableau)
	 m.setCursorPos(1,i)
	 m.write(scannerFrame[i])
	 m.setTextColor(couleurchiffre)
end

m.setBackgroundColor(couleurBG)
m.setCursorPos(2,3)
m.write("1")
m.setCursorPos(4,3)
m.write("2")
m.setCursorPos(6,3)
m.write("3")
m.setCursorPos(2,5)
m.write("4")
m.setCursorPos(4,5)
m.write("5")
m.setCursorPos(6,5)
m.write("6")
m.setCursorPos(2,7)
m.write("7")
m.setCursorPos(4,7)
m.write("8")
m.setCursorPos(6,7)
m.write("9")
m.setTextColor(colors.lime)
m.setCursorPos(2,9)
m.write("V")
m.setTextColor(couleurchiffre)
m.setCursorPos(4,9)
m.write("0")
m.setTextColor(colors.red)
m.setCursorPos(6,9)
m.write("C")
m.setBackgroundColor(colors.black)

function Pass()
  code = code1..code2..code3..code4..code5
  if pin == code
   then
   print("yeah")
	for i = 1,5 do
	 if i == 1 then m.setTextColor(colors.green) end
	 if i == 2 then m.setTextColor(colors.lime) end
	 if i == 3 then m.setTextColor(colors.green) end
	 if i == 4 then m.setTextColor(colors.lime) end
	 if i == 5 then m.setTextColor(colors.green) end
	m.setCursorPos(1,11)
	m.write("X")
	m.setCursorPos(3,11)
	m.write("X")
	m.setCursorPos(5,11)
	m.write("X")
	m.setCursorPos(7,11)
	m.write("X")
	sleep(0.5)
	end

   else
   print("perdu")
	for i = 1,5 do
	 if i == 1 then m.setTextColor(colors.red) end
	 if i == 2 then m.setTextColor(colors.orange) end
	 if i == 3 then m.setTextColor(colors.red) end
	 if i == 4 then m.setTextColor(colors.orange) end
	 if i == 5 then m.setTextColor(colors.red) end
	m.setCursorPos(1,11)
	m.write("X")
	m.setCursorPos(3,11)
	m.write("X")
	m.setCursorPos(5,11)
	m.write("X")
	m.setCursorPos(7,11)
	m.write("X")
	sleep(0.5)
   end
os.reboot()

  end
end

function checkxy(x, y)
if y == 3 then
   if x == 2 then code = 1
   end
   if x == 4 then code = 2
   end
   if x == 6 then code = 3
   end
end

if y == 5 then
   if x == 2 then code = 4
   end
   if x == 4 then code = 5
   end
   if x == 6 then code = 6
   end
end

if y == 7 then
   if x == 2 then code = 7
   end
   if x == 4 then code = 8
   end
   if x == 6 then code = 9
   end
end

if y == 9 then
   if x == 2 then code = "V"
   end
   if x == 4 then code = 0
   end
   if x == 6 then code = "C"
   end
end
end


m.setTextColor(couleurcroix)
code = 100
while code == 100 do
  e, side, x, y = os.pullEvent("monitor_touch")
  checkxy(x, y)
  if code == "C" then os.reboot() end
  code1 = code
  print("X: "..x.." Y: "..y.." Code: "..code.." Pin: "..code1)
end
m.setCursorPos(1,11)
m.write("X")

code = 100
while code == 100 do
  e, side, x, y = os.pullEvent("monitor_touch")
  checkxy(x, y)
  if code == "C" then os.reboot() end
  code2 = code
  print("X: "..x.." Y: "..y.." Code: "..code.." Pin: "..code2)
end
m.setCursorPos(3,11)
m.write("X")

code = 100
while code == 100 do
  e, side, x, y = os.pullEvent("monitor_touch")
  checkxy(x, y)
  if code == "C" then os.reboot() end
  code3 = code
  print("X: "..x.." Y: "..y.." Code: "..code.." Pin: "..code3)
end
m.setCursorPos(5,11)
m.write("X")

code = 100
while code == 100 do
  e, side, x, y = os.pullEvent("monitor_touch")
  checkxy(x, y)
  if code == "C" then os.reboot() end
  code4 = code
  print("X: "..x.." Y: "..y.." Code: "..code.." Pin: "..code4)
end
m.setCursorPos(7,11)
m.write("X")

code = 100
while code == 100 do
  e, side, x, y = os.pullEvent("monitor_touch")
  checkxy(x, y)
  if code == "C" then os.reboot() end
  code5 = code
  if code5 == "V" then Pass() end
  print("X: "..x.." Y: "..y.." Code: "..code.." Pin: "..code5)
end

Pass()

print(code1.." "..code2.." "..code3.." "..code4.." "..code5)
code = code1..code2..code3..code4..code5
print(code)


But, as you can see, the code is really … disgusting? It work pretty fine but i wanna do better, if ever i wanna modify it later.

So i began an other code, trying to clean up this thing by using tables
Spoiler
			  ----=== Initialisation ===----
m = peripheral.wrap("top")
m.clear()
m.setTextColor(colors.white)
m.setBackgroundColor(colors.black)
m.setTextScale(1)
m.setCursorPos(1,1)
local button = {}

			 ----=== Scanner ===----
function scanner()
  local scannerFrame = {
  '	   ',
  ' _____ ',
  '| | | |',
  '|_|_|_|',
  '| | | |',
  '|_|_|_|',
  '| | | |',
  '|_|_|_|',
  '| | | |',
  '|_|_|_|',
  ' | | | ',
  '_|_|_|_'}


  for i = 1, #scannerFrame do
	   m.setCursorPos(1,i)
	   m.write(scannerFrame[i])
  end
end



			  ----=== Numbers ===----

function setTable(name, func, Posx, Posy)
   button[name]= {}
   button[name]["func"]= func
   button[name]["Posx"]= Posx
   button[name]["Posy"]= Posy
end

function fillTable()
  setTable("1", detect, 2, 3)
  setTable("2", detect, 4, 3)
  setTable("3", detect, 6, 3)
  setTable("4", detect, 2, 5)
  setTable("5", detect, 4, 5)
  setTable("6", detect, 6, 5)
  setTable("7", detect, 2, 7)
  setTable("8", detect, 4, 7)
  setTable("9", detect, 6, 7)
  setTable("V", detect, 2, 9)
  setTable("0", detect, 4, 9)
  setTable("C", detect, 6, 9)
end

function fillNumbers()
  for name, data in pairs(button) do
	local x = bData["Posx"]
	local y = bData["Posy"]
	 m.setCursorPos(x,y)
	 m.write(name)
	 end
	end
  end
end


			  ----=== Prog ===----

scanner()
fillNumbers()

But this time, no numbers appear =( (and haven't any error on screen …). This is why i need some help to learn about table (if its table ^^), so … please someone, tell me where i'm wrong =$
Lyqyd #2
Posted 20 February 2013 - 05:48 AM
Split into new topic.

You never called fillTable, and you're using `for name, data in` and then referencing bData. There may be other issues, but those two would definitely prevent writing anything.
Jan #3
Posted 20 February 2013 - 06:28 AM
NB: I'm french, and my english isn't perfect … hope you will understand me ^^.
(snip)

   print("perdu")

No problem, I've seen Mr Beans Holiday, so I know what perdu means ;)/>

I will take a look at your code.
Jan #4
Posted 20 February 2013 - 07:13 AM
Here is an edited version.
see the comments to see what I changed

Spoiler

local m = peripheral.wrap("top")
m.clear()
local couleurtableau = colors.blue
local couleurchiffre = colors.gray
local couleurBG = colors.black
local couleurcroix = colors.orange
local pin = "2468V"
local endcode = ""

  local scannerFrame = {
  '	    ',
  ' _____ ',
  '| | | |',
  '|_|_|_|',
  '| | | |',
  '|_|_|_|',
  '| | | |',
  '|_|_|_|',
  '| | | |',
  '|_|_|_|',
  ' | | | ',
  '_|_|_|_'}

for i = 1, #scannerFrame do
		 m.setTextColor(couleurtableau)
		 m.setCursorPos(1,i)
		 m.write(scannerFrame[i])
		 m.setTextColor(couleurchiffre)
end

-- I made a table of the buttons
local buttons={
{"1",2,3,couleurBG},
{"2",4,3,couleurBG},
{"3",6,3,couleurBG},
{"4",2,5,couleurBG},
{"5",4,5,couleurBG},
{"6",6,5,couleurBG},
{"7",2,7,couleurBG},
{"8",4,7,couleurBG},
{"9",6,7,couleurBG},
{"V",2,9,colors.lime},
{"0",4,9,couleurBG},
{"C",6,9,colors.red}
}
-- This peice of code loops through the buttons
m.setBackgroundColor(couleurBG)
for n,button in ipairs(buttons) do
m.setCursorPos(button[2],button[3])
m.setBackgroundColor(button[4])
m.write(button[1])
end
m.setBackgroundColor(colors.black)
function Pass()
  if pin == endcode
   then
   print("yeah")
	    for i = 1,5 do
		 if i == 1 then m.setTextColor(colors.green) end
		 if i == 2 then m.setTextColor(colors.lime) end
		 if i == 3 then m.setTextColor(colors.green) end
		 if i == 4 then m.setTextColor(colors.lime) end
		 if i == 5 then m.setTextColor(colors.green) end
	    m.setCursorPos(1,11)
	    m.write("X")
	    m.setCursorPos(3,11)
	    m.write("X")
	    m.setCursorPos(5,11)
	    m.write("X")
	    m.setCursorPos(7,11)
	    m.write("X")
	    sleep(0.5)
	    end
   else
   print("perdu")
	    for i = 1,5 do
		 if i == 1 then m.setTextColor(colors.red) end
		 if i == 2 then m.setTextColor(colors.orange) end
		 if i == 3 then m.setTextColor(colors.red) end
		 if i == 4 then m.setTextColor(colors.orange) end
		 if i == 5 then m.setTextColor(colors.red) end
	    m.setCursorPos(1,11)
	    m.write("X")
	    m.setCursorPos(3,11)
	    m.write("X")
	    m.setCursorPos(5,11)
	    m.write("X")
	    m.setCursorPos(7,11)
	    m.write("X")
	    sleep(0.5)
	    end
	    os.reboot()
   end
end

-- This code loops through all the buttons, and checks if the x and y match
function checkxy(x, y)
for n,button in ipairs(buttons) do
  if (x==button[2]) and (y==button[3]) then
   code = button[1]
  end
end
end
m.setTextColor(couleurcroix)

-- I made a loop here too
for pos=0,4 do
code = 100
while code == 100 do
   e, side, x, y = os.pullEvent("monitor_touch")
   checkxy(x, y)
   if code == "C" then os.reboot() end
   endcode = endcode .. code
   -- I changed code1,code2,code3 etc, by a the variable 'endcode'
   if code == "V" then Pass() end
   print("X: "..x.." Y: "..y.." Code: "..code)
end
m.setCursorPos(1+pos*2,11)
-- The posttion of the X is defined by 1+pos*2
m.write("X")
end

-- Inserted a clear
m.clear()
Maybe you can make the Pass() function smaller yourself?
Tip: use this to check if a number is even or odd:

if (number%2)==0 then
-- even
else
-- odd
end
Doyle3694 #5
Posted 21 February 2013 - 06:03 AM
This piece of code:

for i = 1,5 do
   if i == 1 then m.setTextColor(colors.green) end
   if i == 2 then m.setTextColor(colors.lime) end
   if i == 3 then m.setTextColor(colors.green) end
   if i == 4 then m.setTextColor(colors.lime) end
   if i == 5 then m.setTextColor(colors.green) end
   m.setCursorPos(1,11)
   m.write("X")
   m.setCursorPos(3,11)
   m.write("X")
   m.setCursorPos(5,11)
   m.write("X")
   m.setCursorPos(7,11)
   m.write("X")
   sleep(0.5)
end
Can be changed too:

for i = 1,5 do
   if i%2 == 1 then m.setTextColor(colors.green)
   elseif i%2 == 0 then m.setTextColor(colors.lime) end
   for i = 1,7,2 do
	  m.setCursorPos(i,11)
	  m.write("X")
	  sleep(0.5)
   end
end
remiX #6
Posted 21 February 2013 - 07:30 AM
This piece of code:

--snip
Can be changed too:

for i = 1,5 do
   if i%2 == 1 then m.setTextColor(colors.green)
   elseif i%2 == 0 then m.setTextColor(colors.lime) end
   for i = 1,7,2 do
	  m.setCursorPos(i,11)
	  m.write("X")
	  sleep(0.5)
   end
end

And that can be changed to

for i = 1,5 do
	m.setTextColor( i%2 == 1 and colors.green or colors.lime )
	for i = 1,7,2 do
		m.setCursorPos(i,11)
		m.write("X")
		sleep(0.5)
	end
end

^^
Neekow #7
Posted 28 February 2013 - 01:10 PM
Thanks for help, now i think i understood the table and i'll rewrite (at least try) it. i'll post when it's done ;)/>