summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2015-04-19 12:35:18 +0200
committerCamil Staps2015-04-19 12:35:18 +0200
commitdcb5464af68aae1af58d58d401655850e08ae758 (patch)
tree4361613f8ae48f8f8161aa67a829be720ce1178e
parentSimpleFileIO.dcl (diff)
Working Hangman, but: NO max guess; NO ASCII gallow
-rw-r--r--fp2/week1/camil/Galgje.icl44
1 files changed, 44 insertions, 0 deletions
diff --git a/fp2/week1/camil/Galgje.icl b/fp2/week1/camil/Galgje.icl
new file mode 100644
index 0000000..d800469
--- /dev/null
+++ b/fp2/week1/camil/Galgje.icl
@@ -0,0 +1,44 @@
+module Galgje
+
+import StdEnv, SimpleFileIO, RandomGetallen
+
+lexicon_file = "lexicon.txt"
+
+// 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
+
+// From a String and a List of guesses (Chars), return a String that shows dots for letters that were not guessed yet
+stripUnknown :: String [Char] -> String
+stripUnknown s g = toString [if (isMember c g) c '.' \\ c <- (fromString s)]
+
+// Get a random word from the lexicon file
+randomWord :: *env -> (Maybe String, *env) | FileSystem env
+randomWord env
+# (ss,env) = readLines lexicon_file env
+| ss == Nothing = (Nothing, 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
+# io = io <<< stripUnknown w g <<< '\n'
+| stripUnknown w g == w = (True, g, io)
+# io = io <<< "Guess: "
+# (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
+
+Start :: *World -> *World
+Start world
+# (word,world) = randomWord 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
+# (ok,world) = fclose io world
+| not ok = abort "Couldn't close stdio"
+| otherwise = world