Skip to content

Commit

Permalink
[Wings Quest]: New Bools exercise (#637)
Browse files Browse the repository at this point in the history
* Start on wingqeust

* Add new exercise

* Add exercise to config

* Fix naming

* Fix file naming

* Fix variable name

* Fix test case

* Update the function definetion instructions

* Update example strucuture and fixes

* Various improvements
  • Loading branch information
meatball133 authored Aug 2, 2023
1 parent b8adc41 commit 1d97c2e
Show file tree
Hide file tree
Showing 15 changed files with 496 additions and 40 deletions.
3 changes: 2 additions & 1 deletion concepts/booleans/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"blurb": "Booleans are either true or false. They support NOT, AND, and OR operators.",
"authors": [
"wneumann"
"wneumann",
"meatball133"
],
"contributors": []
}
71 changes: 50 additions & 21 deletions concepts/booleans/about.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,67 @@
# About
# Bools

One of Swift's basic types is the [Boolean type][booleans], called `Bool`. Swift provides two Boolean constant values, `true` and `false`:
Swift has a type known as [`Bool`][bools], it is used to represent the values `true` and `false`.

```swift
let doEaglesFly = true
let doElephantsFly = false
## Logical operators

Swift has 3 [logical operators (`!`, `||`, `&&`)][logical-operators] which are used to combine Bools and make expressions that produce different values.

### And(`&&`)

The [_and_ operator][and] in Swift is represented by `&&` and returns `true` if both values given are `true` otherwise it returns `false`.
When using the _and_ operator, one Bool be placed on the right side of the `&&` and another one on the left side.

```Swift
true && true // true
true && false // false
```

Expressions that evaluate to Boolean values are known as _Boolean Expressions_. Some of the most common Boolean expressions are those built using [comparison operators][comparison-operators]:
### Or(`||`)

The [_or_ operator][or] in Swift is represented by `||` and returns `true` if **at least one** of 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 one on the left side.

```swift
1 + 1 == 2 // evaluates to true
3 + 3 < 5 // evaluates to false
10 >= 10 // evaluates to true
true || true // true
true || false // true
false || false // false
```

Boolean Expressions can be combined using Swift's [logical operators][logical-operators]:
### Not(`!`)

The [_not_ operator][not] in Swift is represented by `!` and returns `true` if the given Bool is `false` and returns `false` if `true` is given.
When using the _not_ operator one Bool should be placed after the operator (`!`).

```swift
2 + 2 == 4 && 6 / 2 <= 2 // evaluates to false
4 > 5 || 1 != 0 // evaluates to true
!(2 * 2 == 2 + 2) // evaluates to false
!true // false
!false // true
```

Note that unlike some languages, Swift doesn't have an logical exclusive-or operator; the `!=` comparison operator is used instead.
## Using parentheses(`()`)

When working with booleans you can use [explicit parentheses][explicit-parentheses] to decide which Bools to evaluate first.
The result can differ depending on how the parentheses are used.
In Swift, what is in parentheses is evaluated first.

```swift
true != true // evaluates to false
(1 == 2) != (7 - 4 == 15 / 5) // evaluates to true
true && false && false || true // true
true && false && (false || true) // false
```

Since what is in parentheses is evaluated first, in the following example, the _not_ operator will apply to the expression inside parentheses.

```Swift
!true && false // false
!(true && false) // true
```

Boolean values are particularly useful when working with [conditional statements][conditionals] such as the if statement
```exercism/note
You should only use parentheses when they affect the result, otherwise, should they be omitted.
```

[booleans]: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID328
[comparison-operators]: https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID70
[logical-operators]: https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID76
[conditionals]: https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID127
[logical-operators]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-Operators
[not]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-NOT-Operator
[and]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-AND-Operator
[or]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-OR-Operator
[explicit-parentheses]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Explicit-Parentheses
[bools]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics#Booleans
68 changes: 52 additions & 16 deletions concepts/booleans/introduction.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,67 @@
# Introduction
# Bools

One of Swift's basic types is the Boolean type, called `Bool`. Swift provides two Boolean constant values, `true` and `false`:
Swift has a type known as [`Bool`][bools], it is used to represent the values `true` and `false`.

```swift
let doEaglesFly = true
let doElephantsFly = false
## Logical operators

Swift has 3 [logical operators (`!`, `||`, `&&`)][logical-operators] which are used to combine Bools and make expressions that produce different values.

### And(`&&`)

The [_and_ operator][and] in Swift is represented by `&&` and returns `true` if both values given are `true` otherwise it returns `false`.
When using the _and_ operator, one Bool be placed on the right side of the `&&` and another one on the left side.

```Swift
true && true // true
true && false // false
```

Expressions that evaluate to Boolean values are known as _Boolean Expressions_. Some of the most common Boolean expressions are those built using comparison operators:
### Or(`||`)

The [_or_ operator][or] in Swift is represented by `||` and returns `true` if **at least one** of 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 one on the left side.

```swift
1 + 1 == 2 // evaluates to true
3 + 3 < 5 // evaluates to false
10 >= 10 // evaluates to true
true || true // true
true || false // true
false || false // false
```

Boolean Expressions can be combined using Swift's logical operators:
### Not(`!`)

The [_not_ operator][not] in Swift is represented by `!` and returns `true` if the given Bool is `false` and returns `false` if `true` is given.
When using the _not_ operator one Bool should be placed after the operator (`!`).

```swift
2 + 2 == 4 && 6 / 2 <= 2 // evaluates to false
4 > 5 || 1 != 0 // evaluates to true
!(2 * 2 == 2 + 2) // evaluates to false
!true // false
!false // true
```

