Teaching programming with Lua: having fun

The first problem you encounter when trying to teach programming to kids is lack of ideas that would make it fun. I went through that too; made up some things on the fly, accidentally came up with others, some borrowed from friends. And then decided to keep them in one place in hope that these could be useful to other people.

Of course, there are plenty of programming languages you can teach. We coded the ideas in Lua simply because the ultimate mid-term goal was to write a mod for Minetest, an open-source Minecraft-like game. These mods were to be written in Lua, so for me, the choice of the language was simple.

Anyway, here are the coded ideas, in no particular order.

Funny stories generator

This one I owe to Ivan (thanks again!). This is a program that teaches tables (arrays), string concatenation and random numbers. The latter I chose not to explain in any details, thinking that in this first program, an eight-year-old had already had enough to digest.

The idea is that the program is supposed to print a funny sentence about two kids doing something and it will print something a little different each time it is run.

The very first program was very static:

name1 ="Nicholas"
name2 = "Annemarie"
where="the cinema"
act="had some tea"
print("Yesterday, "..name1.." and "..name2.." went to "..where.." and "..act)

Still, it introduced variables and string concatenation.

The next one shuffled names by picking two from a list at random with the help of Lua tables, which, for the purpose of this excercise, are 1-based arrays of strings:

-- This magic isn't particularly hard to explain, but I chose not to
-- for  the time being
math.randomseed(os.time())

-- Let's use the ISO-standardized names found in 
-- ISO/IEC 14882:2017(E) - Programming Language C++.
names = {"Nicholas", "Annemarie", "Norah"}

name1 = names[math.random(1,#names)] -- choose names from the list at random
name2 = names[math.random(1,#names)]
where = "to the cinema"
act = "had some tea"
print("Yesterday, "..name1.." and "..name2.." went "..where.." and "..act)

And finally, there came the time to add variety to places and actions:

math.randomseed(os.time())

names = {"Nicholas", "Annemarie", "Norah"}
places = {"rock climbing", "swimming", "to the theatre", "fishing", "to the cinema", "home", "to the zoo", "to school"}
acts   = {"cooked dinner", "slept at their desks", "lost a candy", "caught some fish", "watched cartoons", "fed a lion"}

name1 = names[math.random(1,#names)]
name2 = names[math.random(1,#names)]
where = places[math.random(1,#places)] -- now choose those two at random as well
act = acts [math.random(1,#acts)]
print("Yesterday, "..name1.." and "..name2.." went "..where.." and "..act)

Asking questions

This one’s not particularly funny, but it resembles school tests enough to spark interest in a student. It’s (mildly, I admit) amuzing to be on the other side of those tests once in a while. Also, this was a necessary prelude to a much more interesting excercise.

This program teaches basic I/O and control flow and can be enhanced almost indefinitely to your liking. You can add more questions, scores, repetition, etc, etc.

print('How many tens are there in 300?')
io.write('Input your answer: ')
s=io.read()
if s=='30' then
    print('Correct!')
else
    print('Incorrect.')
    print('The answer is 30 tens.')
end

Tic-tac-toe

After a bit of training, the time came to write the first game. It’s a very simple tic-tac-toe for two people playing by entering coordinates of their next move. I decided not to complicate things with winning conditions, the program finishes when there’s no place left for the next move.

From the teaching perspective, it adds two-dimentional arrays, functions, two kinds of loops and complex boolean expressions.

t={  {".", ".", "."},
     {".", ".", "."},
     {".", ".", "."} 
  }

function print_table()
  for i=1,3 do
    io.write(t[i][1], " ", t[i][2], " ", t[i][3], "\n")
  end
end

print_table()
s="x"

game_over=false
while not game_over do
  print(s, " turn")
  io.write("enter row (1-3): ")
  x=tonumber(io.read())
  io.write("enter column (1-3): ")
  y=tonumber(io.read())

  if x>=1 and x<=3 and y>=1 and y<=3 and t[x][y] == "." then   
    t[x][y]=s

    c=0
    for i=1,3 do
      for j=1,3 do
        if t[i][j] == "." then c=c+1 end
      end
    end
    game_over = (c==0)
    print_table()

    if s=="x" then
      s="0"
    else
      s="x" -- s was 0
    end
  end
end

References

Maxim Kartashev

Maxim Kartashev
Pragmatic, software engineer. Working for Altium Tasking on compilers and tools for embedded systems.