Save the Cow
Play a word-guessing game: each guessed letter reveals every place it stands in the secret word, and ten failures lose the game.
Adapted from Exercism’s problem specification (MIT). Listed under Games & Simulation.
// FILE: save_the_cow.nomi
//# A word-guessing game in the style of hangman.
/// Where a game stands.
pub enum State
Ongoing
Win
Lose
end
/// A guess the game no longer takes.
pub enum Error
AlreadyWon
AlreadyLost
end
impl Display for Error
fn to_string(error: Error) -> String
case error
.AlreadyWon -> "cannot guess after the game is won"
.AlreadyLost -> "cannot guess after the game is lost"
end
end
end
/// How many failures a game allows before the one that loses it.
once allowed_failures = 9
/// A word-guessing game. It is a value: each guess answers the game after
/// it.
pub opaque struct Game
word: List<Codepoint>
revealed: Set<Codepoint>
remaining_failures: Int
state: State
end
impl Game
/// A new game whose secret is `word`, with no letters revealed.
pub fn new(word: String) -> Game
{
word: String.to_codepoints(word),
revealed: [],
remaining_failures: allowed_failures,
state: .Ongoing,
}
end
/// The game after guessing `letter`. A letter of the word not yet
/// revealed reveals every place it stands; any other guess is a failure,
/// and a failure with none remaining loses.
pub fn guess(game: Game, letter: Codepoint) -> Result<Game, Error>
case game.state
.Win -> Err(.AlreadyWon)
.Lose -> Err(.AlreadyLost)
.Ongoing when is_hidden(game, letter) -> Ok(reveal(game, letter))
.Ongoing when game.remaining_failures == 0 -> Ok({..game, state: .Lose})
.Ongoing -> Ok({..game, remaining_failures: .. - 1})
end
end
/// Where the game stands.
pub fn state(game: Game) -> State
game.state
end
/// The word with each letter not yet revealed written as `_`.
pub fn masked_word(game: Game) -> String
game.word
|> Iter.map(fn letter ->
if letter in game.revealed then letter else '_'
end)
|> Iter.to_list()
|> String.from_codepoints()
end
/// How many more failures the game allows before the one that loses it.
pub fn remaining_failures(game: Game) -> Int
game.remaining_failures
end
end
/// Whether `letter` is in the word and not yet revealed.
fn is_hidden(game: Game, letter: Codepoint) -> Bool
letter in game.word and not letter in game.revealed
end
/// `game` with `letter` revealed, won when that reveals the whole word.
fn reveal(game: Game, letter: Codepoint) -> Game
revealed = Set.insert(game.revealed, letter)
won = Iter.all_match(game.word, &(&1 in revealed))
{..game, revealed:, state: if won then .Win else .Ongoing}
end
// FILE: save_the_cow_test.nomi
import save_the_cow.{Error, Game, State}
/// A new game of `word` after each of `guesses` in turn, or the first
/// guess it refuses.
fn play(word: String, guesses: List<Codepoint>) -> Result<Game, Error>
Iter.try_reduce(guesses, Game.new(word), Game.guess)
end
test "guess"
[
(
"Initially 9 failures are allowed and no letters are guessed",
[],
State.Ongoing,
"____",
9,
),
(
"After 10 failures the game is over",
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
.Lose,
"____",
0,
),
(
"Losing with several correct guesses",
['t', 'o', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
.Lose,
"_oot",
0,
),
("Feeding a correct letter removes underscores", ['t'], .Ongoing, "___t", 9),
("Feeding a correct letter twice counts as a failure", ['t', 't'], .Ongoing, "___t", 8),
("Guessing a repeated letter reveals all instances", ['t', 't', 'o'], .Ongoing, "_oot", 8),
("Getting all the letters right makes for a win", ['t', 't', 'o', 'l'], .Win, "loot", 8),
(
"Winning on the last guess is still a win",
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 't', 'o', 'l'],
.Win,
"loot",
0,
),
]
|> subtests (description, guesses, state, masked_word, remaining_failures), description
assert Ok(game) = play("loot", guesses)
assert Game.state(game) == state
assert Game.masked_word(game) == masked_word
assert Game.remaining_failures(game) == remaining_failures
end
end
test "guessing after the game is over"
[
(
"Guessing after a lose is error",
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'],
"cannot guess after the game is lost",
),
(
"Guessing after a win is error",
['t', 'o', 'l', 'l'],
"cannot guess after the game is won",
),
]
|> subtests (description, guesses, message), description
assert Err(error) = play("loot", guesses)
assert "${error}" == message
end
endok guess (8 cases)
ok guessing after the game is over (2 cases)
test result: ok. 10 passed, 0 failed
How it’s written
Section titled “How it’s written”- Adapted: the canonical comments suggest an object that changes with each guess, or a functional reactive design. The Nomi version is a value:
Game.guessanswers the game after the guess, and a sequence of guesses is a fold. Gameis an opaque struct of the word’s letters, the set of revealed letters, the failures remaining and theState.guessis onecaseon the state, withwhenguards for the ongoing game: a letter in the word and not yet revealed reveals it, any other guess (a wrong letter, or a right one guessed again) is a failure, and a failure with none remaining loses. The canonical counts fix that rule: 9 failures remain at the start, 0 after nine and still 0 after the tenth, which loses.- Guessing after the game is over is an
Err, oneErrorvariant per outcome, whoseDisplaygives the canonical messages. - A guess is a
Codepoint, since the game works letter by letter (style §20), andmasked_wordmaps each letter of the word to itself or_. - The test file’s
playfolds the guesses withIter.try_reduce(guesses, Game.new(word), Game.guess), which stops at the first refused guess and answers itsErr. - The cases are two
subteststables, one for the games that end in a state and one for the refused guesses.