Note that unlike some languages, Swift doesn't have an logical exclusive-or operator; the `!=` comparison operator is used instead.
## Using parentheses(`()`)

When working with booleans you can use [explicit parentheses][explicit-parentheses] to decide which Bools to evaluate first.
The result can differ depending on how the parentheses are used.
In Swift, what is in parentheses is evaluated first.

```swift
true != true // evaluates to false
(1 == 2) != (7 - 4 == 15 / 5) // evaluates to true
true && false && false || true // true
true && false && (false || true) // false
```

Since what is in parentheses is evaluated first, in the following example, the _not_ operator will apply to the expression inside parentheses.

```Swift
!true && false // false
!(true && false) // true
```

```exercism/note
You should only use parentheses when they affect the result, otherwise, should they be omitted.
```

[logical-operators]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-Operators
[not]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-NOT-Operator
[and]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-AND-Operator
[or]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-OR-Operator
[explicit-parentheses]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Explicit-Parentheses
[bools]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics#Booleans
15 changes: 14 additions & 1 deletion concepts/booleans/links.json
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
[]
[
{
"url": "https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics#Booleans",
"description": "Swift Book: Booleans"
},
{
"url": "https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-Operators",
"description": "Swift Book: Logical operators"
},
{
"url": "https://developer.apple.com/documentation/swift/bool",
"description": "Swift docs: Bool"
}
]
11 changes: 10 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
"prerequisites": [],
"status": "active"
},
{
"slug": "wings-quest",
"name": "Wings Quest",
"uuid": "13a703ee-2180-49c6-86c7-7c848d636a3c",
"concepts": [
"booleans"
],
"prerequisites": ["basics"],
"status": "wip"
},
{
"slug": "freelancer-rates",
"name": "Freelancer Rates",
Expand Down Expand Up @@ -65,7 +75,6 @@
"name": "Vexing Vehicle Purchase",
"uuid": "6fa28581-0992-4770-a657-2bb5157db178",
"concepts": [
"booleans",
"conditionals-if",
"conditionals-guard",
"conditionals-switch",
Expand Down
25 changes: 25 additions & 0 deletions exercises/concept/wings-quest/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Hints

## General

- Don't worry about how the arguments are _derived_, focus on combining the arguments to return the intended result.

## 1. Define if character gets bonus points

- You can use [the logical _and_ operator][and] to combine arguments for a result.

## 2. Define if character scores

- You can use [the logical _or_ operator][or] to combine arguments for a result.

## 3. Define if character loses

- You can use [the logical _not_ operator][not] to negate an argument for a result.

## 4. Define if character wins

- You can use the previous methods to combine arguments for a result.

[not]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-NOT-Operator
[and]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-AND-Operator
[or]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-OR-Operator
58 changes: 58 additions & 0 deletions exercises/concept/wings-quest/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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.

```Swift
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.

```Swift
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 an eagle and does not have a power-up active, and return `false` otherwise.

```Swift
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 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.

```Swift
win(HasPickedUpAllSeeds: false, powerUpActive: true, touchingEagle: false)
// Returns false
```
67 changes: 67 additions & 0 deletions exercises/concept/wings-quest/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Bools

Swift has a type known as [`Bool`][bools], it is used to represent the values `true` and `false`.

## Logical operators

Swift has 3 [logical operators (`!`, `||`, `&&`)][logical-operators] which are used to combine Bools and make expressions that produce different values.

### And(`&&`)

The [_and_ operator][and] in Swift is represented by `&&` and returns `true` if both values given are `true` otherwise it returns `false`.
When using the _and_ operator, one Bool be placed on the right side of the `&&` and another one on the left side.

```Swift
true && true // true
true && false // false
```

### Or(`||`)

The [_or_ operator][or] in Swift is represented by `||` and returns `true` if **at least one** of 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 one on the left side.

```swift
true || true // true
true || false // true
false || false // false
```

### Not(`!`)

The [_not_ operator][not] in Swift is represented by `!` and returns `true` if the given Bool is `false` and returns `false` if `true` is given.
When using the _not_ operator one Bool should be placed after the operator (`!`).

```swift
!true // false
!false // true
```

## Using parentheses(`()`)

When working with booleans you can use [explicit parentheses][explicit-parentheses] to decide which Bools to evaluate first.
The result can differ depending on how the parentheses are used.
In Swift, what is in parentheses is evaluated first.

```swift
true && false && false || true // true
true && false && (false || true) // false
```

Since what is in parentheses is evaluated first, in the following example, the _not_ operator will apply to the expression inside parentheses.

```Swift
!true && false // false
!(true && false) // true
```

```exercism/note
You should only use parentheses when they affect the result, otherwise, should they be omitted.
```

[logical-operators]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-Operators
[not]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-NOT-Operator
[and]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-AND-Operator
[or]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Logical-OR-Operator
[explicit-parentheses]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Explicit-Parentheses
[bools]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics#Booleans
5 changes: 5 additions & 0 deletions exercises/concept/wings-quest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func bonusPoints(powerUpActive: Bool, touchingEagle: Bool) -> Bool {
return powerUpActive && touchingEagle
}

func score(touchingPowerUp: Bool, touchingSeed: Bool) -> Bool {
return touchingPowerUp || touchingSeed
}

func lose(powerUpActive: Bool, touchingEagle: Bool) -> Bool {
return !powerUpActive && touchingEagle
}

func win(HasPickedUpAllSeeds: Bool, powerUpActive: Bool, touchingEagle: Bool) -> Bool {
return HasPickedUpAllSeeds && !lose(powerUpActive: powerUpActive, touchingEagle: touchingEagle)
}
Loading

0 comments on commit 1d97c2e

Please sign in to comment.