q="q=%qprint(q:format(q))"print(q:format(q))
Lua Code Golf
#21
Posted 16 August 2013 - 10:49 PM
#22
Posted 16 August 2013 - 10:55 PM
#24
Posted 16 August 2013 - 11:09 PM
Guidelines:
- The program must be in the form of a function (ex: package(pathToFolder) )
Grim Reaper leading with 233 chars
#25
Posted 16 August 2013 - 11:14 PM
#26
Posted 16 August 2013 - 11:19 PM
The usage is creating your own table, which these functions read and draw with accordingly. The love2d library bump.lua works on the same principle.
Example (changes the button to a random color on click):
os.loadAPI('button') local myButton = { x = 3; y = 2; text = "This is a button!"; bgColor = colors.blue; textColor = colors.lightBlue; } while true do button.draw(myButton) local _, _, mx, my = os.pullEvent('mouse_click') if button.clicked(myButton, mx, my) then button.bgColor = 2 ^ math.random(14) button.textColor = 2 ^ math.random(14) end end
#27
Posted 16 August 2013 - 11:26 PM
lieudusty, on 16 August 2013 - 11:09 PM, said:
Guidelines:
- The program must be in the form of a function (ex: package(pathToFolder) )
Added
Kingdaro, on 16 August 2013 - 11:19 PM, said:
Heh. You've kept yours both functional and clean. I like it Unfortunately for you, I removed a bit of unnecessary code and am now in the lead again with 255 chars, though I don't think it would be difficult for you to catch back up.
#28
Posted 16 August 2013 - 11:27 PM
233 Characters without white space.
EDIT: The path to folder should be the first argument to the program.
#29
Posted 16 August 2013 - 11:32 PM
Grim Reaper, on 16 August 2013 - 11:14 PM, said:
error(("error(%q)"):format("error(%q)"),0)
That doesn't even work, it just prints this:
error("error(%q)")
I think the button API needs clearer requirements - e.g. can the buttons be arbitrary width (Bubba's is always 4 + text length)?
Are exported function names allowed to be shortened to save space? (buttons.draw -> buttons.d etc)
Maybe you could specify the public interface of the API (e.g.
function buttons.new(x, y, width, height, text, fgCol, bgCol) -> buttonObject function buttons.draw(buttonObject) function buttons.check(buttonObject, x, y)
Edit: Here's my button API that allows for arbitrary widths. 281 non-whitespace chars.
t=term local x=function(n)return(" "):rep(n)end new=function(B)B.s=(B.w-B.t:len())/2 B.t=x(math.floor(B.s))..B.t..x(math.ceil(B.s))end rnd=function(B)t.setCursorPos(B.x,B.y)t.setTextColor(B.f)t.setBackgroundColor(B.b)t.write(B.t)end chk=function(B,x,y)return y==B.y and x>=B.x and x<B.x+B.w end
Test program:
term.clear() exitButton = {x=3, y=4, w=19, t="Exit", f=colors.white, b=colors.red} clearButton = {x=3, y=5, w=8, t="Clear", f=colors.blue, b=colors.green} new(exitButton) new(clearButton) rnd(exitButton) rnd(clearButton) while true do local evt,mb,x,y = os.pullEvent("mouse_click") if chk(exitButton, x, y) then break end if chk(clearButton, x, y) then term.setBackgroundColor(colors.black) term.clear() sleep(2) rnd(exitButton) rnd(clearButton) end end
#30
Posted 16 August 2013 - 11:41 PM
function draw(b ) b.draw() end function bounds(b, x, y) if x >= b.x and x <= b.X and y >= b.y and y <= b.Y then return true end end
As long as you pass it the correct data, structured the correct way, it meets the criteria. The user is responsible for creating the function to draw the button, so functions that don't center the text are improper usage. All of the data of the button (including the draw function) is created by the user, so they are allowed to change the colors. There is a function for determining if a click is within the bounds of the button. All three conditions are met at 102 characters (without white space).
#31
Posted 16 August 2013 - 11:41 PM
immibis, on 16 August 2013 - 11:32 PM, said:
Are exported function names allowed to be shortened to save space? (buttons.draw -> buttons.d etc)
Maybe you could specify the public interface of the API (e.g.
function buttons.new(x, y, width, height, text, fgCol, bgCol) -> buttonObject function buttons.draw(buttonObject) function buttons.check(buttonObject, x, y)
Well as it is currently, button padding can be arbitrary or constant as long as the text is centered. That being said, I think there should be a good way to change response requirements based on popular vote. If more than three (3) people up-vote a suggestion to change the response, then I'll change add/remove said suggestion. Please don't reply to it, just upvote. That way I can keep the thread relatively clean
Lyqyd, on 16 August 2013 - 11:41 PM, said:
function draw(b ) b.draw() end function bounds(b, x, y) if x >= b.x and x <= b.X and y >= b.y and y <= b.Y then return true end end
As long as you pass it the correct data, structured the correct way, it meets the criteria. The user is responsible for creating the function to draw the button, so functions that don't center the text are improper usage. All of the data of the button (including the draw function) is created by the user, so they are allowed to change the colors. There is a function for determining if a click is within the bounds of the button. All three conditions are met at 102 characters (without white space).
I didn't even think about that. Very clever!
#32
Posted 16 August 2013 - 11:51 PM
Lyqyd, on 16 August 2013 - 11:41 PM, said:
function draw(b ) b.draw() end function bounds(b, x, y) if x >= b.x and x <= b.X and y >= b.y and y <= b.Y then return true end end
As long as you pass it the correct data, structured the correct way, it meets the criteria. The user is responsible for creating the function to draw the button, so functions that don't center the text are improper usage. All of the data of the button (including the draw function) is created by the user, so they are allowed to change the colors. There is a function for determining if a click is within the bounds of the button. All three conditions are met at 102 characters (without white space).
function d(b)b.d()end function b(b,x,y)return b.b(x,y)endis 53 non-whitespace chars.
Edit: or even (27)
function d(b,...)b.d(...)endwhere d( draws a button and d(b,x,y) checks coordinates and saves the result in b.ok
Edit 2: or even 0 chars
is enough for users to do that (but falls down on "provides a function")
#33
Posted 16 August 2013 - 11:55 PM
I'd say that the button API must not rely on the user to provide any functions, only numbers or strings, and must include a function to check bounds named "check", a function to draw the buttons named "draw", and a function to create a new button named "add" or "new", and allow buttons to be created with custom color backgrounds.
#34
Posted 16 August 2013 - 11:57 PM
#35
Posted 16 August 2013 - 11:59 PM
Edit: Done. With the current requirements, my program is still the #1 I believe. However, I don't think it would take much for Immibis to shave a few characters off of his program and beat mine.
#36
Posted 17 August 2013 - 12:17 AM
local s,T=" ",term function new(B,x,y,w,t,f,B)/>_=(w-t:len())/2 t=s:rep(math.floor(_))..t..s:rep(math.ceil(_)) function B.drw()T.setCursorPos(x,y)T.setTextColor(f)T.setBackgroundColor(b)T.write(t)end function B.chk(X,Y)return y==Y and X>=x and X<x+w end end
Ignore the /> caused by crappy forum software.
Test code:
#37
Posted 17 August 2013 - 12:22 AM
#39
Posted 17 August 2013 - 12:36 AM
http://pastebin.com/BuGmiRrY
Creates a global function package(dirPath) -> packagedString.
Doesn't write it to a file, the caller is responsible for that.
#40
Posted 17 August 2013 - 12:57 AM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users