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

whats wrong with my program?

Started by ironwiccan, 17 June 2013 - 10:49 PM
ironwiccan #1
Posted 18 June 2013 - 12:49 AM
I'm trying to make a turtle that patrols a hallway and turns around at the ends, but it stops at the wall…help?

 turtle.refuel(1)
sleep(1)
 while true do
 turtle.detect()
 end
 if false then
  turtle.forward()
  turtle.attack()
 else
  turtle.turnLeft()
  turtle.turnLeft()
 end
Lyqyd #2
Posted 18 June 2013 - 01:08 AM
Um, no. That program literally doesn't do anything. It would refuel, sleep for one second, and then sit there using turtle.detect() forever. Please give us your entire actual code so that we can help fix it.
ironwiccan #3
Posted 18 June 2013 - 02:09 AM
Sorry. I guess I had an older version saved on my phone. Ill post the rest asap
IntrovertedSiko #4
Posted 18 June 2013 - 05:04 PM
I'm just starting out but…. isn't the first "end" closing the "while" loop? :/ so pretty much it'll just keep detecting…

Try this:

function ______
turtle.refuel(1)
sleep(1)
while true do
if turtle.detect() then --If it detects then...

Do
  Stuff
              
   end
end

Also, this will refuel and sleep once, then jump into the circle of whatever. So maybe you don't want a while true loop. Try a repeat or for loop?
:)/>

P.S you can put "if not…." if you want to reverse what you want to do
Kingdaro #5
Posted 18 June 2013 - 08:39 PM
I'm thinking this is what you want:

turtle.refuel(1)
while true do
  if not turtle.detect() then
    turtle.forward()
    turtle.attack()
  else
    turtle.turnLeft()
    turtle.turnLeft()
  end
end

In English readable form:

Refuel the turtle.
Do the following while true is true:
  Do the following if the turtle does not see a block in front:
    Go forward.
    Attack.
  Otherwise:
    Turn around.