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

[Help] Repeat loop

Started by sjele, 07 August 2012 - 06:57 AM
sjele #1
Posted 07 August 2012 - 08:57 AM
So i am trying to make a program that digs out a 3 x 3 with a lenght set at startup.But when i set the lenght too 3 or any number it refuses to stop?
Can you see the problem here?


--Vars--
lenght = 0
row1 = lenght
row2 = lenght
row2 = lenght
back = lenght
back1 = lenght
back2 = lenght
zero = 0

--Functions--
function returnTurtle()
  repeat
  turtle.back()
  back = back - 1
  until back == zero
end

function returnTurtle1()
  repeat
  turtle.back()
  back1 = back1 - 1
  until back1 == zero
end

function returnTurtle2()
  repeat
  turtle.back()
  back2 = back2 - 1
  until back2 == zero
end

function clearScreen()
  term.clear()
  term.setCursorPos (1, 1)
end



--Startup interface--
  print ( " 3 x 3 tunnel creator v.1 (beta)" )
  print ( " Program is made by sjele " )
--Startup programing--
  write ( " Lenght of tunnel: ")
  lenght = read()
  sleep(1)
  print ( "Tunnel will be "..lenght.." long" )
  sleep(2)
  clearScreen()


--Mining of row 1 --
  turtle.up()
  repeat
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
  row1 = row1 - 1
  until row1 == zero
  returnTurtle()

--Getting into possision of row 2--
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()

-- Mining of row 2 --
  repeat
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
  row2 = row2 - 1
  until row2 == zero
  returnTurtle1()

-- Getting into posision for row 3 --
   turtle.turnRight()
   turtle.forward()
   turtle.turnLeft()

--Mining row 3 --
  repeat
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
  row3 = row3 - 1
  until row3 == 0
  returnTurtle2()










Luanub #2
Posted 07 August 2012 - 10:03 AM
I see a couple of issues. The first being that you're capturing length of the tunnel that the user wants to create in the var lenght but you do not do anything with that var afterwards. You will need to add some logic into the code for it to keep track of how far forward the turtle has gone, and how far it has gone vs lenght. Also since this is going to be a number it would be best to put a tonumber around it so:

local lenght = tonumber(read())

Also you're using the vars row1, row2 and row3 but you never declared them to start with so they start at a nil value. You then subtract 1 from that value when the steps for that row are complete making it -1. You're repeat loop will never stop because the var will never == 0(which is another problem you have "until row2 == zero" when it should be the number 0 not the word)

You have some () after your until statement for row 3 that you do not need.

And the turtle.forward() in the getting into position for row 3 has an extra space in it.
sjele #3
Posted 07 August 2012 - 10:25 AM
I forgot to copy with the vars. Will fix in main post now. Allso how would i use the "tonumber" thing

Added the vars in the original post
Luanub #4
Posted 07 August 2012 - 11:09 AM
I'm going to have to play with it to see why the repeats are not working correctly. The script looks better and see no obvious issues.

One thing you may want to try while I'm playing around with it is replace the repeats with a for loop. A numerical for loop will run through the code the selected number of iterations. So doing this..

for x=1, 5 do
print(x)
end

Will print the var x 5 times. The var x will start as 1 and increase by 1 each time the loop runs.

For your application try doing something like

for x=1, row1 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
end
returnTurtle()

So it should stop when x == row1

And for the tonumber the line I have in the last post should do it. It coverts the var to a number, useful in this case since the read() output is a string.
Edited on 07 August 2012 - 09:40 AM
sjele #5
Posted 07 August 2012 - 11:29 AM
Time to learn how to use for loops :P/>/>
I'll try it out and see if i make it work, thanks for helping me ;)/>/>


--Vars--
lenght = 0
row1 = lenght
row2 = lenght
row3 = lenght
back = lenght
back1 = lenght
back2 = lenght
zero = 0
--Functions--
function returnTurtle()
for r= 1, back do
  turtle.back()
end
end
function returnTurtle1()
for t = 1, back1 do
  turtle.back()
end
end
function returnTurtle2()
for y=1, back2 do
  turtle.back()
end
end
function clearScreen()
  term.clear()
  term.setCursorPos (1, 1)
