Booleans in Cairo are represented by the predeclared Boolean type bool, which values can be either true or false.
let speeding = true; // Boolean variable 'speeding' set to 'true'
let has_error = false; // Boolean variable 'has_error' set to 'false'
Cairo supports three logical operators that can evaluate expressions down to Boolean values, returning either true or false.
| Operator | What it means |
|---|---|
&& (and) |
It is true if both statements are true. |
|| (or) |
It is true if at least one statement is true. |
! (not) |
It is true only if the statement is false. |
In this exercise, you'll be implementing the quest logic for a new RPG game a friend is developing. The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. Unfortunately, disaster strikes, as her best friend was kidnapped while searching for berries in the forest. Annalyn will try to find and free her best friend, optionally taking her dog with her on this quest.
After some time spent following her best friend's trail, she finds the camp in which her best friend is imprisoned. It turns out there are two kidnappers: a mighty knight and a cunning archer.
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters found in the forest and whether Annalyn's pet dog is present or not.
Define the can_fast_attack() function that takes a boolean value that indicates if the knight is awake.
This function returns true if a fast attack can be made based on the state of the knight.
Otherwise, returns false:
let knight_awake = true;
can_fast_attack(knight_awake); // false
Define the can_spy() function that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake.
The function returns true if the group can be spied upon, based on the state of the three characters.
Otherwise, returns false:
let knight_awake = false;
let archer_awake = true;
let prisoner_awake = false;
can_spy(knight_awake, archer_awake, prisoner_awake); // true
Define the can_signal_prisoner() function that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake.
The function returns true if the prisoner can be signaled, based on the state of the two characters.
Otherwise, returns false:
let archer_awake = false;
let prisoner_awake = true;
can_signal_prisoner(archer_awake, prisoner_awake); // true
Define the can_free_prisoner() function that takes four boolean values.
The first three parameters indicate if the knight, archer and the prisoner, respectively, are awake.
The last parameter indicates if Annalyn's pet dog is present.
The function returns true if the prisoner can be freed based on the state of the three characters and the presence of Annalyn's pet dog.
Otherwise, it returns false:
let knight_awake = false;
let archer_awake = true;
let prisoner_awake = false;
let dog_present = false;
can_free_prisoner(knight_awake, archer_awake, prisoner_awake, dog_present); // false
Sign up to Exercism to learn and master Cairo with 25 concepts68 exercises, and real human mentoring, all for free.