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

"For" statement help.

Started by Cranium, 03 August 2012 - 03:43 PM
Cranium #1
Posted 03 August 2012 - 05: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:

+--------+
|		|
|		|
+--------+
MysticT #2
Posted 03 August 2012 - 07: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/>/>
Cranium #3
Posted 03 August 2012 - 08: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?
MysticT #4
Posted 03 August 2012 - 08: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.
Cranium #5
Posted 03 August 2012 - 08:52 PM
Thanks for the link! *BOOKMARKED*