Crystal has a type known as Bool
.
It represents the values true
and false
.
Crystal has three logical operators (!
, ||
, &&
), which combine Bools and make expressions that produce different values.
&&
)The and operator in Crystal is represented by &&
and returns true
if both values are true
; otherwise, it returns false
.
When using the and operator, one Bool is placed on the right side of the &&
and another on the left side.
true && true
# => true
true && false
# => false
||
)The or operator in Crystal is represented by ||
and returns true
if at least one of the values given is true
.
If both of the values are false
, then it returns false
.
When using the or operator, one Bool should be placed on the right side of the ||
and another on the left.
true || true
# => true
true || false
# => true
false || false
# => false
!
)The not operator in Crystal is represented by !
and returns true
if the given Bool is false
and returns false
if true
is provided.
When using the not operator, one Bool should be placed after the operator (!
).
!true
# => false
!false
# => true
()
)When working with booleans, you can use parentheses to decide which Bools to evaluate first. The result can differ depending on how the parentheses are used. In Crystal, what is in parentheses is evaluated first.
true && false && false || true
# => true
true && false && (false || true)
# => false
Since what is in parentheses is evaluated first, the not operator will apply to the expression inside parentheses in the following example.
!true && false
# => false
!(true && false)
# => true
You should only use parentheses when they affect the result; otherwise, they should be omitted.
You are in the process of developing the new highly appreciated game Crystal Hunter. In the game, you are a character who moves around and collects crystals. The player wins by picking up all the crystals. If the player comes in contact with a bandit, they will lose all their crystals and the game. There is an exception to this rule: the player can have an active power-up that makes them invisible to the bandits.
Your goal is to write some rules that will be used in the game.
In the game, the character will get bonus points if they touch a bandit while having a power-up.
Define the Rules#bonus_points?
method that takes two arguments (if the character has an active power-up and if the character is touching a bandit) and returns a boolean value that tells whether the character will get bonus points.
The method should return true
only if the character has a power-up active and is touching a bandit and false
otherwise.
Rules.new.bonus_points?(false, true)
# => false
The player gets points in the game when picking up a crystal or a power-up.
Define the Rules#score?
method, which takes two arguments (if the character is touching a power-up and if the character is touching a crystal) and returns a boolean value indicating whether the character scored or not.
The method should return true
if the character touches a power-up or a crystal and return false
otherwise.
Rules.new.score?(true, true)
# => true
Define the Rules#lose?
method, which takes two arguments (if the character has a power-up active and if the character is touching a bandit) and returns a boolean value that indicates whether the character loses or not.
The method should return true
if the character touches a bandit and does not have a power-up active and return false
otherwise.
Rules.new.lose?(false, true)
# => true
Define the Rules#win?
method, which takes three arguments (if the character has picked up all of the crystals, if the character has a power-up active, and if the character is touching a bandit) and returns a boolean value indicating whether the character wins or not.
The method should return true
if the character has gathered all crystals and has not lost based on the arguments defined in part 3 and return false
otherwise.
Rules.new.win?(false, true, false)
# => false
Sign up to Exercism to learn and master Crystal with 26 concepts, 133 exercises, and real human mentoring, all for free.