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

Turtle 3x3 tunnel

Started by Zukamimozu, 29 August 2012 - 02:57 AM
Zukamimozu #1
Posted 29 August 2012 - 04:57 AM
I was wondering if anyone could help me with this, I have been trying to make a turtle that would dig a 3x3 tunnel for me, I know that there is already the tunnel command that comes with it but that is 2x3 and doesn't really look that well with my house. So I have got the program to run up to a certain part, so after I put in how far I want it sleeps then tells me how far it is going to mine after you put in the desired length then it comes up with an error.


term.clear()
term.setCursorPos(1, 1)
print "Length: ?"
term.setCursorPos(1, 2)
x = read()
sleep(1)
term.clear()
term.setCursorPos(1, 1)
write(" Mining "..x.." blocks far ")
if x > 0 then
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnLeft()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.up()
turtle.digUp()
turtle.turnLeft()
turtle.turnLeft()
turtle.dig()
turtle.up()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.down()
turtle.down()
turtle.turnLeft()
x = x-1
else
end


The Error: mine:10: attempt to compare string with number expected, got string


Any help would be appreciated, thanks :)/>/>
Luanub #2
Posted 29 August 2012 - 05:07 AM
read() captures information as a string, in line 10 you are trying to compare x with a number and x is a string.

capture the input like this..

local x = tonumber(read()) -- it would be good to put local infront of this and make it a local var will save you trouble in the long run
Zukamimozu #3
Posted 29 August 2012 - 05:21 AM
Okay thanks that helped, but I think I did something wrong on line 10 because no matter the input it always mines 1 block far.

So this is my new code.


term.clear()
term.setCursorPos(1, 1)
print "Length: ?"
term.setCursorPos(1, 2)
local x = tonumber(read())
sleep(1)
term.clear()
term.setCursorPos(1, 1)
write(" Mining "..x.." blocks far ")
if x > 0 then
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnLeft()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.up()
turtle.digUp()
turtle.dig()
turtle.turnLeft()
turtle.turnLeft()
turtle.dig()
turtle.up()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.down()
turtle.down()
turtle.turnLeft()
x = x-1
else
end
Luanub #4
Posted 29 August 2012 - 06:06 AM
Your checking to make sure that x = > 0 with the if statement but you never do anything with it after that so the code only runs through one iteration.

Try adding in a for loop to get the code to run x number of times..

if x > 0 then
  for var=1, x do
    turtle.dig()
    turtle.forward()
    turtle.digUp()
    turtle.turnLeft()
    turtle.dig()
    turtle.turnRight()
    turtle.turnRight()
    turtle.dig()
    turtle.up()
    turtle.digUp()
    turtle.dig()
    turtle.turnLeft()
    turtle.turnLeft()
    turtle.dig()
    turtle.up()
    turtle.dig()
    turtle.turnRight()
    turtle.turnRight()
    turtle.dig()
    turtle.down()
    turtle.down()
    turtle.turnLeft()
  end
end

There should be no reason to do x = x - 1 the for loop will do the counting for you.

You also don't need the else before the end.