Skip to content

Commit

Permalink
✨ (concepts) Add concept exercise annalyns-infiltration
Browse files Browse the repository at this point in the history
This concept is inspired from the same in csharp track
  • Loading branch information
devkabiir committed Apr 14, 2023
1 parent 399488b commit 9550d96
Show file tree
Hide file tree
Showing 12 changed files with 684 additions and 15 deletions.
56 changes: 42 additions & 14 deletions concepts/booleans/introduction.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
# Introduction

Booleans in Rust are represented by the `bool` type, which values can be either `true` or `false`.
## Booleans

Rust supports six comparison operators: `==` (Equal), `!=` (Not equal), `>` (Greater than), `<` (Less than), `>=`
(Greater than or equal to), `<=` (Less than or equal to). They can be used to evaluate the relationship of non-boolean
values into a boolean value.
As in most other programming languages, a Boolean type in Rust has two possible values: `true` and `false`.

### Declaration
The Boolean type in Rust is specified using `bool`.

```rust
1 > 0 // true
1 < 0 // false
1 == 0 // false
1 != 0 // true
1 >= 0 // true
1 <= 0 // false
let t = true;

let f: bool = false; // with explicit type annotation
```

The main way to use Boolean values is through conditionals, such as an if
expression.
We’ll cover how if expressions work in Rust in later concepts.

### Use as a type
Booleans can be used as a type for a variable, function parameter or return
type.

```rust
let my_variable: bool;

fn my_function(input: bool) -> bool {
// Do something
}

```

Unlike some other languages, `0` is not `false` and non-zero is not `true`.
### Basic Operators
Rust supports AND (&&), OR (||), NOT (!) and EQALITY (==) operators on Booleans

