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

More GPS Debugging....

Started by deactivated1712, 05 September 2012 - 06:35 PM
deactivated1712 #1
Posted 05 September 2012 - 08:35 PM
Hi,
I wrote this script to go to some coordinates, it works by you telling it which way it is facing, it will then turn to north, then ask for the destination coordinates. Currently, its asking for lots of "ends", which I got confused about because I am rubbish at making it look nice with spaces.


function look(direction)
  write("Direction?   ")
  curDir = read()
   if curDir == n then
	 print("Facing North")
	 end
   if curDir == e then
	  turtle.turnLeft()
	  print("Facing North")
	  end
   if curDir == s then
	   turtle.turnRight()
	   turtle.turnRight()
	   print("Facing North")
	   end
   if curDir == w then
	  turtle.turnRight()
	  print("Facing North")
	  end
   else
    print("Invalid Entry")
    shell.run("goto")
    end
end
   if direction == "north" then
    end
   if direction == "east" then
   turtle.turnLeft()
   end
   if direction == "south" then
    turtle.turnRight()
    turtle.turnRight()
    end
   if direction == west then
    turtle.turnLeft()
    end
end
end
end
end

end
write("Enter X Coord:  ")
xTarget = read()
write("Enter Y Coord;  ")
yTarget = read()
write("Enter Z Coord:  ")
zTarget = read()
x, y, z = gps.locate(5)
	    while yTarget < y do
			 turtle.down()
	    end
	    while yTarget > y do
			 turtle.up()
	    end
	  if xTarget < x then
		 look("west")
		 while xTarget < x do
		  turtle.forward()
	   end
	  end
	 if xTarget > x then
	   look("east")
	    while xTarget > x do
		   turtle.forward()
		 end
	  end
  if zTarget < z then
  look("north")
   while zTarget < z do
    turtle.forward()
   end
  end
   if zTarget > z then
    look("south")
	  while zTarget > z do
	   turtle.forward()
	  end
   end
end


Your mission, should you chose to accept it is to put in all the "ends" and make it work!

THANKS!

H
Pharap #2
Posted 06 September 2012 - 04:05 AM
Hi,
I wrote this script to go to some coordinates, it works by you telling it which way it is facing, it will then turn to north, then ask for the destination coordinates. Currently, its asking for lots of "ends", which I got confused about because I am rubbish at making it look nice with spaces.


function look(direction)
  write("Direction?   ")
  curDir = read()
   if curDir == n then
	 print("Facing North")
	 end
   if curDir == e then
	  turtle.turnLeft()
	  print("Facing North")
	  end
   if curDir == s then
	   turtle.turnRight()
	   turtle.turnRight()
	   print("Facing North")
	   end
   if curDir == w then
	  turtle.turnRight()
	  print("Facing North")
	  end
   else
	print("Invalid Entry")
	shell.run("goto")
	end
end
   if direction == "north" then
	end
   if direction == "east" then
   turtle.turnLeft()
   end
   if direction == "south" then
	turtle.turnRight()
	turtle.turnRight()
	end
   if direction == west then
	turtle.turnLeft()
	end
end
end
end
end

end
write("Enter X Coord:  ")
xTarget = read()
write("Enter Y Coord;  ")
yTarget = read()
write("Enter Z Coord:  ")
zTarget = read()
x, y, z = gps.locate(5)
		while yTarget < y do
			 turtle.down()
		end
		while yTarget > y do
			 turtle.up()
		end
	  if xTarget < x then
		 look("west")
		 while xTarget < x do
		  turtle.forward()
	   end
	  end
	 if xTarget > x then
	   look("east")
		while xTarget > x do
		   turtle.forward()
		 end
	  end
  if zTarget < z then
  look("north")
   while zTarget < z do
	turtle.forward()
   end
  end
   if zTarget > z then
	look("south")
	  while zTarget > z do
	   turtle.forward()
	  end
   end
end


Your mission, should you chose to accept it is to put in all the "ends" and make it work!

THANKS!

H

Part of your issue is that you don't appear to know what an 'elseif' is.

subsequently you have an else with no starting if.

Look at it this way:
Ifs start a 'selection' block, which must be ended with an end
within that block, you may use elseifs, which are like ifs but they work within the selection block, so while
if curDir == n then print("north") end
if curDir == e then print("east") end
are two separate if blocks,

if curDir == n then print("north")
elseif curDir == e then print("east")
end
would be part of the same testing block

The key point that comes next is the usage of else. Else will execute providing none of the previous if/elseif conditions were true, but it can only execute within a selection block, like so:
if curDir == n then print("north")
elseif curDir == e then print("east")
else print("invalid")
end

meaning

if curDir == n then print("north") end
if curDir == e then print("east") end
else print("invalid")
end
is wrong, because all previous ifs have been ended before the else, thus else doesn't have an if before it.

hopefully that has made things clear enough for you to fix some of your bugs.