Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Annalyns Infiltration] concept exercise #782

Merged
merged 8 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@
],
"prerequisites": [],
"status": "wip"
},
{
"slug": "annalyns-infiltration",
"name": "Annalyns Infiltration",
"uuid": "603c7f88-5250-427e-b5d8-f8306fdd9b03",
"concepts": [
"booleans"
],
"prerequisites": [
"basics"
],
"status": "wip"
}
],
"practice": [
Expand Down
23 changes: 23 additions & 0 deletions exercises/concept/annalyns-infiltration/.docs/hints.md
colinleach marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Hints

## General

- There are three [boolean operators][boolean-operators] to work with boolean values.
- Multiple operators can be combined in a single expression.

## 1. Check if a fast attack can be made

- The logical NOT operator (`!`) can be placed before an expression to negate its value.

## 2. Check if the group can be spied upon

- Boolean operators are typically used to evaluate whether two or more expressions are true or not true.

## 3. Check if the prisoner can be signaled

- Boolean operators execute in the order of their precedence (from highest to lowest): `!`, `&&`, `||`.
- In general, use of parentheses is encouraged to make your intention clearer.
- For more details check out the Operator Precedence section on the [official Julia documentation][operator-precedence].

[boolean-operators]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators
[operator-precedence]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity
85 changes: 85 additions & 0 deletions exercises/concept/annalyns-infiltration/.docs/instructions.md
colinleach marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Instructions

In this exercise, you'll be writing some logic for a video 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 is kidnapped while searching for berries in the forest.
Annalyn will try to find and free her friend, optionally taking her dog with her on this quest.

Annalyn eventually finds the camp in which her friend is imprisoned and it turns out there are two kidnappers: a mighty knight and a cunning archer.

The player is presented with some options for what to do next.
For each of the four possible options you need to write a function that tells the game whether it should show that option or not.

## 1. Check if the 'Fast Attack' option should be shown

If the knight is sleeping, then Annalyn will be able to make a quick attack into the camp before he can wake up properly and get his armour on.

Implement a function named `can_do_fast_attack` that takes a boolean value which indicates if the knight is awake.
This function returns `true` if the 'Fast Attack' action is available based on the state of the character. Otherwise, returns `false`:

```julia-repl
julia> knight_awake = true

julia> can_do_fast_attack(knight_awake)
false
```

## 2. Check if the 'Spy' option should be shown

The group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.

Implement a function named `can_spy` that takes three boolean values, indicating if the knight, archer and prisoner, respectively, are awake.
The function returns `true` if the 'Spy' action is available based on the state of the characters.
Otherwise, returns `false`:

```julia-repl
# Output suppressed from the next 3 lines
julia> knight_awake = false
julia> archer_awake = true
julia> prisoner_awake = false

julia> can_spy(knight_awake, archer_awake, prisoner_awake)
true
```

## 3. Check if the 'Signal Prisoner' option should be shown

The prisoner can be signalled using bird sounds if she is awake and the archer is sleeping.
If the archer is awake then can't be safely signaled because the archer is also trained in bird signalling.

Implement a function named `can_signal_prisoner` that takes two boolean values, indicating if the archer and prisoner, respectively, are awake.
The function returns `true` if the 'Signal Prisoner' action is available based on the state of the characters.
Otherwise, returns `false`:

```julia-repl
julia> archer_awake = false
julia> prisoner_awake = true

julia> can_signal_prisoner(archer_awake, prisoner_awake)
true
```

## 4. Check if the 'Free Prisoner' option should be shown

Annalyn can try sneaking into the camp to free her friend. This is a risky thing to do and can only succeed in one of two ways:

- If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep.
The knight is scared of the dog and the archer will not have time to get ready before Annalyn and her friend can escape.

- If Annalyn does not have her dog then she and the prisoner must be very sneaky!
Annalyn can free the prisoner if they are awake and the knight and archer are both sleeping, but if he prisoner is sleeping they can't be rescued: they would be startled by Annalyn's sudden appearance and wake up the knight and archer.

Implement a function named `can_free_prisoner` that takes four boolean values.
The first three parameters indicate if the knight, archer and prisoner, respectively, are awake.
The last parameter indicates if Annalyn's pet dog is present.
The function returns `true` if the 'Free Prisoner' action is available based on the state of the characters. Otherwise, it returns `false`:

```julia-repl
julia> knight_awake = false
julia> archer_awake = true
julia> prisoner_awake = false
julia> dog_present = false

julia> can_free_prisoner(knight_awake, archer_awake, prisoner_awake, dog_present)
false
```
58 changes: 58 additions & 0 deletions exercises/concept/annalyns-infiltration/.docs/introduction.md
colinleach marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Introduction

## Booleans in Julia

True or false values are represented by the `Bool` type.
It contains only two values: `true` and `false`.

```julia-repl
julia> true
true

julia> false
false
```

## Boolean Operators

There are three Boolean operators in Julia.

`&&` is Boolean "and".
It evaluates to `true` if the expressions on *both* sides of `&&` are `true`.

```julia-repl
julia> true && true
true

julia> true && false
false
```

`||` is Boolean "or".
It evaluates to `true` if an expression on *either* side of `||` is `true`.

```julia-repl
julia> true || true
true

julia> false || true
true
```

`!` is Boolean "not".
It exchanges `true` and `false` values.

```julia-repl
julia> !true
false

julia> !false
true
```

For longer and more complicated expressions, it is best to use parentheses to make your intention clear.

```julia-repl
julia> (true || false) && (false && true)
false
```
7 changes: 7 additions & 0 deletions exercises/concept/annalyns-infiltration/.docs/source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Source

This exercise is based on the ["Booleans" exercise][source] of the Javascript track, designed by [Rishi Kothari][rishiosaur] and [Derk-Jan Karrenbeld][sleepless].

[source]: https://github.com/exercism/v3/tree/1b7c392e1cc576f3948ed55216611c42fabb19b1/languages/javascript/exercises/concept/booleans
[rishiosaur]: https://github.com/rishiosaur
[sleepless]: https://github.com/SleeplessByte
26 changes: 26 additions & 0 deletions exercises/concept/annalyns-infiltration/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"authors": [
"SaschaMann",
"colinleach"
],
"contributors": [
"cmcaine",
"BNAndras"
],
"files": {
"solution": [
"annalyns-infiltration.jl"
],
"test": [
"runtests.jl"
],
"exemplar": [
".meta/exemplar.jl"
]
},
"language_versions": "≥1.0",
"forked_from": [
"javascript/annalyns-infiltration"
],
"blurb": "Learn about boolean expressions by implementing the quest logic for a new RPG."
}
28 changes: 28 additions & 0 deletions exercises/concept/annalyns-infiltration/.meta/exemplar.jl
colinleach marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
can_do_fast_attack(knight_awake)

