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

i need help with my turtle building a 9x9

Started by gigz, 05 September 2012 - 12:16 AM
gigz #1
Posted 05 September 2012 - 02:16 AM
this is the current code.

turtle.up()

o = 5
while o > 0 do

i = 9
while i > 0 do

turtle.place.Down()
turtle.forward()

i =i-1
end
p = 5
while p > 0 do

turtle.up()
p =p-1
end



I'm having trouble make it go up after it makes the first layer so it just stays on the same layer but goes over it five times then go up five blocks
Luanub #2
Posted 05 September 2012 - 02:30 AM
Try using for loops instead

Make the turtle go up 5 times.

for x=1, 5 do
turtle.up()
end

You can probably eliminate most of your while loops, convert then to for loops and eliminate the need to do the math. Should be more robust.
gigz #3
Posted 05 September 2012 - 02:33 AM
hang on what do you mean? Can you show me because im pretty new to this and want to learn how to program.
NIN3 #4
Posted 05 September 2012 - 03:13 AM
Im not sure what your code is supost to be doing, since it seems its supost to place a block, go forward, then place more, then after a while go up. like making long stairs.

Anyway, he means for you to do somthing like this. (note that im building a sky pillar, since im not sure what your doing)

for i=1,5 do
turtle.placeDown()
turtle.up()
end
This is what you want to do. replace the 5 with what ever number you want, to make the tower this high
gigz #5
Posted 05 September 2012 - 03:25 AM
Why im trying to do is have the turrtle build a 9x9 house but just the walls but it only builds the first part then goes over it five times and won't go up a layer and I dont know where to put the veriable or at least a way to fix it.
NIN3 #6
Posted 05 September 2012 - 03:34 AM
Reminds me of some old code I have hanging around. I never finished this, since It became outdated. But I shall give it to you, to learn from it.
Spoiler

set = 1
function wallside() --this is a function, and with in it, a loop. This allows me to call on this loop again and again.
for i=1,param1 do --param1 is a variable, set later, when I ask the user how large there house is to be.
turtle.back()
turtle.place()
end
end
function wallsidepro() --this one does the 4rth wall, since it has to get to the next floor.
for i=2,param1 do
turtle.back()
turtle.place()
end
turtle.turnLeft()
turtle.back()
turtle.place()
turtle.up()
turtle.forward()
turtle.turnRight()
turtle.back()
turtle.turnRight()
end
function walls() --this is a collection of the above 2 fuctions, and it runs them too make a full set of walls for a floor.
wallside()
turtle.turnRight()
wallside()
turtle.turnRight()
wallside()
turtle.turnRight()
wallsidepro()
end
function rightturn() -- this is just a collection of movments, so I dont have to write them out lots.
turtle.turnRight()
turtle.back()
turtle.turnRight()
turtle.back()
end
end
function leftturn() --another function that is used to collect up stuff.
turtle.turnLeft()
turtle.back()
turtle.turnLeft()
turtle.back()
end
function floor() --as it says, this makes the floor. its still buggy, so I wouldent copy paste it......
for i=1,param1 do
if set == 1 then do
wallside()
rightturn()
set = set+1
end
elseif set == 2 then do
wallside()
leftturn()
set = set-1
end
end
end
function recovery()
if set==2 then do
turtle.back(param1)
turtle.turnRight()
turtle.back(param1)
turtle.up()
turtle.turnRight()
turtle.back()
end
elseif set==1 then do
turtle.forward()
turtle.turnLeft()
turtle.forward(param1)
turtle.up()
turtle.turnLeft()
turtle.back()
end
end						   --end the buggy part, I might fix it later.
print("how long shall it be?") --this is the first line that runs, and where all the magic happens.
param1= read()					 --collects how long the wall is.
param1 = tonumber(param1) --converts to number
if param1>=3 then do --small failsafe, since it cant handle small numbers
write("ok, it shall be ")
write(param1)
write(" blocks long.\n")
write("And how tall?\n")
param2=read()			  --how tall the building is
param2 = tonumber(param2)
write("Ok, its "..param2.." blocks tall.\n")
end
floor() --makes the floor
recovery() --buggy, is supost to bring it back to the start pos, so it can start on walls
for i=1,param2 do --makes the walls, as high as wished
walls()
end
floor()
recovery()
turtle.forward()
turtle.down(param2) --brings it to the corner stone of the house.
end
Luanub #7
Posted 05 September 2012 - 04:02 AM
A numerical for loop with run through the commands inside it the selected number of times so instead of doing something like this


i = 9
while i > 0 do
turtle.place.Down()
turtle.forward()
i =i-1
end

You can simply do, and it will iterate through the code 9 times.

for i=1, 9 do
turtle.place.Down()
turtle.forward()
end

numerical for loop breakdown is for var=1, num do
var is the variable that will increase with each increment, and the number following it is where you want to start the count.

for x=1, 5 do
print(x)
end

Will print:
1
2
3
4
5
on the screen.

num is the number of increments you want to run. You can just put a number or a var containing the number you want to increment.

There is a third argument that I rarely see used that would come after then num that is how much you want to increment by so(basically a step)

for x=1, 5, 0.5 do
print(x)
end
Would print:
1
1.5
2
2.5
3


Hope this helps some. The for loops are a lot more reliable then doing while loops and using your own math.

For what you are doing I would convert to for loops.

EDIT: It looks like you are missing a lot of code. There is nothing that ever turns and builds the other walls. Is this all of the code?

From what I can tell this is going to place 9 blocks, then move up 5 spaces and repeat over and over since o will never == 0.

Try this and see if it will do the trick. I just put it together and have not tested it but it should work.

for c=1, 5 do
turtle.up()
for a=1, 4 do
  for x=1, 8 do
	turtle.placeDown()
	turtle.forward()
  end
  turtle.turnRight()
end
end
Edited on 05 September 2012 - 02:16 AM
FuzzyPurp #8
Posted 05 September 2012 - 05:06 AM
Why not just watch direwolf's video, using the pause button here and there?
gigz #9
Posted 05 September 2012 - 05:46 AM
@ luanub I believe I typed the code wrong on here at one part I have it so where it turns and finishs the first layer.
gigz #10
Posted 05 September 2012 - 05:59 AM
@ fuzzyPop that's because I wanted to use my own code not just copy someone else's because I'm trying to learn programming.