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

Starting new AI project, Is this code clean ?

Started by wood, 14 January 2016 - 06:37 AM
wood #1
Posted 14 January 2016 - 07:37 AM
Hi, I'm new here and still a beginner at programming but I decided this year I should do some more programming.
So I installed CC and started playing with turtles again (I have a little experience with it already).
So long story short I got the idea to do a sort of turtle AI where it would roam the land free and multiply.

Here is a bit of code that I use to clear a square or rectangular area :

http://pastebin.com/ErTZQQ0R

Alright so here is my question : is the function clearLayer "optimized" ?

To explain quickly what it does, the 1st IF check if the number of rows is odd, if so it's fairly straight forward.
Then the ELSEIF is the part that was harder to come up with. So this execute only if the number of rows are even because when the turtle finish clearing a layer it's not facing the same way when it goes up to the next layer. That's why the reste of the code is there.

So is it optimized, I was really proud of it all when it was finaly working handling all conditions but I feel there is a lot of stuff that could be simplified but I fail to see how.
Simply looking to improve my programming skills, thank you in advance for anyone leaving a reply.
Bomb Bloke #2
Posted 14 January 2016 - 07:52 AM
Werrllll, you're basically repeating the same code block three times there. You should be able to create a single version which handles all cases.

I'll give you a tip - what happens if you add rows/x to i, and then take the modulus of the result…? What if you factor in the current layer?

I'm also a bit concerned in that the "go up" code around line 69 appears to be sitting within a loop it shouldn't be.
Creator #3
Posted 14 January 2016 - 12:27 PM
How is this related to AI? Self replicating turtle?
wood #4
Posted 14 January 2016 - 04:34 PM
@Creator
It's a small part of the entire code.
I just started to write the code, I'm focusing on one small parts at a time because I'm not all that good in programming.

How it relate to AI: it is used by the turtle to clear an area where it will build a platform to harvest trees. I could also use it to send the turtle quarrying to gather ressources.
wood #5
Posted 16 January 2016 - 03:11 PM
@Bomb Bloke
Hi, first of all thank you for you support.
I was just about to beg you for more precise instruction but I finally figured out how to simplify my code.
Here is the new simplified version : http://pastebin.com/6k8n330u
I added a yAlt variable to keep track of what "layer" the turtle is at.
Please tell me if there is any more improvement that could be done, like could I remove the ELSE from the first IF as to not have any repeating code.
Again thank you for your help.
Bomb Bloke #6
Posted 17 January 2016 - 12:15 AM
That's a bit better - you very nearly got it. But I'll show you what I was thinking of:

local function clearLayer()
	local bump
	if rows%2 == 0 and yAlt%2 == 0 then
		bump = 0
	else
		bump = 1
	end
	
	for i=1, rows do
		clearLine()
		if (i+bump)%2 == 1 and i < rows then
			turtle.turnLeft()
			turtle.dig()
			turtle.forward()
			turtle.turnLeft()
		elseif i < rows then
			turtle.turnRight()
			turtle.dig()
			turtle.forward()
			turtle.turnRight()
		end
	end
end

And this chunk here:

	local bump
	if rows%2 == 0 and yAlt%2 == 0 then
		bump = 0
	else
		bump = 1
	end

… can then be further reduced, using the technique discussed here:

	local bump = (rows%2 == 0 and yAlt%2 == 0) and 0 or 1

Also see this guide regarding the use of the "local" keyword.

One other thing: it'd be worthwhile making a custom function that makes the turtle go forward. Something like this for example:

local function forward(amount)
	amount = amount or 1  --# If amount is nil (function was called without a value), sets it to 1.
	
	for i = 1, amount do
		while not turtle.forward() do  --# Until an attempt to move forward succeeds,
			turtle.dig()           --# attempt to clear blocks
			turtle.attack()        --# and attempt to kill mobs
		end
	end
end

This way, your code won't fail if the turtle digs a block and still can't move because there's eg a mob in the way, or some gravel falls to fill the space.