Jump to content




"For" statement help.


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

#1 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 03 August 2012 - 04:43 PM

I have been trying to condense my REALLY long border function, but I am having trouble with the math... I want the top of the border to look like this: +-----------+ But have it expanded to fit the entire screen. So far, I am dubious about the math I set up, so if anyone can help me figure this out, it would be great!
Code here:
function border2()
term.setCursorPos(1,1)
write("+")
  for 2,mX - 1 do
	term.setCursorPos(mX,1)
	write("-")
  end
term.setCursorPos(mX,1)
write("+")
end
I do know to use
mX, mY = term.getSize()
to actually define the variables I used. I also want this scheme to wrap around the entire screen. Like this:
+--------+
|		|
|		|
+--------+


#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 03 August 2012 - 06:15 PM

This should work:
local w, h = term.getSize() -- get terminal size (width, height)
local s = "+"..string.rep("-", w - 2).."+" -- create top and bottom line string
-- draw top line
term.setCursorPos(1, 1)
term.write(s)
-- draw sides
for y = 2, h - 1 do
  term.setCursorPos(1, y)
  term.write("|")
  term.setCursorPos(w, y)
  term.write("|")
end
-- draw bottom line
term.setCursorPos(1, h)
term.write(s)
If there's something you don't understand, just ask and we'll try to answer :P/>

#3 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 03 August 2012 - 07:19 PM

A couple questions:
1. Would this just be as a function? Or would I want it to be part of the code?
2. I understand the code, except for "string.rep". What exactly does that do?

#4 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 03 August 2012 - 07:35 PM

1. It's a block of code, you can put it inside of a function or directly on the code (inside a loop or whatever). I think it's better to put it in a function, so you can use it multiple times without rewriting/copying it.
2. string.rep is a standard lua function. This is a good tutorial about the string functions, it should be enoguh to understand that (and other) function.

#5 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 03 August 2012 - 07:52 PM

Thanks for the link! *BOOKMARKED*





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users