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

Problem with a Mining Turtle Lua Source Code

Started by jkhsjdhjs, 30 August 2013 - 03:07 PM
jkhsjdhjs #1
Posted 30 August 2013 - 05:07 PM
Title: Problem with a Mining Turtle Lua Source Code

Hello,

I got a Problem with my Mining Program for my Mining Turtle. It would be really nice if you could take a look over it.
I pasted my sourcecode here.

And the tabs in the source code are only for a more clear view over the code. When I use the program in the turtle I delete the tabs.
Thanks a lot.

Regards,

jkhsjdhjs
Bubba #2
Posted 30 August 2013 - 05:11 PM
Split into new topic. Oh and you shouldn't delete the tabs. They have no effect on how the program runs and are a great programming practice that you should always use :)/>
Kingdaro #3
Posted 30 August 2013 - 05:26 PM
First problem:

float x=0, y=0

You don't have to define variable types. This will suffice:

x = 0
y = 0

--# or
x = 0; y = 0

--# oor
x, y = 0, 0

Second problem, the use of while loops is usually discouraged when you need to do a task a certain number of times. It's much cleaner and generally better logic to use a for loop. Instead of doing this:

i = 1
while i <= 10 do
  print(i)
  i = i + 1
end

You can do this:

for i=1, 10 do
  print(i)
end

--# for loops also have a step option
--# this will print 1, 3, 5, etc.
for i=1, 10, 2 do
  print(i)
end

Also a note, you'll have to start thinking in terms of 1 instead of 0, because lua tends to favor 1-indexing over 0-indexing.
jkhsjdhjs #4
Posted 30 August 2013 - 06:31 PM
for i=1, 10 do
print(i)
end


Will this loop repeat 10 times? If yes, I understood this type of loop.
Kingdaro #5
Posted 30 August 2013 - 06:32 PM
Yes it will.
jkhsjdhjs #6
Posted 30 August 2013 - 06:33 PM
Ah, ok thank you all very much ;)/>

EDIT: Yeah, it's working :D/>
Here's the final source code.