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

Help cleaning up program

Started by 1480c1, 12 October 2015 - 03:03 AM
1480c1 #1
Posted 12 October 2015 - 05:03 AM
I'm wondering if anyone can help me clean up my code, I'm a very basic beginner and compiled this just by reading the wiki.

http://pastebin.com/jcvyeANX

sorry if it hurts your eyes. I did not understand most of it and only got by my knowlege of Basic coding language that I know from my calculator. I'm mainly trying to compress cobble using a crafty turtle but I need to skip slot 8, 4, 12 and need to keep atleast one cobble in each 9 slot excluding the factors of 4 for it to startup.
Lyqyd #2
Posted 12 October 2015 - 05:11 AM
Moved to Ask a Pro.
valithor #3
Posted 12 October 2015 - 05:30 AM
One of your biggest friends in cleaning up your code will be the for loop. Site with short explanation here: http://www.tutorials...ua_for_loop.htm

Another thing will be the modulus operator "%". The useful part about modulus, is you can use it to see if a number can be evenly divided into by another number. Luckily in your program all of the slots you want to avoid using can be divided by 4, which makes it easy to check. You can think of modulus as the remainder if one number was divided by another.

Example of modulus:

2%5 --# this will output 2, because 5 can not go into 2 any evenly
7%5 --# this will also output 2, because after 5 goes into 7 once evenly all that is left is 2
12%5 --# also 2 for same reason as above

--# if you want to see if a number has can be evenly divided
4%2 --# this will equal 0, because 2 goes into 4 evenly
9%3 --# also 0


local slot1 = turtle.getItemDetail(1).name
local slot2 = turtle.getItemDetail(2).name
local slot3 = turtle.getItemDetail(3).name
local slot5 = turtle.getItemDetail(5).name
local slot6 = turtle.getItemDetail(6).name
local slot7 = turtle.getItemDetail(7).name
local slot9 = turtle.getItemDetail(9).name
local slot10 = turtle.getItemDetail(10).name
local slot11 = turtle.getItemDetail(11).name
local A = 4
local C = "minecraft:cobblestone"


function suckCobble()
  for i = 1, 11 do --# refer to the link i posted to see what this loop is doing
	if i%4 == 0 then -- Checking to see if i can be evenly divided by 4
	  turtle.select(i)
	  turtle.suck(A)
	end
  end
end

function balance()
  for i = 1, 11 do
	if i%4 == 0 then --# checking to see if i can be equal divided by 4
	  if turtle.getItemCount(i) ~= A + 1 then
		turtle.select(i)
		turtle.drop()
		turtle.suck(A + 1)
	  end
	end
  end
end

while true do
   suckCobble()
balance()
   turtle.craft(A)
   sleep(15)
end

There are a few other things that could be cut down, but they are slightly more complicated. If you want to look them up on your own, the main thing I left out is you could use a table to hold all of those variables that you defined at the beginning.
Edited on 12 October 2015 - 03:38 AM
1480c1 #4
Posted 12 October 2015 - 02:46 PM
thank you very much.