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

Trouble With Tables... ( SOLVED )

Started by xionous, 02 June 2013 - 02:49 PM
xionous #1
Posted 02 June 2013 - 04:49 PM
OK so what i'm trying to do with this program is to make a maze that has a changeable configuration.

First off here is the code i have so far:


w = peripheral.wrap("right") -- initializes the retherupgrade
m = 5 -- number of available mazes (have to set manually)


--list of available mazes

maze0 = {}
maze1 = {1,2,3,4}
maze2 = {3,4,5,6}
maze3 = {3,4,5,6}
maze4 = {4,5,6,7,8}
maze5 = {5,6,7,8,9}

function random() -- function to choose a random maze
  nr = math.random(1,m) -- chooses a maze between 1 and m
  print("loading maze")
  print(nr)
end

function reset()
  for i = 1,#maze1 do
	signal = maze1[i]
	w.setFreq(signal)
	w.set(true)
	sleep(0,1)
	w.set(false)
  end
end

function set()
  for i = 1,#maze1 do
	signal = maze1[i]
	w.setFreq(signal)
	w.set(true)
	sleep(0,1)
	w.set(false)
print(signal)
  end
end

function alarm() -- makes an alarm go off before the walls change
  rs.setOutput("front", true)
  sleep(4,5)
  rs.setOutput("front", false)
end

function main()
  random()
  set()
  sleep(20)
  alarm()
  reset()
  os.reboot()
end

--start the main function
while true do
  main()
end

now what i cannot figure out is how to change these lines:


for i = 1,#maze1 do
	signal = maze1[i]

to be able to select the tables at random but still be able to output all of the numbers in the table. I have been at this for a few hours and cannot seem to find the information i am looking for.
Lyqyd #2
Posted 03 June 2013 - 12:46 PM
Split into new topic.

You can put your mazes into a table and select them from there randomly.
xionous #3
Posted 03 June 2013 - 03:58 PM
the maze configurations are in several tables, but I want to be able to select the tables at random or if there is another way around it i have no idea what code to use to achieve that. I'm a beginner at LUA and most of my knowledge is obtained from tutorials.
Lyqyd #4
Posted 03 June 2013 - 04:03 PM
I can see that. I'm saying you should put those tables in another table and select randomly from within that table.


local mazes = {
  {5, 7, 1},
  {3, 8, 4},
}

local currentMaze = mazes[math.random(1, #mazes)]

currentMaze would then contain one maze table.
xionous #5
Posted 03 June 2013 - 04:29 PM
Wow that worked perfectly :)/> thanks so much for your help! I wish I knew more about LUA, I'm learning in my free time and I sometimes hit roadblocks I cannot overcome on my own. One day I will understand it better…. hopefully. :P/>