repositories
/
fp1415.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
dcb5464
)
Added mistakes left counter; now all that is left to do is an ASCII art gallow
author
Camil Staps
<info@camilstaps.nl>
Sun, 19 Apr 2015 11:58:23 +0000
(13:58 +0200)
committer
Camil Staps
<info@camilstaps.nl>
Sun, 19 Apr 2015 11:58:23 +0000
(13:58 +0200)
fp2/week1/camil/Galgje.icl
patch
|
blob
|
history
diff --git
a/fp2/week1/camil/Galgje.icl
b/fp2/week1/camil/Galgje.icl
index
d800469
..
f8dd8f3
100644
(file)
--- a/
fp2/week1/camil/Galgje.icl
+++ b/
fp2/week1/camil/Galgje.icl
@@
-4,6
+4,11
@@
import StdEnv, SimpleFileIO, RandomGetallen
\r
lexicon_file = "lexicon.txt"
\r
\r
\r
lexicon_file = "lexicon.txt"
\r
\r
+// Is a Char a member of a String
\r
+isMemberString :: String Char -> Bool
\r
+isMemberString "" c = False
\r
+isMemberString s c = s.[0] == c || isMemberString (s % (1,size s - 1)) c
\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
// 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
@@
-20,16
+25,18
@@
randomWord env
# (seed,env) = getNewRandomSeed env
\r
| otherwise = (Just (skip_nl ((shuffle (fromJust ss) seed)!!0)), env)
\r
\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
+// word, guesses,
mistakes left,
stdio -> (win, new guesses, stdio)
\r
+
play :: String [Char] Int
*File -> (Bool, [Char], *File)
\r
+
play w g n
io
\r
# io = io <<< stripUnknown w g <<< '\n'
\r
| stripUnknown w g == w = (True, g, io)
\r
# io = io <<< stripUnknown w g <<< '\n'
\r
| stripUnknown w g == w = (True, g, io)
\r
-# io = io <<< "Guess: "
\r
+# io = io <<< "Guess
(" <<< toString n <<< ")
: "
\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
# (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
+| isMemberString w g` = play w [g`:g] n io
\r
+| n == 0 = (False, [g`:g], io)
\r
+| otherwise = play w [g`:g] (n-1) io
\r
\r
Start :: *World -> *World
\r
Start world
\r
\r
Start :: *World -> *World
\r
Start world
\r
@@
-37,8
+44,8
@@
Start world
| word == Nothing = abort "Couldn't get random word"
\r
# word = fromJust word
\r
# (io,world) = stdio 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
+# (win,g,io) =
play word [] 5
io
\r
+# io = if win (io <<< "You win!\n")
(io <<< "You lose!\n")
\r
# (ok,world) = fclose io world
\r
| not ok = abort "Couldn't close stdio"
\r
| otherwise = world
\r
# (ok,world) = fclose io world
\r
| not ok = abort "Couldn't close stdio"
\r
| otherwise = world
\r