Booleans in Clojure are represented by true
or false
.
Predicate functions (functions which return a boolean) will typically end with a question mark (?
), but this is by convention only.
The core library includes functions for logical operators such as not
, and
, and or
.
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.
Implement the can-fast-attack?
function, which takes a boolean value indicating whether the knight is awake. The function returns true
if a fast attack can be made based on the state of the knight. Otherwise, it returns false
:
(def knight-awake? true)
(can-fast-attack? knight-awake?)
;;=> false
Implement the can-spy?
function, which takes three boolean values indicating whether 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, it returns false
:
(def knight-awake? false)
(def archer-awake? true)
(def prisoner-awake? false)
(can-spy? knight-awake? archer-awake? prisoner-awake?)
;;=> true
Implement the can-signal-prisoner?
function, which takes two boolean values indicating whether the archer and the prisoner, respectively, are awake. The function returns true
if the prisoner can be signalled based on the state of the two characters. Otherwise, it returns false
:
(def archer-awake? false)
(def prisoner-awake? true)
(can-signal-prisoner? archer-awake? prisoner-awake?)
;;=> true
Implement the can-free-prisoner?
function, which takes four boolean values. The first three parameters indicate whether the knight, archer and the prisoner, respectively, are awake. The last parameter indicates whether 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 Annalyn's pet dog presence. Otherwise, it returns false
:
(def knight-awake? false)
(def archer-awake? true)
(def prisoner-awake? false)
(def dog-present? false)
(can-free-prisoner? knight-awake? archer-awake? prisoner-awake? dog-present?)
;;=> false
Sign up to Exercism to learn and master Clojure with 12 concepts, 85 exercises, and real human mentoring, all for free.