Jump to content




Tower Building turtle


  • You cannot reply to this topic
12 replies to this topic

#1 UP844

  • Members
  • 21 posts

Posted 05 February 2013 - 03:47 PM

I'm programming a turtle to build a tower. I could probably do it 4 times as fast myself but thats not as fun. Anyway I'm using for loops a lot. I don't want to edit the coding everytime I want to change the height of the tower. First I'm having it build a base with:
[/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif]turtle.forward()[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif]turtle.up()[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif]for i = 1, (tower height) do[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif] turtle.placeDown()[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif] turtle.forward()[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif] turtle..placeDown()
 turtle.forward()[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif] turtle.placeDown()[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif] turtle.turnRight()
 turtle.forward()
 turtle.placeDown()
 turtle.forward()
 turtle.placeDown()
 turtle.turnRight()
 turtle.forward()[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif] turtle.placeDown()
 turtle.forward()
 turtle.placeDown
 turtle.turnRight()[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif] turtle.forward()
 turtle.placeDown()
 turtle.forward()
 turtle.turnRight()
 turtle.up()[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif]end[/font][/font]
[font=courier new,courier,monospace][font=arial,helvetica,sans-serif]
[/font]
After this there is some more stuff to make the top of the tower look cooler. But is there anyway that I could replace the (tower height) with a variable and when I execute the program, have to type a number afterwards that will equal the variable? I'm just starting out on programming with Lua and I'm really bad with programming in general.

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 February 2013 - 03:52 PM

side ntoe: use [code][/code] tags

Something like this would do it
local args = {...} -- this gets the runtime args as long as its not in a function
local height = tonumber(args[1])
local width = tonumber(args[2])

for i = 1, height do
  for x = 1, 4 do -- we have 4 sides
	for y = 1, width - 1 do -- we need to -1 so it lines up and is the actual width
	  turtle.forward()
	  turtle.placeDown()
	  turtle.turnRight()
	end
  end
  turtle.up()
end


#3 UP844

  • Members
  • 21 posts

Posted 06 February 2013 - 03:34 AM

View PostTheOriginalBIT, on 05 February 2013 - 03:52 PM, said:

side ntoe: use [code][/code] tags

Something like this would do it
local args = {...} -- this gets the runtime args as long as its not in a function
local height = tonumber(args[1])
local width = tonumber(args[2])

for i = 1, height do
  for x = 1, 4 do -- we have 4 sides
	for y = 1, width - 1 do -- we need to -1 so it lines up and is the actual width
	  turtle.forward()
	  turtle.placeDown()
	  turtle.turnRight()
	end
  end
  turtle.up()
end
And to run the program would it be (program name) (tower height)? And I'll edit the code thing.

#4 UP844

  • Members
  • 21 posts

Posted 06 February 2013 - 03:44 AM

I'm programming a turtle to build a tower. I could probably do it 4 times as fast myself but thats not as fun. Anyway I'm using for loops a lot. I don't want to edit the coding everytime I want to change the height of the tower. First I'm having it build a base with:
turtle.forward()
turtle.up()
for i = 1, (tower height)
 turtle.placeDown()
 turtle.forward()
 turtle.placeDown()
 turtle.forward()
 turtle.placeDown()
 turtle.turnRight()
 turtle.forward()
 turtle.placeDown()
 turtle.forward()
 turtle.placeDown()
 turtle.turnRight()
 turtle.forward()
 turtle.placeDown()
 turtle.forward()
 turtle.placeDown()
 turtle.turnRight()
 turtle.forward()
 turtle.placeDown()
 turtle.forward()
 turtle.turnRight()
 turtle.up()
end
After this there is some more stuff to make the top of the tower look cooler. But is there anyway that I could replace the (tower height) with a variable and when I execute the program, have to type a number afterwards that will equal the variable? I'm just starting out on programming with Lua and I'm really bad with programming in general.

#5 Goof

  • Members
  • 751 posts

Posted 06 February 2013 - 04:49 AM

Well this should work:

edit... putted the code on pastebin (easier to read)
link: http://pastebin.com/9pVBGHWh

Edited by Mikk809h, 06 February 2013 - 04:56 AM.


#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 06 February 2013 - 04:53 AM

View PostUP844, on 06 February 2013 - 03:34 AM, said:

And to run the program would it be (program name) (tower height)? And I'll edit the code thing.
with the program i supplied there it would be
<program name> <tower height> <tower width>

if you didn't want width you could replace line 3 with this
local width = 4
or whatever width you wish it to be.

#7 UP844

  • Members
  • 21 posts

Posted 06 February 2013 - 04:32 PM

View PostTheOriginalBIT, on 06 February 2013 - 04:53 AM, said:

View PostUP844, on 06 February 2013 - 03:34 AM, said:

And to run the program would it be (program name) (tower height)? And I'll edit the code thing.
with the program i supplied there it would be
<program name> <tower height> <tower width>

if you didn't want width you could replace line 3 with this
local width = 4
or whatever width you wish it to be.
Wait I'm running this on an older version of Lua since im using Tekkit it says tower:5: 'for' limit must be a number. Is this because of the version of Lua I'm using?

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 06 February 2013 - 04:42 PM

View PostUP844, on 06 February 2013 - 04:32 PM, said:

Wait I'm running this on an older version of Lua since im using Tekkit it says tower:5: 'for' limit must be a number. Is this because of the version of Lua I'm using?
Not one bit... it probably means that this line
local height = tonumber(args[1])
you have actually typed out like this
local height = args[1]
yes?

