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

Room Builder

Started by calmelb, 11 January 2013 - 02:09 AM
calmelb #1
Posted 11 January 2013 - 03:09 AM
Hello Everyone, I was playing around in CC and I thought to create a room builder, I have got it going round doing a half outline of the room and the height, But I could not get it to fill in the room, could I please get some help? Here is my code:

Spoilerprint("What is the Length?: ")
length = io.read()
print("What is the Width?: ")
width = io.read()
print("What is the Height?: ")
height = io.read()
for i = 1, length do
turtle.digDown()
turtle.placeDown()
turtle.forward()
end
turtle.turnRight()
for i = 1, width do
turtle.digDown()
turtle.placeDown()
turtle.forward()
end

If anyone can help then that would be great!
Orwell #2
Posted 11 January 2013 - 03:52 AM
Does this work? Because your first problem will be that length, width and height are strings, not numbers. Use this to convert them to numbers easily:

length = tonumber(read())
The same goes for the other variables.

Second, you'd want to use nested for loops. This means that there's one for loop that goes trough the different levels (height). In that loop, you'd have a loop that goes through each row (width). In that loop you'd have a loop that will go through placing each block in that row (length). I'd suggest using functions for this to make it modular and readable.

Third, you'd have to rotate your turtle in between rows and reposition it in between levels.
ChunLing #3
Posted 11 January 2013 - 05:23 AM
A for loop automatically tonumbers its parameters, but it is best practice to originally set them as numbers (this also allows you to repeat the input phase until the user provides numbers, making the program a good deal less failure prone).

Are you trying to create a room (walls and a ceiling) or a solid block? Right now it looks like you do the footings for two walls.
Orwell #4
Posted 11 January 2013 - 05:31 AM
A for loop automatically tonumbers its parameters, but it is best practice to originally set them as numbers (this also allows you to repeat the input phase until the user provides numbers, making the program a good deal less failure prone).
How could I not have known that? :huh:/> I'm glad I've learned something new but I'm also shocked by my ignorance :P/>.
ChunLing #5
Posted 11 January 2013 - 05:34 AM
Meh, there are plenty of things you have no reason to know about Lua unless you've been a slob in your programming, because Lua goes to extraordinary lengths to be forgiving of novice mistakes.
calmelb #6
Posted 11 January 2013 - 06:49 PM
I am trying to do a room but i do not know how to get the full cube shape, I also do not really get how to do those loops. Sorry if I was a bit vague
ChunLing #7
Posted 12 January 2013 - 04:26 AM
Well, there's a couple of ways to go about it. Basically, we could do one layer at a time or we can do three layers at a time (faster and more fuel efficient, but requires more coding).

For now I supposed we'll stick to the one layer approach. Right now you've got two sequential for loops separated by a turn, each of which does one layer of one wall in one dimension. You want too walls for each dimension, so let's just turn right again and enclose it in a for j=1,2 do loop:
for j=1,2 do
    for i = 1, length do
        turtle.digDown()
        turtle.placeDown()
        turtle.forward()
    end
    turtle.turnRight()
    for i = 1, width do
        turtle.digDown()
        turtle.placeDown()
        turtle.forward()
    end
    turtle.turnRight()
end
Then we want to repeat this so we lay down layers until we reach the proper height. So we go up, enclose everything in another loop (and we make this one the i loop and the innermost loops use k, just for style):
for i=1,height do
    -- everything so far, with the inner loops using k (not super important, just for easy tracking of the scopes)
    turtle.up()
end
And then we reposition and do the ceiling, we can go up one more and put a lid on top, or we can go in and insert the ceiling in the smaller space. Going up is a bit easier (cause we do that anyway), so we'll do that. We do a row, using a loop, then we hook right or left depending on whether the loop is an even or odd iteration in an enclosing loop. Like so:
-- everything so far
for i=1,length do
    for j=1,width do
        turtle.placeDown()
        turtle.forward()
    end
    if i%2 ~= 0 then --its odd, so we hook right
        turtle.turnRight()
        turtle.placeDown()
        turtle.forward()
        turtle.turnRight()
    else --even, so we hook left
        turtle.turnLeft()
        turtle.placeDown()
        turtle.forward()
        turtle.turnLeft()
    end
end
calmelb #8
Posted 14 January 2013 - 06:27 PM
Ok, thanks for that! I will use this info for the future! Thanks!