O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

gives the output of:
10
12
14
The variable a is automatically declared as a local variable and is only visi
-
ble within the loop. For loops may be exited using the
break keyword.
The other type of for loop is used to traverse tables. This has a different
syntax.
fork,vintdo
something()
end
The k and v refer to the table ts key-value pair. Here’s an example to clarify
how it works:
data = {a=1, b=2, c=3}
for k, v in data do
print (k, v)
end
Running this snippet of code produces the output:
a1
c3
b2
As you can see, the values are not listed in the expected order. This is
because the order is undefined in Lua; it depends on how the table has been
stored internally.
Rock-Paper-Scissors in Lua
As a simple example to demonstrate some of the syntax of the Lua pro
-
gramming language, here’s some simple code that plays the game
rock-paper-scissors. (The AI is extremely dumb; it just picks randomly.)
-------------------------------------------------------------------------------
--Name: rock_paper_scissors2.lua
--Author: Mat Buckland
--Desc: lua script to implement a rock-paper-scissors game
-------------------------------------------------------------------------------
--[[seed the random number generator]]
To Script, or Not to Script, That Is the Question
| 265
Scripting in Lua
math.randomseed(os.time())
--[[these global variables will hold the scores of the player
and the computer]]
user_score = 0
comp_score = 0
--[[ this table is used to determine who wins which round ]]
lookup = {};
lookup["rock"] = {rock = ”draw”, paper = “lose”, scissors = “win” }
lookup["paper"] = {rock = “win”, paper = ”draw”, scissors = “lose”}
lookup["scissors"] = {rock = “lose”, paper = “win”, scissors = ”draw”}
--[[this function returns the computer's best guess]]
function GetAIMove()
--create a table so we can convert an integer to a play string
local int_to_name = {"scissors", "rock", "paper"}
--get a random integer in the range 1-3 and use it as an index
--into the table we've just made so that the function can return
--a random play
return int_to_name[math.random(3)]
end
--[[this function uses the lookup table to decide the winner and
allocates scores accordingly]]
function EvaluateTheGuesses(user_guess, comp_guess)
print ("user guess... "..user_guess.." comp guess... "..comp_guess)
if (lookup[user_guess][comp_guess] == “win”) then
print ("You Win the Round!")
user_score = user_score + 1
elseif (lookup[user_guess][comp_guess] == “lose”) then
print ("Computer Wins the Round")
comp_score = comp_score + 1
266 | Chapter 6
Scripting in Lua
else
print ("Draw!")
print (lookup[user_guess][comp_guess])
end
end
--[[ main game loop ]]
print ("Enter q to quit game");
print()
loop = true
while loop == true do
--let the user know the current score
print("User: "..user_score.." Computer: "..comp_score)
--grab input from the user via the keyboard
user_guess = io.stdin:read '*l'
--[[declare a table to convert the user's input into a string]]
local letter_to_string = {s = "scissors", r = "rock", p = "paper"}
if user_guess == "q" then
loop = false --quit the game if user enters 'q'
elseif (user_guess == "r") or (user_guess == "p") or (user_guess == "s") then
comp_guess = GetAIMove()
EvaluateTheGuesses(letter_to_string[user_guess], comp_guess)
else
print ("Invalid input, try again")
end
end
Now that you’ve got a feel for the Lua language, let’s get on with what you
really want to know: how to interface Lua with your C/C++ programs.
To Script, or Not to Script, That Is the Question | 267
Scripting in Lua

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required