The boolean type in Lua has the two traditional boolean values: true
and false
. These values can be assigned to a variable, combined with logical operators (and
, or
, not
) and used in conditional tests (e.g. in control structures).
local is_true = true
local is_false = false
true and true --> true
true and false --> false
false or true --> true
false or false --> false
not false --> true
not true --> false
-- prints 0
if is_true == true then
print(0)
else
print(1)
end
Conditional tests and the logical operators consider both the boolean false
and nil
as false and anything else as true.
1 and 2 --> 2
nil and 2 --> nil
false and 2 --> false
1 or 2 --> 1
false or 'hello' --> "hello"
nil or false --> false
Both and
and or
uses short-circuit evaluation, which means they evaluate their second operand only when necessary. All operations are evaluated according to the operator precedence, where not
is evaluated before and
and or
.
In this exercise, you need to implement some rules from the classic game Pac-Man.
You have four rules to implement, all related to the game states.
Implement a function that takes in if Pac-Man has a power pellet active and if Pac-Man is touching a ghost and returns a boolean value if Pac-Man is able to eat the ghost. The function should return true
only if Pac-Man has a power pellet active and is touching a ghost.
rules.eat_ghost(false, true)
-- => false
Implement a function that takes in if Pac-Man is touching a power pellet and if Pac-Man is touching a dot and returns a boolean value if Pac-Man scored. The function should return true
if Pac-Man is touching a power pellet or a dot.
rules.score(true, true)
-- => true
Implement a function that takes in if Pac-Man has a power pellet active and if Pac-Man is touching a ghost and returns a boolean value if Pac-Man loses. The function should return true
if Pac-Man is touching a ghost and does not have a power pellet active.
rules.lose(false, true)
-- => true
Implement a function that takes in if Pac-Man has eaten all of the dots, if Pac-Man has a power pellet active and if Pac-Man is touching a ghost and returns a boolean value if Pac-Man wins. The function should return true
if Pac-Man has eaten all of the dots and has not lost based on the arguments defined in part 3.
rules.win(false, true, false)
-- => false
Sign up to Exercism to learn and master Lua with 3 concepts, 100 exercises, and real human mentoring, all for free.