Learn Lua by writing a "guess the number" game | Opensource.com

Learn Lua by writing a "guess the number" game

Get to know Lua, a dynamically typed, lightweight, efficient, and embeddable scripting language, by programming a simple game.

Puzzle pieces coming together to form a computer screen
Image by : 

Opensource.com

x

Subscribe now

Get the highlights in your inbox every week.

If you're a fan of scripting languages like Bash, Python, or Ruby, you might find Lua interesting. Lua is a dynamically typed, lightweight, efficient, and embeddable scripting language with an API to interface with C. It runs by interpreting bytecode with a register-based virtual machine, and it can be used for everything from procedural programming to functional programming to data-driven programming. It can even be used for object-oriented programming through the clever use of arrays, or tables, used to mimic classes.

A great way to get a feel for a language is by writing a simple application you're already familiar with. Recently, some Opensource.com correspondents have demonstrated how to use their favorite languages to create a number-guessing game. Lua is one of my favorites, so here's my Lua version of the guessing game.

Install Lua

If you're on Linux, you can install Lua from your distribution's software repository. On macOS, you can install Lua from MacPorts or Homebrew. On Windows, you can install Lua from Chocolatey.

Once you have Lua installed, open your favorite text editor and get ready to code.

Lua code

First, you must set up a pseudo-random number generator, so your player has something unpredictable to try to guess. This is a two-step process: first, you start a random seed based on the current time, and then you select a number within the range of 1 to 100:

math.randomseed(os.time())
number = math.random(1,100)

Next, create what Lua calls a table to represent your player. A table is like an array in Bash or an ArrayList in Java. You can create a table and then assign child variables associated with that table. In this code, player is the table, and player.guess is an entry in that table:

player = {}
player.guess = 0

For the purpose of debugging, print the secret number. This isn't good for the game, but it's great for testing. Comments in Lua are preceded by double dashes:

print(number) --debug

Next, set up a while loop that runs forever upon the condition that the value assigned to player.guess is not equal to the random number established at the start of the code. Currently, player.guess is set to 0, so it is not equal to number. Lua's math operator for inequality is ~=, which is admittedly unique, but you get used to it after a while.

The first thing that happens during this infinite loop is that the game prints a prompt so that the player understands the game.

Next, Lua pauses and waits for the player to enter a guess. Lua reads from files and standard in (stdin) using the io.read function. You can assign the results of io.read to a variable that is dynamically created in the player table. The problem with the player's input is that it is read as a string, even if it's a number. You can convert this input to an integer type using the tonumber() function, assigning the result back to the player.guess variable that initially contained 0:

while ( player.guess ~= number ) do
  print("Guess a number between 1 and 100")
  player.answer = io.read()
  player.guess = tonumber(player.answer)

Now that player.guess contains a new value, it's compared to the random number in an if statement. Lua uses the keywords if, elseif, and else and terminates the statement with the keyword end:

  if ( player.guess > number ) then
    print("Too high")
  elseif ( player.guess < number) then
    print("Too low")
  else
    print("That's right!")
    os.exit()
  end
end

At the end, the function os.exit() closes the application upon success and the keyword end is used twice: once to end the if statement and again to end the while loop.

Run the application

Run the game in a terminal:

$ lua ./guess.lua
96
Guess a number between 1 and 100
1
Too low
Guess a number between 1 and 100
99
Too high
Guess a number between 1 and 100
96
That's right!

That's it!

Intuitive and consistent

As you may be able to tell from this code, Lua is sublimely consistent and fairly intuitive. Its table mechanism is a refreshing way of associating data, and its syntax is minimalistic and efficient. There are few wasted lines in Lua code; in fact, at least one pair of lines in this example could be optimized further, but I wanted to demonstrate data conversion as its own step (maybe you can find the two lines I'm referring to and restructure them).

Lua is a pleasure to use, and its documentation is a pleasure to read, mostly because there's just not that much to it. You'll learn the core language in no time, and then you'll be free to explore LuaRocks to discover all the great libraries others have contributed to make your time with Lua even easier. "Lua" means "moon" in Portuguese, so give it a try tonight.

Learning and studying technology is the key to success

Writing simple games is a fun way to learn a new programming language. Put that principle to work to get started with Java.
Python programming language logo with question marks

Explore Python (and other programming languages) by writing a "guess the number" game.
bash logo on green background

Programming a simple game is a great way to practice a new language and compare it against others you know.

Topics

About the author

Seth Kenlon
Seth Kenlon - Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time. He is one of the maintainers of the Slackware-based multimedia production project Slackermedia.