Rust supports two boolean operators: `||` (Or), `&&` (And). They are only evaluated when the left-hand operand does not already
determine the result of the expression. That is, `||` only evaluates its right-hand operand when the left-hand operand evaluates
to `false`, and `&&` only when it evaluates to `true`.
```rust
let a = true;
let b = false;

!a // => false

a == b // => false

a == true // => true

a && b // => false

a || b // => true

```
15 changes: 14 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
},
"exercises": {
"concept": [
{
"slug": "annalyns-infiltration",
"name": "Annalyn's Infiltration",
"uuid": "897fa2ea-c29c-4d4e-9469-ff661a9b838a",
"difficulty": 1,
"concepts": [
"booleans"
],
"prerequisites": [],
"status": "wip"
},
{
"slug": "lucians-luscious-lasagna",
"uuid": "29a2d3bd-eec8-454d-9dba-4b2d7d071925",
Expand All @@ -43,7 +54,9 @@
"concepts": [
"functions"
],
"prerequisites": [],
"prerequisites": [
"booleans"
],
"status": "active"
},
{
Expand Down
13 changes: 13 additions & 0 deletions exercises/concept/annalyns-infiltration/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Hints

## General

- There are three boolean operators<sup>[1] [2]</sup> to work with boolean values.
- Multiple operators can be combined in a single expression.

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

- The boolean operators<sup>[1] [2]</sup> can also be applied to boolean parameters.

[1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#lazy-boolean-operators
[2]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#negation-operators
69 changes: 69 additions & 0 deletions exercises/concept/annalyns-infiltration/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Instructions

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.

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.

Having found the kidnappers, Annalyn considers which of the following actions she can engage in:

- _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.
- _Spy_: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.
- _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.
- _Free prisoner_: Annalyn can try sneaking into the camp to free the prisoner.
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 the prisoner can escape.
- If Annalyn does not have her dog then she and the prisoner must be very sneaky!
Annalyn can free the prisoner if the prisoner is awake and the knight and archer are both sleeping, but if the prisoner is sleeping they can't be rescued: the prisoner would be startled by Annalyn's sudden appearance and wake up the knight and archer.

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.

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

Implement the `can_fast_attack()` function that takes a boolean value that indicates if the knight is awake. This function returns `true` if a fast attack can be made based on the state of the knight. Otherwise, returns `false`:

```rust
let knight_is_awake = true;
can_fast_attack(knight_is_awake);
// => false
```

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

Implement the `can_spy()` function that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake. The function returns `true` if the group can be spied upon, based on the state of the three characters. Otherwise, returns `false`:

```rust
let knight_is_awake = false;
let archer_is_awake = true;
let prisoner_is_awake = false;
can_spy(knight_is_awake, archer_is_awake, prisoner_is_awake);
// => true
```

## 3. Check if the prisoner can be signalled

Implement the `can_signal_prisoner()` function that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake. The function returns `true` if the prisoner can be signalled, based on the state of the two characters. Otherwise, returns `false`:

```rust
let archer_is_awake = false;
let prisoner_is_awake = true;
can_signal_prisoner(archer_is_awake, prisoner_is_awake);
// => true
```

## 4. Check if the prisoner can be freed

Implement the `can_free_prisoner()` function 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 function 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`:

```rust
let knight_is_awake = false;
let archer_is_awake = true;
let prisoner_is_awake = false;
let pet_dog_is_present = false;
can_free_prisoner(knight_is_awake, archer_is_awake, prisoner_is_awake, pet_dog_is_present);
// => false
```
49 changes: 49 additions & 0 deletions exercises/concept/annalyns-infiltration/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Introduction

## Booleans

As in most other programming languages, a Boolean type in Rust has two possible values: `true` and `false`.

### Declaration
The Boolean type in Rust is specified using `bool`.
```rust
let t = true;

let f: bool = false; // with explicit type annotation
```

The main way to use Boolean values is through conditionals, such as an if
expression.
We’ll cover how if expressions work in Rust in later concepts.

### Use as a type
Booleans can be used as a type for a variable, function parameter or return
type.

```rust
let my_variable: bool;

fn my_function(input: bool) -> bool {
// Do something
}

```

### Basic Operators
Rust supports AND (&&), OR (||), NOT (!) and EQALITY (==) operators on Booleans

```rust
let a = true;
let b = false;

!a // => false

a == b // => false

a == true // => true

a && b // => false

a || b // => true

```
8 changes: 8 additions & 0 deletions exercises/concept/annalyns-infiltration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
/target/
**/*.rs.bk

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
22 changes: 22 additions & 0 deletions exercises/concept/annalyns-infiltration/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"forked_from": [
"fsharp/annalyns-infiltration"
],
"blurb": "Learn about booleans while helping Annalyn rescue her friend.",
"authors": [
"devkabiir"
],
"contributors": [],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/annalyns-infiltration.rs"
],
"exemplar": [
".meta/exemplar.rs"
]
}
}
21 changes: 21 additions & 0 deletions exercises/concept/annalyns-infiltration/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Design

## Learning objectives

- Know of the existence of the `bool` type and its two values.
- Know about boolean operators and how to build logical expressions with them.
- Know of the boolean operator precedence rules.

## Out of scope

- Pattern matching on booleans.

## Concepts

- `booleans`: know of the existence of the `bool` type and its two values; know about boolean operators and how to build logical expressions with them; know of the boolean operator precedence rules.

## Prerequisites

This exercise's prerequisites Concepts are:

- none
21 changes: 21 additions & 0 deletions exercises/concept/annalyns-infiltration/.meta/exemplar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pub fn can_fast_attack(knight_is_awake: bool) -> bool {
return !knight_is_awake;
}

pub fn can_spy(knight_is_awake: bool, archer_is_awake: bool, prisoner_is_awake: bool) -> bool {
return knight_is_awake || archer_is_awake || prisoner_is_awake;
}

pub fn can_signal_prisoner(archer_is_awake: bool, prisoner_is_awake: bool) -> bool {
return !archer_is_awake && prisoner_is_awake;
}

pub fn can_free_prisoner(
knight_is_awake: bool,
archer_is_awake: bool,
prisoner_is_awake: bool,
pet_dog_is_present: bool,
) -> bool {
return !knight_is_awake && !archer_is_awake && prisoner_is_awake
|| !archer_is_awake && pet_dog_is_present;
}
4 changes: 4 additions & 0 deletions exercises/concept/annalyns-infiltration/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
edition = "2021"
name = "annalyns_infiltration"
version = "1.0.0"
20 changes: 20 additions & 0 deletions exercises/concept/annalyns-infiltration/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub fn can_fast_attack(_knight_is_awake: bool) -> bool {
unimplemented!("Implement can_fast_attack");
}

pub fn can_spy(_knight_is_awake: bool, _archer_is_awake: bool, _prisoner_is_awake: bool) -> bool {
unimplemented!("Implement can_spy");
}

pub fn can_signal_prisoner(_archer_is_awake: bool, _prisoner_is_awake: bool) -> bool {
unimplemented!("Implement can_signal_prisoner");
}

pub fn can_free_prisoner(
_knight_is_awake: bool,
_archer_is_awake: bool,
_prisoner_is_awake: bool,
_pet_dog_is_present: bool,
) -> bool {
unimplemented!("Implement can_free_prisoner");
}
Loading

0 comments on commit 9550d96

Please sign in to comment.