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

End expected

Started by applesauce10189, 04 January 2014 - 08:12 PM
applesauce10189 #1
Posted 04 January 2014 - 09:17 PM
It's a turtle mining program, idk what to do, I added end over and over one at a time and it kept giving me the message so then I thought maybe I had too many, so I got rid of one of two end's and still got the error,


function emptyInventory()
  for i = 3, 16 do
    turtle.select(i)
    turtle.dropDown(64)
  end
end

function gravityCheck()
  if  turtle.dig() == true then 
    turtle.dig() until turtle.dig() == false
  end
end

function appleMine()
  while true do
    for i = 1,60 do
      turtle.select(1)
      turtle.digDown()
      turtle.down()
      turtle.digDown()
      turtle.down()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.forward()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.forward()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.forward()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.forward()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.back()
      turtle.back()
      turtle.up()
      turtle.placeDown()
      emptyInventory()
      appleFuel()
    end
  end
end
function appleFuel()
  if turtle.getFuelLevel() <= 50 then
    turtle.select(2)
    turtle.placeUp()
    turtle.suckUp()
    turtle.refuel(64)
    turtle.digUp()
  end
end
turtle.select(1)
appleMine()

note to self, when copy/pasting from pastebin use raw paste data or the website will destroy your code whether you use
 or [ spoiler ]
Edited on 04 January 2014 - 08:16 PM
Bomb Bloke #2
Posted 04 January 2014 - 09:24 PM
Did you mean?:

function gravityCheck()
  while turtle.dig() do end
end
applesauce10189 #3
Posted 04 January 2014 - 09:29 PM
I'm new to coding, so thanks for the help,
applesauce10189 #4
Posted 04 January 2014 - 09:35 PM
New problem, it just turns to the right and stops the entire program. The previous version of the program *cough cough* THIS http://www.computerc...turtle-program/ *cough cough* worked perfectly, if you want to know the difference between this version and the other one, the only addition to this version is the function gravityCheck() and putting it to use in a couple spots. Quick edit. It also turns off after a few seconds.
Yet another edit, I think this might be a client problem not code, I broke and placed the turtle and now I can't even type into it, and every now and then when I place it, it doesn't even turn on when I open the GUI.
Third and hopefully last edit. Exiting and coming back to my single player world seemed to fix it. Still turns to the right and turns off though.
Edited on 04 January 2014 - 08:55 PM
Bomb Bloke #5
Posted 04 January 2014 - 10:17 PM
Hrm. What do you mean by "stops the whole program"? Produces an error? If so, what? Or does it just stall, and not do anything at all? Does it not dig, or move downwards, at any point? Please be precise about what happens, and if you're not sure about exactly which line it's sticking on, add print statements in until you are.
applesauce10189 #6
Posted 04 January 2014 - 10:21 PM
Basically, it digs down, doesn't go down as it should, probably out of fuel actually, but it turns right, then the turtle itself turns off. Quick edit. I added little checkpoints in the program. Every time appleMine() does something it prints a message. It dug right after turning right. but it might be stopping on the middle of gravityCheck() and not showing the checkpoint.
Edited on 05 January 2014 - 08:30 AM
applesauce10189 #7
Posted 05 January 2014 - 09:38 AM
Okay, so, added checkpoints and now I know it's the while turtle.dig() do end part that's screwing up the program. It starts but doesn't end. Maybe put a break in there? I'll edit the code a bit and show you the code afterwards and I'll tell you if it works or not.


Here's the new code. It says there's no loop to break on line 9.
Spoiler

function emptyInventory()
  for i = 3, 16 do
	turtle.select(i)
	turtle.dropDown(64)
  end
end

function gravityCheck()
  print("Beginning 'while true do' statement.")
  while turtle.dig do end
	if turtle.dig() == false then
	  break
	end
  print("while true do statement broken.")
end

function appleMine()
  while true do
	for i = 1,60 do
	  turtle.select(1)
	  turtle.digDown()
	  print("Dug down.")
	  turtle.down()
	  print("Moved down.")
	  turtle.digDown()
	  print("Dug down again.")
	  turtle.down()
	  print("Moved down again.")
	  turtle.turnRight()
	  print("WARNING: TURNED RIGHT!")
	  turtle.dig()
	  print("Dug.")
	  gravityCheck()
	  print("New gravity check. May be the problem.")
	  turtle.turnLeft()
	  turtle.turnLeft()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.forward()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnLeft()
	  turtle.turnLeft()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.forward()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnLeft()
	  turtle.turnLeft()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.forward()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnLeft()
	  turtle.turnLeft()
	  turtle.forward()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnRight()
	  turtle.back()
	  turtle.back()
	  turtle.up()
	  turtle.placeDown()
	  emptyInventory()
	  appleFuel()
	end
  end
end
function appleFuel()
  if turtle.getFuelLevel() <= 50 then
	turtle.select(2)
	turtle.placeUp()
	turtle.suckUp()
	turtle.refuel(64)
	turtle.digUp()
  end
end
turtle.select(1)
appleMine()
Edited on 05 January 2014 - 08:54 AM
Kingdaro #8
Posted 05 January 2014 - 10:00 AM
Replace "break" with "return":

function gravityCheck()
  print("Beginning 'while true do' statement.")
  while turtle.dig do end
  if turtle.dig() == false then
    return
  end
  print("while true do statement broken.")
end
You can only use break inside of loops, and using "return" will stop the function at that point.
Bomb Bloke #9
Posted 05 January 2014 - 11:11 AM
Or better yet, ditch the "if" block entirely.
electrodude512 #10
Posted 05 January 2014 - 06:04 PM

function gravityCheck()
	print("Beginning 'while true do' statement.")
	while turtle.dig do end
		if turtle.dig() == false then
			break
		end
	print("while true do statement broken.")
end

What Bomb Block said would be the ideal solution:

function gravityCheck()
	print("Beginning 'while true do' statement.")
	while turtle.dig() do end
	print("while true do statement broken.")
end

Your original code crashed because you ended the while loop before the if statement. You wanted to end the while loop after the if statement. Also, you weren't calling turtle.dig in the 'while turtle.dig do', you were only checking if it was defined, which it always will be for a turtle (unless someone deletes it).
Edited on 05 January 2014 - 05:08 PM
applesauce10189 #11
Posted 05 January 2014 - 07:51 PM
The program's fixed and working fine. New problem at hand that is just now appearing. Not sure why it didn't happen before but after adding/fixing gravityCheck() it's just now happening, so, basically, the program is supposed to dig a staircase down and such, for every level or so it goes down, it goes back 2 goes up and places the item/fuel chests, refuels and empties the inventory. here's the problem, it doesn't go back 2, it either goes back 1 or not at all, then when it tries to empty inventory and refuel, it gets nothing achieved.
subzero22 #12
Posted 08 January 2014 - 01:43 PM
is it trying to back up into a block? if so turtles can't dig backwards as far as I know. also what kind of mining program is it suppose to be or is it just a stair one?