Skip to content

Commit f6ec10a

Browse files
dectoriHiD
authored andcommitted
Add annalyns-infiltration concept exercise
1 parent 6df7da4 commit f6ec10a

File tree

16 files changed

+780
-0
lines changed

16 files changed

+780
-0
lines changed

concepts/booleans/about.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Booleans in Kotlin are represented by the `Boolean` type, which values can be either `true` or `false`.
2+
3+
Kotlin supports three built-in [boolean operators][reference]: `!` (negation aka NOT), `&&` (lazy conjunction aka AND), and `||` (lazy disjunction aka OR). The `&&` and `||` operators use _short-circuit evaluation_, which means that the right-hand side of the operator is only evaluated when needed.
4+
5+
```kotlin
6+
true || false // => true
7+
true && false // => false
8+
```
9+
10+
The three boolean operators each have a different [_operator precedence_][precedence]. As a consequence, they are evaluated in this order: `!` first, `&&` second, and finally `||`. If you want to 'escape' these rules, you can enclose a boolean expression in parentheses (`()`), as the parentheses have an even higher operator precedence.
11+
12+
```kotlin
13+
!true && false // => false
14+
!(true && false) // => true
15+
```
16+
17+
[reference]: https://kotlinlang.org/docs/reference/basic-types.html#booleans
18+
[precedence]: https://kotlinlang.org/docs/reference/grammar.html#expressions

concepts/booleans/links.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"url": "https://kotlinlang.org/docs/reference/basic-types.html#booleans",
4+
"description": "reference"
5+
},
6+
{
7+
"url": "https://kotlinlang.org/docs/reference/grammar.html#expressions",
8+
"description": "precedence"
9+
}
10+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## General
2+
3+
- There are three [boolean operators][reference] to work with boolean values.
4+
- Multiple operators can be combined in a single expression.
5+
6+
## 1. Check if a fast attack can be made
7+
8+
- The [boolean operators][reference] can also be applied to boolean parameters.
9+
10+
[reference]: https://kotlinlang.org/docs/reference/basic-types.html#booleans
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
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.
2+
3+
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.
4+
5+
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
6+
7+
- Fast attack: a fast attack can be made if the knight is sleeping, as it takes time for him to get his armor on, so he will be vulnerable.
8+
- Spy: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.
9+
- Signal prisoner: the prisoner can be signalled using bird sounds if the prisoner is awake and the archer is sleeping, as archers are trained in bird signaling so they could intercept the message.
10+
- Free prisoner: if the prisoner is awake and the other two characters are sleeping, a sneaky entry into the camp can free the prisoner. This won't work if the prisoner is sleeping, as the prisoner will be startled by the sudden appearance of her friend and the knight and archer will be awoken. The prisoner can also be freed if the archer is sleeping and Annalyn has her pet dog with her, as the knight will be scared by the dog and will withdraw, and the archer can't equip his bow fast enough to prevent the prisoner from being freed.
11+
12+
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.
13+
14+
## Tasks
15+
16+
## 1. Check if a fast attack can be made
17+
18+
Implement the `canFastAttack()` method that takes a boolean value that indicates if the knight is awake. This method returns `true` if a fast attack can be made based on the state of the knight. Otherwise, returns `false`:
19+
20+
```kotlin
21+
val knightIsAwake = true
22+
23+
canFastAttack(knightIsAwake)
24+
// => false
25+
```
26+
27+
## 2. Check if the group can be spied upon
28+
29+
Implement the `canSpy()` method that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake. The method returns `true` if the group can be spied upon, based on the state of the three characters. Otherwise, returns `false`:
30+
31+
```kotlin
32+
val knightIsAwake = false
33+
val archerIsAwake = true
34+
val prisonerIsAwake = false
35+
36+
canSpy(knightIsAwake, archerIsAwake, prisonerIsAwake)
37+
// => true
38+
```
39+
40+
## 3. Check if the prisoner can be signalled
41+
42+
Implement the `canSignalPrisoner()` method that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake. The method returns `true` if the prisoner can be signalled, based on the state of the two characters. Otherwise, returns `false`:
43+
44+
```kotlin
45+
val archerIsAwake = false
46+
val prisonerIsAwake = true
47+
48+
canSignalPrisoner(archerIsAwake, prisonerIsAwake)
49+
// => true
50+
```
51+
52+
## 4. Check if the prisoner can be freed
53+
54+
Implement the `canFreePrisoner()` method 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 method 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`:
55+
56+
```kotlin
57+
val knightIsAwake = false
58+
val archerIsAwake = true
59+
val prisonerIsAwake = false
60+
val petDogIsPresent = false
61+
62+
canFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent)
63+
// => false
64+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## booleans
2+
3+
Booleans in Kotlin are represented by the `Boolean` type, which values can be either `true` or `false`.
4+
5+
Kotlin supports three built-in [boolean operators][reference]: `!` (negation aka NOT), `&&` (lazy conjunction aka AND), and `||` (lazy disjunction aka OR). The `&&` and `||` operators use _short-circuit evaluation_, which means that the right-hand side of the operator is only evaluated when needed.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "dector",
5+
"exercism_username": "dector"
6+
}
7+
],
8+
"forked_from": ["fsharp/booleans"]
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Learning objectives
2+
3+
- Know of the existence of the `Boolean` type and its two values.
4+
- Know about boolean operators and how to build logical expressions with them.
5+
- Know of the boolean operator precedence rules.
6+
7+
## Out of scope
8+
9+
- Pattern matching on booleans.
10+
11+
## Concepts
12+
13+
- `booleans`: know of the existence of the `Boolean` type and its two values; know about boolean operators and how to build logical expressions with them; know of the boolean operator precedence rules.
14+
15+
## Prerequisites
16+
17+
This exercise's prerequisites Concepts are:
18+
19+
- `basics`: know how to define methods.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
fun canFastAttack(knightIsAwake: Boolean): Boolean =
2+
!knightIsAwake
3+
4+
fun canSpy(
5+
knightIsAwake: Boolean,
6+
archerIsAwake: Boolean,
7+
prisonerIsAwake: Boolean
8+
): Boolean =
9+
knightIsAwake || archerIsAwake || prisonerIsAwake
10+
11+
fun canSignalPrisoner(archerIsAwake: Boolean, prisonerIsAwake: Boolean): Boolean =
12+
!archerIsAwake && prisonerIsAwake
13+
14+
fun canFreePrisoner(
15+
knightIsAwake: Boolean,
16+
archerIsAwake: Boolean,
17+
prisonerIsAwake: Boolean,
18+
petDogIsPresent: Boolean
19+
): Boolean =
20+
!knightIsAwake &&
21+
!archerIsAwake &&
22+
prisonerIsAwake || !archerIsAwake &&
23+
petDogIsPresent
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
2+
3+
plugins {
4+
kotlin("jvm") version "1.4.10"
5+
}
6+
7+
repositories {
8+
jcenter()
9+
}
10+
11+
dependencies {
12+
testImplementation("org.junit.jupiter:junit-jupiter:5.5.2")
13+
}
14+
15+
tasks.withType<Test> {
16+
useJUnitPlatform()
17+
testLogging {
18+
exceptionFormat = TestExceptionFormat.FULL
19+
events("passed", "failed", "skipped")
20+
}
21+
}
54.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)