diff options
author | Camil Staps | 2016-02-02 19:24:50 +0100 |
---|---|---|
committer | Camil Staps | 2016-02-02 19:24:50 +0100 |
commit | a7d7542dc646a5fd124ef71e71ce260889f1701b (patch) | |
tree | 04ed89503bbb3cc9933273a1326a53ca724c3492 /1415/fp2/week1/camil/Galgje.icl | |
parent | week6 camil: working positioning of lines by putting empties at left and righ... (diff) |
Diffstat (limited to '1415/fp2/week1/camil/Galgje.icl')
-rw-r--r-- | 1415/fp2/week1/camil/Galgje.icl | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/1415/fp2/week1/camil/Galgje.icl b/1415/fp2/week1/camil/Galgje.icl new file mode 100644 index 0000000..75b09d0 --- /dev/null +++ b/1415/fp2/week1/camil/Galgje.icl @@ -0,0 +1,58 @@ +// Mart Lubbers s4109503, Camil Staps s4498062
+
+module Galgje
+
+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
+
+// 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, mistakes left, stdio -> (win, new guesses, stdio)
+play :: String [Char] Int *File *env -> (Bool, [Char], *File, *env) | FileSystem env
+play w g n io world
+# io = io <<< stripUnknown w g <<< '\n'
+| stripUnknown w g == w = (True, g, io, world)
+# (round,world) = readFile ("round-" +++ (toString n) +++ ".txt") world
+| round == Nothing = abort "Couldn't get gallow"
+# io = io <<< (fromJust round)
+# 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"
+| isMemberString w g` = play w [g`:g] n io world
+| n == 0 = (False, [g`:g], io, world)
+| otherwise = play w [g`:g] (n-1) io world
+
+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,world) = play word [] 5 io world
+# (lost,world) = readFile "round-lost.txt" world
+| lost == Nothing = abort "Couldn't get gallow"
+# io = if win (io <<< "You win!\n") (io <<< "You lose!\n" <<< fromJust lost)
+# (ok,world) = fclose io world
+| not ok = abort "Couldn't close stdio"
+| otherwise = world
|