Welcome, hi, good day, hello, what's up?! I'm the Sketchwhale!
I make games, talk about them, draw comics and post sketches.

fredag den 26. august 2016

Tiny Lua Program for People Who Don't Know Code

I 'sketched' this program yesterday on the train, and thought it'd be fun to share, because it's simple and easy to comprehend for people who are new to code. It's written in Lua, but if you know other languages, it's easy enough to decipher. But since you might not know other programming languages, I thought I'd explain it in light detail. I've whited out the unimportant code, that simply makes the program read aloud itself.

The program instructs you to give it some text (handing it some eggs) and will then scramble whatever input you give it, into garbage and output this (scrambled eggs).

To scramble, it picks a random index in the string _s and inserts this character, yolk, of string _s into a new string eggs. It then concatenates the text before and after yolk into a new string _s, and repeats this action until eggs contains all the characters of s.

It's a silly program, but a little cute, and by using the os.execute() function with the say program (does Linux have this as well?), it gives a little more showmanship.


function Scramble (s)
  local _s = "" 
local eggs = ""
_s = s
for i = 1, s:len() do
rndIndex = math.random(_s:len())
yolk = _s:sub(rndIndex, rndIndex)
_s = _s:sub(1, rndIndex - 1) .. _s:sub(rndIndex + 1)
eggs = eggs .. yolk 
return eggs

function Cook ()
instruction = "\nHand me some eggs, will you?\n\n"
say = "say"
os.execute("say Hand me some eggs, will you?")
sentence = io.read("*line")
os.execute("say '" .. sentence .."'")
eggs = Scramble (sentence)
io.write "\nHere you go\n\n"
os.execute("say Here you go")
io.write(eggs .. "\n")
os.execute("say '" .. eggs .. "'")
io.write("\nWhoops, seems I got some shells in there.\n")
    os.execute("say Whoops, seems I got some shells in there")


Cook ()

Ingen kommentarer:

Send en kommentar