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

Building a two-ingredient house

Started by Cherrie, 15 April 2012 - 09:28 AM
Cherrie #1
Posted 15 April 2012 - 11:28 AM
Hey, I was trying to make a program for a turtle to build a house from given ingredients in slots 1-4, first 3 layers are 10x10 from ingredient #1 on slots 1 and 2, the rest are from ingredient #2 on slots 3 4, where first layer is 10x10, then each layer's perimeter decreases by 2.
When I try to run it the game gives me an error saying there's an "end" expected to close "while" string at line 24.
Here is the code. (there's only one 'while' loop)

print("Building a house. Requirements: 120 units of base ingredient and 116 units of roof ingredient.")
local function cornerLeft()
   turtle.turnRight()
   turtle.forward()
   turtle.turnLeft()
   turtle.forward()
   turtle.forward()
   turtle.turnLeft()
end
local function buildLayer(i)
   for l=1,i do
   turtle.place()
   turtle.turnRight()
   turtle.forward()
   turtle.turnLeft()
   end
end
x=turtle.getItemCount(1)+turtle.getItemCount(2)
y=turtle.getItemCount(3)+turtle.getItemCount(4)
while y>=1 do
   --Auto-select first or second slot.
   if turtle.getItemCount(1)>0 then
	turtle.select(1)
   end
   else
	turtle.select(2)
   end
   --Build 3 layers of first ingredient, 10x10 sized.
   for k=1,3 do
	for j=1,4 do
	 if j=1 then
	  i=10
	 end
	 if j=2 or j=3 then
	  i=9
	 end
	 if j=4 then
	  i=8
	 end
	 buildLayer(i)
	 cornerLeft()
	end
	turtle.up()
   end
   --Auto-select third or fourth slot.
   if turtle.getItemCount(3)>0 then
	turtle.select(3)
   end
   else
	turtle.select(4)
   end
   --Build 5 roof layers, size decreasing by 2 blocks each layer.
	for j=1,4 do
	 if j=1 then
	  i=10
	 end
	 if j=2 or j=3 then
	  i=9
	 end
	 if j=4 then
	  i=8
	 end
	 buildLayer(i)
	 cornerLeft()
         i=i-2
	end
   end
end

Help me spot the issue, anyone? I tried adding one "end" at the very bottom, it didn't help. I also tried adding multiple "end"s but still same error - got me thinking the problem is somewhere inside the loop that breaks its access to the appointed "end".
A little question for CC forum veterans - is this worth a 'program release' thread, or should I put more work and tweaks to it beforehand? (I'm talking about the complete, working version of what I'm trying to do here)

P.S. who knew turtles can 'place' blocks in front of them, without having any physical blocks around them? a very lovely feature, makes life easier :>

Cheers.
Cookiebal #2
Posted 15 April 2012 - 12:26 PM
It's
if … then … else … end
you got
if … then .. end else … end
a few times

Like here
   if turtle.getItemCount(1)>0 then
        turtle.select(1)
   end
   else
        turtle.select(2)
   end
Cherrie #3
Posted 15 April 2012 - 12:30 PM
oh lawd… followed the lead of some pre-made progs on that one. thanks :)/>/>