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

Mazes for fun and challenge

Started by Neekow, 20 February 2017 - 12:44 AM
Neekow #1
Posted 20 February 2017 - 01:44 AM
Well, returning to CC without playing it during a loo[…]oong time is quite hard, but still as fun as before =)

So, i'm trying to do a turtle what can find different objectives in a maze, and send what it already did on a screen.
Rule for mazes:
- paths are 1 wide
- maze is unknown, only the type (to end, to obj, to obj then end)
- If there's a start and an end known, taking the estimated shortest way (discovering wall step by step)
- If there's a start and objective to find (8 RS block on floor in square), going randomly if gone on a wrong way, taking the nearest undiscovered path. trying to find where the square can be. Return to start by path finding.

How will i do that … well:
- taking the estimated shortest way to the objective (as position unknown, going randomly and looking to a logical patern of where it can be)
- doing sends each step to a computer (count how many time it scans, moves etc …)
- will fill and print what it made on the turtle and monitor, turtle centered

And … i looked some site about tables, but didn't understand a bit …

Think something like:
maze = {{ wall, wall, path},
{ wall, wall, path},
{ wall, wall, path}}
But i clearly i've no idea to how i can create it =/ (and so insert values in), if someone can help a bit? =)

________________________________________________________________________



EDIT:
hum … think i just found (funny how i can be stuck on a thing … and when i post to ask help i find something).

so:

table = {}
if type(table[x]) ~= "table" then table[x] = {} end

"x" will be the line what the turtle is testing.

am i right?
Edited on 20 February 2017 - 01:15 AM
Bomb Bloke #2
Posted 20 February 2017 - 10:37 AM
Say you did this:

maze = {{ 1, 2, 3},
{ 4, 5, 6}}

"maze" then points to a tables with two keys in it, each of which also point to tables. Thus maze[1] is the first of these, and maze[2] is the second. If maze[2] points to a table, then maze[2][3] points to the third key in that table; which is the number 6, in this instance.

print( maze[2][3] )  --> 6

And if you wanted to change that particular key, it's the same as any variable assignment:

maze[2][3] = 7

Thing is that you can refer to these keys using variables:

for i = 1, 2 do
  for j = 1, 3 do
    print(maze[i][j])
  end
end

These are well worth a read for more information and other examples:

http://lua-users.org/wiki/TablesTutorial
http://www.computercraft.info/forums2/index.php?/topic/19709-table-help/
Neekow #3
Posted 22 February 2017 - 02:01 PM

function createTable(nb)
  if type(Laby[nb]) ~= "table" then
    Laby[nb] = {}
  end
end


function fillTable()
  turtleSens()
    if TurtleSens == 0 then
	  nbx = TurtlePosX
	  nby = TurtlePosY + 1
    elseif TurtleSens == 1 then
	 nbx = TurtlePosX - 1
	  nby = TurtlePosY
    elseif TurtleSens == 2 then
	 nbx = TurtlePosX
	  nby = TurtlePosY - 1
    elseif TurtleSens == 3 then
	  nbx = TurtlePosX + 1
	  nby = TurtlePosY
    end
  createTable(nby)
    if turtle.detect() == true then
	  Laby[nby][nbx] = Wall
    else
	  Laby[nby][nbx] = Path
    end 
end

Think something like that would work ^^ (i've nothing to try atm).

But yet, i'm quite stuck on "how the program should work" so i'm doing a plan … ^^
Neekow #4
Posted 27 February 2017 - 01:59 AM
Beside opening a new thread, do a double post ^^ (does editing a post mark it as unread?)
please note that i cant try in game at the moment =(

———–


Well, i'm looking about runing a program with another program. I remember that i used shell.run() But completly forgotten how it's works. And if i'm not wrong, i'll have to resolve/set the path before run.

How do i do that? =/
my expectation for /root/maze (where is the main program)/parts (where are the other parts):

shell.setPath("parts/")
shell.run("part_2", arg1, arg2, arg3)

and … how do i recover args in the second program? ^^'
And final question about shells. If i end it, do i return on the program what created it, if yes, how to return args?

———–


about tables beside do

	  Laby = {}
	  Laby[0] = {}
	  Laby[0][0] = {}
	  Laby[0][0][carte] = {}
	  Laby[0][0][pFind] = {}
do:
 Laby[0][0] = { { carte = {}, pFind = {} } }
works?

———–


Is there a way to have an event on unloading? Like: if turtle will unload do a last function before shuting down.


———–


That all for now … will probaby return with a huge amount of question when i'll can try what i made ^^
Bomb Bloke #5
Posted 28 February 2017 - 09:08 AM
Well, i'm looking about runing a program with another program. I remember that i used shell.run() But completly forgotten how it's works. And if i'm not wrong, i'll have to resolve/set the path before run.
How do i do that? =/

With shell.resolve(). Eg:

shell.run( shell.resolve( "scriptInTheCurrentDirectory" ) )

and … how do i recover args in the second program? ^^'

They're treated as varargs. Often it's easiest to dump them into a table:

local arg = {...}  -- arg[1] = arg1, arg[2] = arg2, arg[3] = arg3, etc

I'm uncertain as to whether shell.run() will allow you to pass anything other than strings. It's built for running scripts as if they were being started via the command line. Easy enough for you to test for yourself.

And final question about shells. If i end it, do i return on the program what created it, if yes, how to return args?

When a script started by shell.run() finishes, execution in the first script continues from the point where it left off. Variables cannot be returned via shell.run() specifically; if you need that, then either don't use shell.run(), or save your data to disk and read it back later.

about tables beside do

Laby = {}
Laby[0] = {}
Laby[0][0] = {}
Laby[0][0][carte] = {}
Laby[0][0][pFind] = {}
do:
 Laby[0][0] = { { carte = {}, pFind = {} } }
works?

I think you mean:

Laby = {
  [0] = {
    [0] = {
      ["carte"] = {},
      ["pFind"] = {}
    }
  }
}

… which you can condense to one line if you like:

Laby = {[0] = {[0] = {["carte"] = {}, ["pFind"] = {}}}}

Remember that counting in Lua usually starts at 1, though. Yeah you can force it to go from 0, but you may find it causes unexpected behaviour down the track.

Is there a way to have an event on unloading? Like: if turtle will unload do a last function before shuting down.

There is no way to detect that something is about to shut the turtle down, if that's what you mean. Likewise, you cannot delay anything that would shut the turtle down.