Browse Source

Exercism: Annalyns Infiltration

master
Julio Biason 3 years ago
parent
commit
0be59671bf
  1. 4
      .gitignore
  2. 23
      csharp/annalyns-infiltration/.exercism/config.json
  3. 1
      csharp/annalyns-infiltration/.exercism/metadata.json
  4. 27
      csharp/annalyns-infiltration/AnnalynsInfiltration.cs
  5. 14
      csharp/annalyns-infiltration/AnnalynsInfiltration.csproj
  6. 313
      csharp/annalyns-infiltration/AnnalynsInfiltrationTests.cs
  7. 39
      csharp/annalyns-infiltration/HELP.md
  8. 12
      csharp/annalyns-infiltration/HINTS.md
  9. 86
      csharp/annalyns-infiltration/README.md

4
.gitignore vendored

@ -11,5 +11,5 @@ _build
.formatter.exs
# C#
obj/**
bin/**
**/obj/**
**/bin/**

23
csharp/annalyns-infiltration/.exercism/config.json

@ -0,0 +1,23 @@
{
"blurb": "Learn about booleans while helping Annalyn rescue her friend.",
"contributors": [
"yzAlvin"
],
"authors": [
"ErikSchierboom"
],
"forked_from": [
"fsharp/annalyns-infiltration"
],
"files": {
"solution": [
"AnnalynsInfiltration.cs"
],
"test": [
"AnnalynsInfiltrationTests.cs"
],
"exemplar": [
".meta/Exemplar.cs"
]
}
}

1
csharp/annalyns-infiltration/.exercism/metadata.json

@ -0,0 +1 @@
{"track":"csharp","exercise":"annalyns-infiltration","id":"3671d2acfd6e47a2ac62efd357fa7def","url":"https://exercism.org/tracks/csharp/exercises/annalyns-infiltration","handle":"JBiason","is_requester":true,"auto_approve":false}

27
csharp/annalyns-infiltration/AnnalynsInfiltration.cs

@ -0,0 +1,27 @@
using System;
static class QuestLogic
{
public static bool CanFastAttack(bool knightIsAwake)
{
return !knightIsAwake;
}
public static bool CanSpy(bool knightIsAwake, bool archerIsAwake, bool prisonerIsAwake)
{
return knightIsAwake || archerIsAwake || prisonerIsAwake;
}
public static bool CanSignalPrisoner(bool archerIsAwake, bool prisonerIsAwake)
{
return !archerIsAwake && prisonerIsAwake;
}
public static bool CanFreePrisoner(bool knightIsAwake, bool archerIsAwake, bool prisonerIsAwake, bool petDogIsPresent)
{
return !archerIsAwake &&
((!knightIsAwake && (prisonerIsAwake || petDogIsPresent))
|| (knightIsAwake && petDogIsPresent))
;
}
}

14
csharp/annalyns-infiltration/AnnalynsInfiltration.csproj

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
</ItemGroup>
</Project>

313
csharp/annalyns-infiltration/AnnalynsInfiltrationTests.cs

@ -0,0 +1,313 @@
using Xunit;
using Exercism.Tests;
public class QuestLogicTests
{
[Fact]
[Task(1)]
public void Cannot_execute_fast_attack_if_knight_is_awake()
{
var knightIsAwake = true;
Assert.False(QuestLogic.CanFastAttack(knightIsAwake));
}
[Fact]
[Task(1)]
public void Can_execute_fast_attack_if_knight_is_sleeping()
{
var knightIsAwake = false;
Assert.True(QuestLogic.CanFastAttack(knightIsAwake));
}
[Fact]
[Task(2)]
public void Cannot_spy_if_everyone_is_sleeping()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = false;
Assert.False(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_everyone_but_knight_is_sleeping()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = false;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_everyone_but_archer_is_sleeping()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = false;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_everyone_but_prisoner_is_sleeping()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = true;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_only_knight_is_sleeping()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = true;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_only_archer_is_sleeping()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = true;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_only_prisoner_is_sleeping()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = false;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(2)]
public void Can_spy_if_everyone_is_awake()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = true;
Assert.True(QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(3)]
public void Can_signal_prisoner_if_archer_is_sleeping_and_prisoner_is_awake()
{
var archerIsAwake = false;
var prisonerIsAwake = true;
Assert.True(QuestLogic.CanSignalPrisoner(archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(3)]
public void Cannot_signal_prisoner_if_archer_is_awake_and_prisoner_is_sleeping()
{
var archerIsAwake = true;
var prisonerIsAwake = false;
Assert.False(QuestLogic.CanSignalPrisoner(archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(3)]
public void Cannot_signal_prisoner_if_archer_and_prisoner_are_both_sleeping()
{
var archerIsAwake = false;
var prisonerIsAwake = false;
Assert.False(QuestLogic.CanSignalPrisoner(archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(3)]
public void Cannot_signal_prisoner_if_archer_and_prisoner_are_both_awake()
{
var archerIsAwake = true;
var prisonerIsAwake = true;
Assert.False(QuestLogic.CanSignalPrisoner(archerIsAwake, prisonerIsAwake));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_everyone_is_awake_and_pet_dog_is_present()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = true;
var petDogIsPresent = true;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_everyone_is_awake_and_pet_dog_is_absent()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = true;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Can_free_prisoner_if_everyone_is_asleep_and_pet_dog_is_present()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = false;
var petDogIsPresent = true;
Assert.True(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_everyone_is_asleep_and_pet_dog_is_absent()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = false;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Can_free_prisoner_if_only_prisoner_is_awake_and_pet_dog_is_present()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = true;
var petDogIsPresent = true;
Assert.True(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Can_free_prisoner_if_only_prisoner_is_awake_and_pet_dog_is_absent()
{
var knightIsAwake = false;
var archerIsAwake = false;
var prisonerIsAwake = true;
var petDogIsPresent = false;
Assert.True(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_archer_is_awake_and_pet_dog_is_present()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = false;
var petDogIsPresent = true;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_archer_is_awake_and_pet_dog_is_absent()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = false;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Can_free_prisoner_if_only_knight_is_awake_and_pet_dog_is_present()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = false;
var petDogIsPresent = true;
Assert.True(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_knight_is_awake_and_pet_dog_is_absent()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = false;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_knight_is_asleep_and_pet_dog_is_present()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = true;
var petDogIsPresent = true;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_knight_is_asleep_and_pet_dog_is_absent()
{
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = true;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Can_free_prisoner_if_only_archer_is_asleep_and_pet_dog_is_present()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = true;
var petDogIsPresent = true;
Assert.True(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_archer_is_asleep_and_pet_dog_is_absent()
{
var knightIsAwake = true;
var archerIsAwake = false;
var prisonerIsAwake = true;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_prisoner_is_asleep_and_pet_dog_is_present()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = false;
var petDogIsPresent = true;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
[Fact]
[Task(4)]
public void Cannot_free_prisoner_if_only_prisoner_is_asleep_and_pet_dog_is_absent()
{
var knightIsAwake = true;
var archerIsAwake = true;
var prisonerIsAwake = false;
var petDogIsPresent = false;
Assert.False(QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent));
}
}

39
csharp/annalyns-infiltration/HELP.md

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit AnnalynsInfiltration.cs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [C# track's documentation](https://exercism.org/docs/tracks/csharp)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [Gitter](https://gitter.im/exercism/xcsharp) is Exercism C# track's Gitter room; go here to get support and ask questions related to the C# track.
- [/r/csharp](https://www.reddit.com/r/csharp) is the C# subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/c%23) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

12
csharp/annalyns-infiltration/HINTS.md

@ -0,0 +1,12 @@
# Hints
## General
- There are three [boolean operators][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 [boolean operators][operators] can also be applied to boolean parameters.
[operators]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators

86
csharp/annalyns-infiltration/README.md

@ -0,0 +1,86 @@
# Annalyn's Infiltration
Welcome to Annalyn's Infiltration on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
Booleans in C# are represented by the `bool` type, which values can be either `true` or `false`.
C# supports three boolean operators: `!` (NOT), `&&` (AND), and `||` (OR).
## 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_: 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.
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 (_static_) `QuestLogic.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`:
```csharp
var knightIsAwake = true;
QuestLogic.CanFastAttack(knightIsAwake);
// => false
```
## 2. Check if the group can be spied upon
Implement the (_static_) `QuestLogic.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`:
```csharp
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = false;
QuestLogic.CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake);
// => true
```
## 3. Check if the prisoner can be signalled
Implement the (_static_) `QuestLogic.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`:
```csharp
var archerIsAwake = false;
var prisonerIsAwake = true;
QuestLogic.CanSignalPrisoner(archerIsAwake, prisonerIsAwake);
// => true
```
## 4. Check if the prisoner can be freed
Implement the (_static_) `QuestLogic.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`:
```csharp
var knightIsAwake = false;
var archerIsAwake = true;
var prisonerIsAwake = false;
var petDogIsPresent = false;
QuestLogic.CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent);
// => false
```
## Source
### Created by
- @ErikSchierboom
### Contributed to by
- @yzAlvin
Loading…
Cancel
Save