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

Turtle Not Turning?

Started by Sivarias, 21 October 2013 - 11:41 AM
Sivarias #1
Posted 21 October 2013 - 01:41 PM
Hey guys!

I've written a basic floor laying code (my friends and I are working on a skyscraper!) and I wanted it to be customizable so that the turtle would either turn left first from where you placed him or right first from where you placed him. As far as I can tell, my if statement checks are right. But the Turtle insists on making his first turn a right turn, no matter what the input is.

My code is as follows,


    --!floor
    --This simply lays down a floor of a specified area Nothing more
    print("I start counting the blocks in front of me!")
    print("How far forward boss?")
    local x=tonumber(read())-1
    print("How far across boss?")
    local y=tonumber(read())
    print("Left or Right boss? (L/R)")
    local dir=read()
    if dir=="L" or "l" then
	 dir="L"
    elseif dir=="R" or "r" then
	 dir="R"
    end
    a=1
    t.forward()
    if not turtle.detectDown() then
	 turtle.select(a)
	 turtle.placeDown()
    end
    if x*y+1<=turtle.getFuelLevel() then
	 for j=1, y do
	  for i=1, x do
	   if turtle.getItemCount(a)==0 then
	    a=a+1
	   end
	    t.forward()
	   if not turtle.detectDown() then
	    turtle.select(a)
	    turtle.placeDown()
	   end
	  end
	  if turtle.getItemCount(a)==0 then
	   a=a+1
	  end
	  if dir=="R" then
	   t.right()
	   t.forward()
	   if not turtle.detectDown() then
	    turtle.select(a)
	    if j~=y then
		 turtle.placeDown()
	    end
	   end
	   t.right()
	   dir="L"
	  elseif dir=="L" then
	   t.left()
	   t.forward()
	   if not turtle.detectDown() then
	    turtle.select(a)
	    if j~=y then
		 turtle.placeDown()
	    end
	   end
	   t.left()
	   dir="R"
	  end
	 end
    else
	 print("Not enough fuel boss! Try again later!")
    end

Lyqyd #2
Posted 21 October 2013 - 01:58 PM
I think you have the direction he turns confused; dir will always be L after that if. The problem is, this conditional:


if dir=="L" or "l" then

Would say this if you wrote it in English: "if dir is equal to "L", or if "l" is neither nil nor false, then". You need to change it to actually test against the variable both times:


if dir == "L" or dir == "l" then

Or simply use string.lower or string.upper to force a specific case:


dir = string.upper(dir)
if dir == "L" then
LBPHacker #3
Posted 21 October 2013 - 02:06 PM

dir = string.upper(dir)
if dir = "L" then
With a proper equality operator ( == ), of course. Just for the record.

[Fixed, thanks. -L]
Sivarias #4
Posted 21 October 2013 - 02:49 PM
Thank you, I did not know that about the or statements. My coding has primary been in matlab and python and with them

w=1 or 2
checked for w=1 and w=2.

Thanks!