end

--Startup interface--
  print ( " 3 x 3 tunnel creator v.1 (beta)" )
  print ( " Program is made by sjele " )
--Startup programing--
  write ( " Lenght of tunnel: ")
  lenght = read()
  sleep(1)
  print ( "Tunnel will be "..lenght.." long" )
  sleep(2)
  clearScreen()

--Mining of row 1 --
  turtle.up()
for x= 1, row1 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
  sleep(.1)
end
  returnTurtle()
--Getting into possision of row 2--
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
-- Mining of row 2 --
for c=1, row2 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
end
  returnTurtle1()
-- Getting into posision for row 3 --
   turtle.turnRight()
   turtle.forward()
   turtle.turnLeft()
--Mining row 3 --
for v=1, row3 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
end
  returnTurtle2()

For some wierd reason right now it calls returnTurtle functions at start
After i added for loops
Tgwizman #6
Posted 07 August 2012 - 12:16 PM
My honest opinion would be to take out the sleeps and add some debugging "prints"/"writes" to help figure out that pesky returnTurtle().
Other than that, the code looks just fine! :P/>/>
Luanub #7
Posted 07 August 2012 - 12:49 PM
Okay I figured it out. Should really be able to use either the repeat or the for loop. It's upto you. The problem was with having all the vars declared at the top for some reason they were keeping the initial value of 0 and not updating to the users input.

I moved the row vars down to where they were just before the loops and moved the back vars into the functions.

I also made the vars local which will help to avoid some potential issues. I recommend always using local vars.

Corrected Code:
Spoiler

--Vars--
local lenght = 0
local zero = 0
--Functions--
function returnTurtle()
local back = lenght
for r= 1, back do
  turtle.back()
end
end
function returnTurtle1()
local back1 = lenght
for t = 1, back1 do
  turtle.back()
end
end
function returnTurtle2()
local back2 = lenght
for y=1, back2 do
  turtle.back()
end
end
function clearScreen()
  term.clear()
  term.setCursorPos (1, 1)
end

--Startup interface--
  print ( " 3 x 3 tunnel creator v.1 (beta)" )
  print ( " Program is made by sjele " )
--Startup programing--
  write ( " Lenght of tunnel: ")
  lenght = tonumber(read())
  sleep(1)
  print ( "Tunnel will be "..lenght.." long" )
  sleep(2)
  clearScreen()

--Mining of row 1 --
  turtle.up()
  local row1 = lenght
for x= 1, row1 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
  sleep(.1)
end
  returnTurtle()
--Getting into possision of row 2--
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
-- Mining of row 2 --
local row2 = lenght
for c=1, row2 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
end
  returnTurtle1()
-- Getting into posision for row 3 --
   turtle.turnRight()
   turtle.forward()
   turtle.turnLeft()
--Mining row 3 --
local row3 = lenght
for v=1, row3 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
end
  returnTurtle2()


Also I decided I would improve it for you and remove the row and back vars all together. Play with this..

Improved Script

--Vars--
local lenght = 0
local zero = 0
--Functions--
function returnTurtle()
for r= 1, lenght do
  turtle.back()
end
end

function clearScreen()
  term.clear()
  term.setCursorPos (1, 1)
end

function digRow()
for x= 1, lenght do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
  sleep(.1)
end
  returnTurtle()
end

function newRow()
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
end

--Startup interface--
  print ( " 3 x 3 tunnel creator v.1 (beta)" )
  print ( " Program is made by sjele " )
--Startup programing--
  write ( " Lenght of tunnel: ")
  lenght = tonumber(read())
  sleep(1)
  print ( "Tunnel will be "..lenght.." long" )
  sleep(2)
  clearScreen()

--Mine Tunnel --
  turtle.up()
  for a=1, 2 do
	digRow()
	newRow()
  end
  digRow()
Edited on 07 August 2012 - 10:56 AM
sjele #8
Posted 07 August 2012 - 01:26 PM
Ohhhh, thanks. Time to study what i messed up and how you fixed it.
Thanks for taking you're time to help me ;)/>/>

Ohh, i see. Next time i am making a turtle script i'll have this open :P/>/>
I learned so much from this.