Let's take a break from learning and make something. We'll apply what we have learnt so far to code the exciting game of Rock, Paper, Scissors. Open a new file and code along.
First we need to ask the player for their choice. We can do this by prompting until they enter a value of either r, p, or s.
local function prompt()
allowed = { "r", "p", "s" }
choice = ""
while not contains(allowed, choice) do
io.write("(r)ock, (p)aper, or (s)cissors? ")
choice = io.read()
end
return choice
end
The conditional in the while loop uses a function called contains to see if the input choice is in the allowed table. The problem is this method is not a standard part of Lua, we need to write it ourselves.
contains functionThe simplist way to implement this function is to loop over the values in the table until we find the value. When we do, we set a variable to true and break from the loop. The break keyword says stop running the loop immediately.
local function contains(t, value)
present = false
for _, v in ipairs(t) do
if v == value then
present = true
break
end
end
return present
endWe can use a random number generator as the source of the computer player's choice. We use math.random to pick a number between 1 and 3. We then use this as an index into the map table to get a choice of r, p, or s.
local function generate()
num = math.random(1, 3)
map = { "r", "p", "s" }
return map[num]
endAll that's left to do is determine the outcome of the game by inspecting both choices that have been made. We do that here with an if-elseif-else construct.
local function outcome(player_choice, cpu_choice)
if player_choice == cpu_choice then
return "Draw!"
elseif player_choice == "r" and cpu_choice == "s" then
return "Rock blunts scissors. You win!"
elseif player_choice == "p" and cpu_choice == "r" then
return "Paper wraps rock. You win!"
elseif player_choice == "s" and cpu_choice == "p" then
return "Scissors cut paper. You win!"
else
return "You lose :("
end
endOur program in all its glory. We call the functions we have defined to get the player and computer choices. Then we determine the outcome and print it to the screen.
-- Rock, paper, scissors.
local function contains(t, value)
present = false
for _, v in ipairs(t) do
if v == value then
present = true
break
end
end
return present
end
local function prompt()
allowed = { "r", "p", "s" }
choice = ""
while not contains(allowed, choice) do
io.write("(r)ock, (p)aper, or (s)cissors? ")
choice = io.read()
end
return choice
end
local function generate()
num = math.random(1, 3)
map = {"r", "p", "s" }
return map[num]
end
local function outcome(player_choice, cpu_choice)
if player_choice == cpu_choice then
return "Draw!"
elseif player_choice == "r" and cpu_choice == "s" then
return "Rock blunts scissors. You win!"
elseif player_choice == "p" and cpu_choice == "r" then
return "Paper wraps rock. You win!"
elseif player_choice == "s" and cpu_choice == "p" then
return "Scissors cut paper. You win!"
else
return "You lose :("
end
end
local player_choice = prompt()
local cpu_choice = generate()
io.write(player_choice .. " vs " .. cpu_choice .. "\n")
local outcome = outcome(player_choice, cpu_choice)
io.write(outcome .. "\n")Rewrite the outcome function to use a table to look up the outcome instead of using the if statement. Hint: you can use the combination of player_choice and cpu_choice as the table keys.
Make the game play for 10 rounds instead of just once.
local function outcome(player_choice, cpu_choice)
key = player_choice .. cpu_choice
map = {
rr = "Draw!",
pp = "Draw!",
ss = "Draw!",
rs = "Rock blunts scissors. You win!",
pr = "Paper wraps rock. You win!",
sp = "Scissors cut paper. You win!"
}
return map[key] or "You lose :("
end
for round = 1, 10 do
local player_choice = prompt()
local cpu_choice = generate()
io.write(player_choice .. " vs " .. cpu_choice .. "\n")
local outcome = outcome2(player_choice, cpu_choice)
io.write(outcome .. "\n")
end