Skip to content

Instantly share code, notes, and snippets.

@tcoopman
Created October 4, 2015 15:50
Show Gist options
  • Save tcoopman/53d8bcec55955be78ff9 to your computer and use it in GitHub Desktop.
Save tcoopman/53d8bcec55955be78ff9 to your computer and use it in GitHub Desktop.
import Graphics.Element exposing (show)
import String
type Assertion =
AssertT | AssertF
assertEqual : a -> a -> Assertion
assertEqual a b =
if | (toString a) == (toString b) -> AssertT
| otherwise -> AssertF
test : String -> Assertion -> String
test name assertion =
case assertion of
AssertT ->
"Success: " ++ name
AssertF ->
"Failure: " ++ name
hangmanImages: List (List String)
hangmanImages =
[
[" "
," "
," "
]
, [" O "
," "
," "
]
, [" O "
," | "
," "
]
, [" O "
," | "
,"/ "
]
, [" O "
," | "
,"/ \\"
]
, ["_O "
," | "
,"/ \\"
]
, ["_O_"
," | "
,"/ \\"
]
]
type alias GuessState = { wordToGuess: String, guesses: String}
guessState: GuessState
guessState =
{ wordToGuess = "MyWord", guesses = "" }
maxTries: Int
maxTries = List.length hangmanImages
isGuessed: GuessState -> Bool
isGuessed guessState =
let guessedCharsContainChar c = String.contains (String.fromChar c) guessState.guesses
in
String.all guessedCharsContainChar guessState.wordToGuess
showWordWithGuesses: GuessState -> String
showWordWithGuesses guessState =
let blankOrChar c =
if | String.contains (String.fromChar c) guessState.guesses -> c
| otherwise -> '_'
in String.map blankOrChar guessState.wordToGuess
numberOfGuesses: GuessState -> Int
numberOfGuesses guessState =
String.length guessState.guesses
numberOfWrongGuesses: GuessState -> Int
numberOfWrongGuesses guessState =
let nomatch c = not (String.contains (String.fromChar c) guessState.wordToGuess)
in String.length (String.filter nomatch guessState.guesses)
isGameDone: GuessState -> Bool
isGameDone guessState =
numberOfWrongGuesses guessState == maxTries - 1 || isGuessed guessState
main =
show [
[test "maxTries" (assertEqual 7 (maxTries))],
[test "isGuessed False" (assertEqual False (isGuessed {wordToGuess = "test", guesses = ""}))],
[test "isGuessed True" (assertEqual True (isGuessed {wordToGuess = "test", guesses = "test"}))],
[test "isGuessed False" (assertEqual True (isGuessed {wordToGuess = "test", guesses = "tste"}))],
[test "showWordWithGuesses" (assertEqual "t_st" (showWordWithGuesses {wordToGuess = "test", guesses = "ts"}))],
[test "showWordWithGuesses" (assertEqual "__s_" (showWordWithGuesses {wordToGuess = "test", guesses = "s"}))],
[test "numberOfWrongGuesses" (assertEqual 1 (numberOfWrongGuesses {wordToGuess = "test", guesses = "sa"}))],
[test "isGameDone" (assertEqual True (isGameDone {wordToGuess = "test", guesses = "tse"}))],
[test "isGameDone False" (assertEqual False (isGameDone {wordToGuess = "test", guesses = "set"}))]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment