Skip to content
Open
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,14 @@
"string_transformation"
]
},
{
"slug": "save-the-cow",
"name": "Save the Cow",
"uuid": "21b2a344-925e-427a-a2bb-69122df2ce61",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "sieve",
"name": "Sieve",
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/save-the-cow/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Instructions

Implement the logic for a word-guessing game.

A player tries to solve a secret word by guessing individual letters.
They win if they reveal all the letters in the secret word.
They lose if they make ten incorrect guesses before revealing the word.
4 changes: 4 additions & 0 deletions exercises/practice/save-the-cow/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Introduction

Bessie the cow has wandered onto an alien spaceship.
Guess the secret door code to bring her home before the ship blasts off.
17 changes: 17 additions & 0 deletions exercises/practice/save-the-cow/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"resu-xuniL"
],
"files": {
"solution": [
"save_the_cow.sh"
],
"test": [
"save_the_cow.bats"
],
"example": [
".meta/example.sh"
]
},
"blurb": "Implement a word-guessing game."
}
41 changes: 41 additions & 0 deletions exercises/practice/save-the-cow/.meta/example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

guess() {
local word="$1"; shift
local -a guesses=( "$@" )
local state="Ongoing"
local remaining_failures=9
local masked_word=${word//?/_}
local i

for guess in "${guesses[@]}"; do
if [[ $state == "Win" ]]; then
echo "cannot guess after the game is won" >&2
exit 1
elif [[ $state == "Lose" ]]; then
echo "cannot guess after the game is lost" >&2
exit 1
fi

if [[ -n $guess && $word =~ $guess && ! $masked_word =~ $guess ]]; then
for (( i = 0; i < ${#word}; i++ )); do
if [[ ${word:i:1} == "$guess" ]]; then
masked_word="${masked_word:0:i}${guess}${masked_word:i+1}"
fi
done
if [[ ! $masked_word =~ "_" ]]; then
state="Win"
fi
elif [[ -n $guess ]]; then
if (( remaining_failures == 0 )); then
state="Lose"
else
(( remaining_failures -- ))
fi
fi
done

printf "State: %s\nMasked word: %s\nRemaining failures: %s" "$state" "$masked_word" "$remaining_failures"
}

guess "$@"
21 changes: 21 additions & 0 deletions exercises/practice/save-the-cow/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ header }}
{% for case in cases %}
@test "{{ case["descriptions"] | join(": ") }}" {
{% if loop.first %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} "{{ case["input"]["word"] }}" "{{ case["input"]["guesses"] | join('" "') }}"
{%- if case["expect_error"] %}
assert_failure
assert_output "{{ case["expect_error_msg"] }}"
{%- else %}
expected="$(cat << EXPECTED
State: {{ case["expected"]["state"] }}
Masked word: {{ case["expected"]["maskedWord"] }}
Remaining failures: {{ case["expected"]["remainingFailures"] }}
EXPECTED
)"

assert_success
assert_output "$expected"
{%- endif %}
}
{% endfor %}
40 changes: 40 additions & 0 deletions exercises/practice/save-the-cow/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[71d340f9-fc29-4826-872e-ad7d0b83dd98]
description = "Initially 9 failures are allowed and no letters are guessed"

[76759c24-8f1a-4fc8-9ffd-d6a1ff0cf03b]
description = "After 10 failures the game is over"

[d6f2e202-7857-46fb-b709-43a7f3f3b2de]
description = "Losing with several correct guesses"

[71bc0cda-2032-4637-80c8-fc8771124c08]
description = "Feeding a correct letter removes underscores"

[5b568a1c-867d-418f-97a8-7b6f8a7ca0a2]
description = "Feeding a correct letter twice counts as a failure"

[3d40f15b-0271-4c5d-b1a4-3e1f66ff221f]
description = "Guessing a repeated letter reveals all instances"

[11a86435-e401-4250-a26e-3b0d8c4049ad]
description = "Getting all the letters right makes for a win"

[b3d81876-84ee-45bb-b531-baa1b05b4709]
description = "Winning on the last guess is still a win"

[cf204398-5e9f-402f-8bff-42cb7cbbbda9]
description = "Guessing after a lose is error"

[c2ec5b3d-4923-4a0e-a485-6aa6e78c7ece]
description = "Guessing after a win is error"
Loading