From c4c6947589f778dfb9a2af79ee8fe72dd8f11733 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Sun, 19 Apr 2015 13:58:23 +0200 Subject: [PATCH] Added mistakes left counter; now all that is left to do is an ASCII art gallow --- fp2/week1/camil/Galgje.icl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/fp2/week1/camil/Galgje.icl b/fp2/week1/camil/Galgje.icl index d800469..f8dd8f3 100644 --- a/fp2/week1/camil/Galgje.icl +++ b/fp2/week1/camil/Galgje.icl @@ -4,6 +4,11 @@ import StdEnv, SimpleFileIO, RandomGetallen lexicon_file = "lexicon.txt" +// Is a Char a member of a String +isMemberString :: String Char -> Bool +isMemberString "" c = False +isMemberString s c = s.[0] == c || isMemberString (s % (1,size s - 1)) c + // From the slides skip_nl :: String -> String skip_nl str = if (size str > 0 && str.[size str-1] == '\n') (str%(0,size str-2)) str @@ -20,16 +25,18 @@ randomWord env # (seed,env) = getNewRandomSeed env | otherwise = (Just (skip_nl ((shuffle (fromJust ss) seed)!!0)), env) -// word, guesses, stdio -> (win, new guesses, stdio) -round :: String [Char] *File -> (Bool, [Char], *File) -round w g io +// word, guesses, mistakes left, stdio -> (win, new guesses, stdio) +play :: String [Char] Int *File -> (Bool, [Char], *File) +play w g n io # io = io <<< stripUnknown w g <<< '\n' | stripUnknown w g == w = (True, g, io) -# io = io <<< "Guess: " +# io = io <<< "Guess (" <<< toString n <<< "): " # (ok,g`,io) = freadc io # (_,io) = freadline io // to read until the next \n | not ok = abort "Couldn't get guessed letter" -| otherwise = round w [g`:g] io +| isMemberString w g` = play w [g`:g] n io +| n == 0 = (False, [g`:g], io) +| otherwise = play w [g`:g] (n-1) io Start :: *World -> *World Start world @@ -37,8 +44,8 @@ Start world | word == Nothing = abort "Couldn't get random word" # word = fromJust word # (io,world) = stdio world -# (win,g,io) = round word [] io -# io = if win (io <<< "You win!\n") io +# (win,g,io) = play word [] 5 io +# io = if win (io <<< "You win!\n") (io <<< "You lose!\n") # (ok,world) = fclose io world | not ok = abort "Couldn't close stdio" | otherwise = world -- 2.20.1