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

Mine turtle (need help)

Started by gheotic, 03 February 2013 - 02:44 AM
gheotic #1
Posted 03 February 2013 - 03:44 AM
im trying to make a program that clears a area thats 3 high and then i can choose how fare it should mine forward and how many rows it should mine.

this code is made so fare so it should mine a 10x10 area but it only takes 2 rows how should i make it to take 10x10???


local dis = 10
local rows = 10

---------------------------------------
local x = 0
local y = 0
local turns = 0

function Clear() -- Clear Screen
term.clear()
term.setCursorPos(1,1)
end
function Return() --return
if x == dis then
turtle.turnRight()
turtle.turnRight()
while x > 0 do
turtle.forward()
x = x - 1
end -- loop
end -- end if
end -- end function

function digForward()
print("Digforward function")
while x < dis do -- dig the first row
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
x = x + 1
end
Return()
end
function digBackward()
print("DigBackward function")
while x > 1 do -- dig the first row
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
x = x - 1
end
end
--------------------------------------
Clear()
turtle.up()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
x = x + 1
print("ready to start")

digForward()

if turns < rows then

turtle.turnRight() -- turn right
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
turtle.turnRight()
rows = turns + 1
end

digBackward()
if turns < rows then
turtle.turnLeft() -- turn left
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
turtle.turnLeft()
rows = rows + 1

end


any help would be appreciated :)/>
Mailmanq! #2
Posted 03 February 2013 - 04:23 AM
I tried, but you need to clean up your code, It is too much work to read.

Here are a few tips :

1.) For loops, they are a loop that do something a specified amount of times

Example :


for i = 1, 50 do
  print("Boop!")
end

This will print boop 50 times, you can use any variable for i as well as replace the 1 and 50 with any numbers, it basically starts with the first number and adds one until it equals fifty, every time it adds one, it runs the code in the loop.

2.) INDENTATION when in loops, if statement, or functions, put all the code 2 spaces over.
soccer16x #3
Posted 03 February 2013 - 05:01 AM
I see in your program you call "if turns < rows then… " and i see nowhere where turns will ever be greater than rows so it will always either go forward or back. Also, what i see is "rows = turns + 1" so your resetting your row back down to 1 everytime? And then i see "rows = rows + 1" so your just adding to rows again? effectively making turns always less than rows so it would never stop? And when I just tested it all it did was dig forward 11 then come back turn right and then mined turned right and then turned left and mined again then went forward and turned left.

I don't even know how I'd help you, but if I were you here I'd figure out what it is you're trying to get to here in your code, because your current code is just confusing the turtle.

Oh and also I see that you want to add the functionality of saying how long and wide it should be, so what you can do is:
tArgs = {...}

dist = tArgs [1]
rows = tArgs[2]
And then with this when you enter in the name of the program you put at the end the length and width you would like. so if it's called mine you would put in "mine 10 10" to mine a 10x 10 area.

and then like mailmanq said, for loops would be better, especially since then you dont have to worry about x + y counting necessarily. So how you'd use it would be
for i = 1, dist do
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
end
--Then you'd want to have it turn left, or right depending on how what you want to do so that way its faster mining then having to come back and then turn right.
turtle.turnLeft()
turtle.forward()
turtle.digUp()
turtle.digDown()
end
--And then you'd have it start over and also set the number of rows done with some sort of variable, like x? and once x == rows then you'd finish.