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:
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:
And finally, there came the time to add variety to places and actions:
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.
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.
References
- A very dense Lua reference that I used to learn the language myself.