Working Hangman, but: NO max guess; NO ASCII gallow
authorCamil Staps <info@camilstaps.nl>
Sun, 19 Apr 2015 10:35:18 +0000 (12:35 +0200)
committerCamil Staps <info@camilstaps.nl>
Sun, 19 Apr 2015 10:35:18 +0000 (12:35 +0200)
fp2/week1/camil/Galgje.icl [new file with mode: 0644]

diff --git a/fp2/week1/camil/Galgje.icl b/fp2/week1/camil/Galgje.icl
new file mode 100644 (file)
index 0000000..d800469
--- /dev/null
@@ -0,0 +1,44 @@
+module Galgje\r
+\r
+import StdEnv, SimpleFileIO, RandomGetallen\r
+\r
+lexicon_file = "lexicon.txt"\r
+\r
+// From the slides\r
+skip_nl :: String -> String\r
+skip_nl str = if (size str > 0 && str.[size str-1] == '\n') (str%(0,size str-2)) str\r
+\r
+// From a String and a List of guesses (Chars), return a String that shows dots for letters that were not guessed yet\r
+stripUnknown :: String [Char] -> String\r
+stripUnknown s g = toString [if (isMember c g) c '.' \\ c <- (fromString s)]\r
+\r
+// Get a random word from the lexicon file\r
+randomWord :: *env -> (Maybe String, *env) | FileSystem env\r
+randomWord env\r
+# (ss,env) = readLines lexicon_file env\r
+| ss == Nothing = (Nothing, env)\r
+# (seed,env) = getNewRandomSeed env\r
+| otherwise = (Just (skip_nl ((shuffle (fromJust ss) seed)!!0)), env)\r
+\r
+// word, guesses, stdio -> (win, new guesses, stdio)\r
+round :: String [Char] *File -> (Bool, [Char], *File)\r
+round w g io\r
+# io = io <<< stripUnknown w g <<< '\n'\r
+| stripUnknown w g == w = (True, g, io)\r
+# io = io <<< "Guess: "\r
+# (ok,g`,io) = freadc io\r
+# (_,io) = freadline io // to read until the next \n\r
+| not ok = abort "Couldn't get guessed letter"\r
+| otherwise = round w [g`:g] io\r
+\r
+Start :: *World -> *World\r
+Start world\r
+# (word,world) = randomWord world\r
+| word == Nothing = abort "Couldn't get random word"\r
+# word = fromJust word\r
+# (io,world) = stdio world\r
+# (win,g,io) = round word [] io\r
+# io = if win (io <<< "You win!\n") io\r
+# (ok,world) = fclose io world\r
+| not ok = abort "Couldn't close stdio"\r
+| otherwise = world\r