#9 UP844

  • Members
  • 21 posts

Posted 07 February 2013 - 11:05 AM

View PostTheOriginalBIT, on 06 February 2013 - 04:42 PM, said:

View PostUP844, on 06 February 2013 - 04:32 PM, said:

Wait I'm running this on an older version of Lua since im using Tekkit it says tower:5: 'for' limit must be a number. Is this because of the version of Lua I'm using?
Not one bit... it probably means that this line
local height = tonumber(args[1])
you have actually typed out like this
local height = args[1]
yes?
I did exactly what you did I've tried it several times I have no idea what's wrong.

#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 February 2013 - 03:06 PM

View PostUP844, on 07 February 2013 - 11:05 AM, said:

I did exactly what you did I've tried it several times I have no idea what's wrong.
The code I supplied is working perfectly fine for me... run it like this and see what happens
tower 5 5


#11 UP844

  • Members
  • 21 posts

Posted 08 February 2013 - 03:24 PM

View PostTheOriginalBIT, on 07 February 2013 - 03:06 PM, said:

View PostUP844, on 07 February 2013 - 11:05 AM, said:

I did exactly what you did I've tried it several times I have no idea what's wrong.
The code I supplied is working perfectly fine for me... run it like this and see what happens
tower 5 5
Still not working. If I put something in my cd network file in my .tekkit folder what do I have to save it as will notepad work?

#12 ChunLing

  • Members
  • 2,027 posts

Posted 08 February 2013 - 07:50 PM

Are you using this code?

View PostTheOriginalBIT, on 05 February 2013 - 03:52 PM, said:

side ntoe: use [code][/code] tags

Something like this would do it
local args = {...} -- this gets the runtime args as long as its not in a function
local height = tonumber(args[1])
local width = tonumber(args[2])

for i = 1, height do
  for x = 1, 4 do -- we have 4 sides
	for y = 1, width - 1 do -- we need to -1 so it lines up and is the actual width
	  turtle.forward()
	  turtle.placeDown()
	  turtle.turnRight()
	end
  end
  turtle.up()
end
There are a number of places where you could mess this up. What is your exact error?

A missing tonumber() wouldn't really matter, since both for parameters and arithmetic automatically tonumber (but it is better style to tonumber things if they're numbers). Your previous error must have been because you didn't supply all the necessary command line parameters...you can put in a default value by editing those lines like so:
local height = tonumber(args[1]) or 5 --default height value
local width = tonumber(args[2])  or 3 --default width value
That way if you omit a command line parameter (or supply something that isn't convertible to number) the program doesn't error.

Anyway, you need to post the exact code you're using and the exact error (or other unexpected behavior) you're seeing. Don't just keep saying "it doesn't work". We have no real idea what "it" is, and we have no idea what "doesn't work" means.

Looking over this code, it does indeed seem odd. It should be:
local args = {...}
local height = tonumber(args[1]) or 5
local width = tonumber(args[2]) or 3

for i = 1, height do
    for x = 1, 4 do
        for y = 1, width-1 do
            turtle.forward()
            turtle.placeDown()
        end
        turtle.turnRight()
    end
    turtle.up()
end
The other code would have produced a 2x2 tower...in time...with a lot of running around in circles (okay, tight squares).

Edited by ChunLing, 08 February 2013 - 07:56 PM.


#13 UP844

  • Members
  • 21 posts

Posted 09 February 2013 - 12:52 PM

View PostChunLing, on 08 February 2013 - 07:50 PM, said:

Are you using this code?

View PostTheOriginalBIT, on 05 February 2013 - 03:52 PM, said:

side ntoe: use [code][/code] tags

Something like this would do it
local args = {...} -- this gets the runtime args as long as its not in a function
local height = tonumber(args[1])
local width = tonumber(args[2])

for i = 1, height do
  for x = 1, 4 do -- we have 4 sides
	for y = 1, width - 1 do -- we need to -1 so it lines up and is the actual width
	  turtle.forward()
	  turtle.placeDown()
	  turtle.turnRight()
	end
  end
  turtle.up()
end
There are a number of places where you could mess this up. What is your exact error?

A missing tonumber() wouldn't really matter, since both for parameters and arithmetic automatically tonumber (but it is better style to tonumber things if they're numbers). Your previous error must have been because you didn't supply all the necessary command line parameters...you can put in a default value by editing those lines like so:
local height = tonumber(args[1]) or 5 --default height value
local width = tonumber(args[2])  or 3 --default width value
That way if you omit a command line parameter (or supply something that isn't convertible to number) the program doesn't error.

Anyway, you need to post the exact code you're using and the exact error (or other unexpected behavior) you're seeing. Don't just keep saying "it doesn't work". We have no real idea what "it" is, and we have no idea what "doesn't work" means.

Looking over this code, it does indeed seem odd. It should be:
local args = {...}
local height = tonumber(args[1]) or 5
local width = tonumber(args[2]) or 3

for i = 1, height do
	for x = 1, 4 do
		for y = 1, width-1 do
			turtle.forward()
			turtle.placeDown()
		end
		turtle.turnRight()
	end
	turtle.up()
end
The other code would have produced a 2x2 tower...in time...with a lot of running around in circles (okay, tight squares).
First off sorry for the saying "it doesn't work" I would've specified but I didn't feel the need since it was giving me the same error report I will next time. And thank you so much the code is working now :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users