Tracks
/
Swift
Swift
/
Exercises
/
Wings Quest
Wings Quest

Wings Quest

Learning Exercise

Introduction

Swift provides a built-in type called Bool to represent truth values: true and false.

Logical Operators

Swift provides three logical operators (!, &&, and ||) to combine and evaluate Boolean expressions.

Logical AND (&&)

The and operator (&&) returns true only if both operands are true. If either operand is false, it returns false. Place an expression or Boolean value on each side of the && symbol:

true && true  // true
true && false // false

Logical OR (||)

The or operator (||) returns true if at least one of the operands is true. It returns false only when both operands are false. Place an expression or Boolean value on each side of the || symbol:

true || true   // true
true || false  // true
false || false // false

Logical NOT (!)

The not operator (!) inverts a Boolean value. It returns true if the value is false, and false if the value is true. Place the ! operator immediately before the value or expression:

!true  // false
!false // true

Using Parentheses (())

You can use parentheses to group expressions and control the order of evaluation. In Swift, expressions inside parentheses are evaluated first:

true && false && false || true   // true
true && false && (false || true) // false

Grouping with parentheses also determines how the NOT operator applies to complex expressions:

!true && false   // false (evaluates (!true) first, then false && false)
!(true && false) // true  (evaluates (true && false) first to false, then !false)
Note

Use parentheses when you need to clarify evaluation order or make complex conditions easier to read. Avoid adding unnecessary parentheses when the default precedence is already clear.

Instructions

You are in process of developing the new highly appreciated game Wings Quest. In the game you are a bird that moves around and collects seeds. The player wins by picking up all the seeds. If the player comes in contact with an eagle, then the player will lose all their seeds and lose the game. There is an exception to this rule: the player can have an active power-up that makes them fly higher than other birds.

Your goal is to write some rules that will be used in the game.

1. Define if bird gets bonus points

In the game, the bird will get bonus points if they touch an eagle while having a power-up.

Define the function bonusPoints(powerUpActive:touchingEagle:) that takes two arguments powerUpActive, which holds if the bird has an active power-up, and the argument touchingEagle which holds if the bird is touching an eagle. The function should return true only if the bird has a power-up active and is touching an eagle, and false otherwise.

bonusPoints(powerUpActive: false, touchingEagle: true)
// Returns false

2. Define if bird scores

In the game, the player gets points when picking up a seed or a power-up.

Define the function score(touchingPowerUp:touchingSeed:) that takes two arguments touchingPowerUp, which holds if the bird is touching a power-up, the argument touchingSeed which holds if the bird is touching a seed. The function should return true if the bird is touching a power-up or a seed, and return false otherwise.

score(touchingPowerUp: true, touchingSeed: true)
// Returns true

3. Define if bird loses

Define the function lose(powerUpActive:touchingEagle:) that takes two arguments powerUpActive, which holds if the bird has an active power-up, and the argument touchingEagle which holds if the bird is touching an eagle. The function should return true if the character is touching an eagle and does not have a power-up active, and return false otherwise.

lose(powerUpActive: false, touchingEagle: true)
// Returns true

4. Define if bird wins

Define the win(hasPickedUpAllSeeds:powerUpActive:touchingEagle:) function that takes the arguments:

  • hasPickedUpAllSeeds if the bird has picked up all of the seeds.
  • powerUpActive if the bird has a power-up active.
  • touchingEagle if the bird is touching an eagle.

The function should return true if the bird has gathered all of the seeds and has not lost based on the arguments defined in part 3, and return false otherwise.

win(hasPickedUpAllSeeds: false, powerUpActive: true, touchingEagle: false)
// Returns false
Edit via GitHub The link opens in a new window or tab
Swift Exercism

Ready to start Wings Quest?

Sign up to Exercism to learn and master Swift with 35 concepts116 exercises, and real human mentoring, all for free.