Tracks
/
Lua
Lua
/
Exercises
/
pacman-rules
pacman-rules

pacman-rules

Learning Exercise

Introduction

Booleans

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.

Instructions

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.

1. Define if Pac-Man eats a ghost

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

2. Define if Pac-Man scores

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

3. Define if Pac-Man loses

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

4. Define if Pac-Man wins

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
Edit via GitHub The link opens in a new window or tab
Lua Exercism

Ready to start pacman-rules?

Sign up to Exercism to learn and master Lua with 3 concepts, 100 exercises, and real human mentoring, all for free.