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

Turtle fill hole program - Logic problem

Started by TyCamden, 15 October 2012 - 03:18 PM
TyCamden #1
Posted 15 October 2012 - 05:18 PM
My program runs now, but it has a logic problem…

I place it in front of a row of blocks in which only the one in front of the turtle has a 1-deep hole.

It fills that hole
moves left
senses there is no hole to fill
moves left
senses there is no hole to fill…

This is where it should recenter to where it started and start looking to the right

Instead it goes on to the left forever.

Any ideas to help me ?

code…


--[[ Program name : fillarea
	   Put this program in front of an unfilled hole in the ground
		 (with a defined edge on the layer the turtle is sitting on),
		 it should fill it with dirt then return to starting position.
	  --]]
--[[ Be sure the turtles has the following items
	  in the correct slots...
	    slot 13 has 64 coal/charcoal
	    other slots (at least 1) has filler (dirt/etc)
	  --]]
function refuel()
  fuellvl = turtle.getFuelLevel()
  if fuellvl<100 then
    amtchar = turtle.getItemCount(13)
    if amtchar > 1 then
	  turtle.select(13)
	  turtle.refuel(2)
	  turtle.select(1)
    else
	  if amtchar > 0 then
	    turtle.select(13)
	    turtle.refuel(1)
	    turtle.select(1)
	  end
    end
  end
end
function fillerCheck()
  fillerFlag = 0
  for X = 1,16 do
    turtle.select(X)
    if fillSlot == 13 then
	  fillSlot = 14
    end
    if turtle.getItemCount(X) > 0 then
	  fillerFlag = 1
    end
  end
  if fillerFlag == 0 then
    print "Out of Filler"
  end
end
function moveLeft()
  turtle.turnLeft()
  turtle.forward()
  turtle.turnRight()
  colModifier = colModifier - 1
end
function moveRight()
  turtle.turnLeft()
  turtle.forward()
  turtle.turnRight()
  colModifier = colModifier + 1
end
function moveForward()
  turtle.forward()
  rowModifier = rowModifier + 1
end
function moveBack()
  turtle.back()
  rowModifier = rowModifier - 1
end
function moveSide()
  if lookingLeft == 1 then
    moveLeft()
  else
    moveRight()
  end
end
function moveRecenter()
  while colModifier < 0 do
   moveRight()
  end
  while colModifier > 0 do
   moveLeft()
  end
end
function moveRecenterRow()
  while rowModifier < 0 do
    moveForward()
  end
  while rowModifier > 0 do
    moveBack()
  end
end
function fillHole()
  --[[ move down as far as it can --]]
  while not turtle.detectDown() do
    refuel()
    turtle.down()
    downMoves = downMoves + 1
  end
  --[[ move up and fill --]]
  while downMoves > 0 do
    refuel()
    turtle.up()
    fillerCheck()
    if fillerFlag == 1 then
	  while not turtle.placeDown() do
	    fillSlot = fillSlot + 1
	    if fillSlot == 13 then
		  fillSlot = 14
	    end
	    turtle.select(fillSlot)
	  end
	  turtle.placeDown()
    end
    downMoves = downMoves - 1
  end
  recentFillCol = 1
  recentFillRow = 1
end
--[[ set numeric var's --]]
  fuellvl = 0
  amtchar = 0
  fillerFlag = 0
  downMoves = 0
  X = 0
  fillSlot = 1
  rowModifier = 0
  colModifier = 0
  searchOver = 0
  lookingLeft = 1
  recentFillCol = 0
  recentFillRow = 0
--[[ move forward one --]]
  refuel()
  fillerCheck()
  turtle.forward()
  rowModifier = rowModifier + 1
while searchOver == 0 and fillerFlag ~= 0 do
  if not turtle.detectDown() then
    fillHole()
    moveSide()
  else
    --[[ no hole directly under --]]
    if recentFillCol == 1 then
	  --[[ there is no hole under AND the spot on the side WAS filled as well --]]
	  moveSide()
	  recentFillCol = 0 --[[ RECENTLY ADDED THIS LINE... DID NOT SEEM TO HELP --]]
    else
	  --[[ there is no hole under AND the spot on the side was NOT filled as well --]]
	  if lookingLeft == 1 then
	    moveRecenter()
	    lookingLeft = 2
	    recentFillCol = 1 --[[ NOT SURE IF THIS IS NECESSARY --]]
	  else
	    moveRecenter()
	    if recentFillRow == 1 then
		  moveForward()
		  lookingLeft = 1
		  recentFillRow = 0 --[[ NOT SURE IF THIS SHOULD BE 0 or 1 --]]
	    else
		  --[[ you are done with the filling of the hole --]]
		  searchOver = 1
	    end
	  end
    end
    recentFill = 0
  end
end
moveRecenter()
moveRecenterRow()
Lyqyd #2
Posted 15 October 2012 - 05:39 PM
Please don't clutter up the forums by making a new topic for each and every minor issue you run into while debugging the same program. One topic would have worked just as well (possibly better) than the now three (or is it four?) topics you've posted about the same piece of code.
TyCamden #3
Posted 15 October 2012 - 05:57 PM
Sorry, I thought that a new problem deserved a new topic. I didn't mean to clutter it up. I thought it was just the thing to do.

I will use this topic for this program ("fillarea") from here forward.

Any help with the logic of my program, by anyone would be appreciated.