Return if a fast attack is possible.
"""
can_do_fast_attack(knight_awake) = !knight_awake

"""
can_spy(knight_awake, archer_awake, prisoner_awake)

Return if the spy action is possible.
"""
can_spy(knight_awake, archer_awake, prisoner_awake) = knight_awake || archer_awake || prisoner_awake

"""
can_signal_prisoner(archer_awake, prisoner_awake)

Return if signaling the prisoner is possible.
"""
can_signal_prisoner(archer_awake, prisoner_awake) = prisoner_awake && !archer_awake

"""
can_free_prisoner(knight_awake, archer_awake, prisoner_awake, dog_present)

Return if the prisoner can be freed.
"""
can_free_prisoner(knight_awake, archer_awake, prisoner_awake, dog_present) =
!knight_awake && !archer_awake && prisoner_awake || dog_present && !archer_awake
11 changes: 11 additions & 0 deletions exercises/concept/annalyns-infiltration/annalyns-infiltration.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function can_do_fast_attack(knight_awake)
end

function can_spy(knight_awake, archer_awake, prisoner_awake)
end

function can_signal_prisoner(archer_awake, prisoner_awake)
end

function can_free_prisoner(knight_awake, archer_awake, prisoner_awake, dog_present)
end
71 changes: 71 additions & 0 deletions exercises/concept/annalyns-infiltration/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Test

include("annalyns-infiltration.jl")

# Julia 1.0 compat
# The function definition of eachrow is taken from Julia Base,
# released under the MIT license: https://julialang.org/license
if VERSION < v"1.1"
@eval eachrow(A) = (view(A, i, :) for i in axes(A, 1))
end

@testset verbose = true "tests" begin
@testset "fast attack" begin
@test !can_do_fast_attack(true)
@test can_do_fast_attack(false)
end

@testset "spying" begin
character_state_combinations = Bool[
0 0 0 0;
0 0 1 1;
0 1 0 1;
0 1 1 1;
1 0 0 1;
1 0 1 1;
1 1 1 1;
]

for state in eachrow(character_state_combinations)
@test can_spy(state[1:3]...) == state[4]
end
end

@testset "signaling prisoner" begin
character_state_combinations = Bool[
0 0 0;
0 1 1;
1 0 0;
1 1 0;
]

for state in eachrow(character_state_combinations)
@test can_signal_prisoner(state[1:2]...) == state[3]
end
end

@testset "freeing prisoner" begin
character_state_combinations = Bool[
0 0 0 0 0;
0 0 0 1 1;
0 0 1 0 1;
0 0 1 1 1;
0 1 0 0 0;
0 1 0 1 0;
0 1 1 0 0;
0 1 1 1 0;
1 0 0 0 0;
1 0 0 1 1;
1 0 1 0 0;
1 0 1 1 1;
1 1 0 0 0;
1 1 0 1 0;
1 1 1 0 0;
1 1 1 1 0;
]

for state in eachrow(character_state_combinations)
@test can_free_prisoner(state[1:4]...) == state[5]
end
end
end
Loading