GetallenRaden done:)
[fp1415.git] / fp2 / week3 / mart / GetallenRaden.icl
index 37584fb..74678aa 100644 (file)
@@ -1,14 +1,10 @@
 module GetallenRaden\r
 \r
-/*     Dynamics werken alleen met de 32-bit versie van de Clean compiler.\r
-       (*) Gebruik Environment 'Experimental'\r
-       (*) In Project:Project Options: zet vlag 'Enable dynamics' aan\r
-*/\r
 import StdEnv\r
-import StdDynamic, StdDynamicFileIO            // nodig voor dynamics en dynamics in files\r
-import StdFileSelect                                   // platform file-selector dialoog\r
+import StdDynamic, StdDynamicFileIO\r
+import StdFileSelect\r
 \r
-// Generation program\r
+// Example generation program\r
 //fib :: [Int]\r
 //fib = [fib` i \\ i <- [0..]]\r
 //     where \r
@@ -23,16 +19,32 @@ makeSeq :: Dynamic -> [Int]
 makeSeq (x :: [Int]) = x\r
 makeSeq _ = abort "You selected a file not containing a sequence"\r
 \r
-loadSeq :: String *World -> *([Int], *World)\r
+basename :: String Int -> String\r
+basename s 0 = toString s.[0]\r
+basename s n = let c = toString s.[n] in if (c == "\\") "" ((basename s (n-1)) +++ c)\r
+\r
+loadSeq :: String *World -> *([Int], String, *World)\r
 loadSeq s world\r
 # s = let l = size s in s % (0, l-5)\r
 # (ok, dyn, world) = readDynamic s world\r
 | not ok = abort "You didn't select a dynamic file"\r
-| otherwise = (makeSeq dyn, world)\r
+| otherwise = (makeSeq dyn, basename s ((size s)-1), world)\r
+\r
+play :: *File String [Int] Int Int -> *File\r
+play io _ _ _ 5 = io <<< "Congratulations, you had 5 correct answers\n"\r
+play io seqname [nextnum:sequence] currentnum correct\r
+# io = io <<< (seqname +++ "[" +++ toString currentnum +++ "] = " +++ toString nextnum +++ "\n")\r
+# (ok, guessednumber, io) = freadi io\r
+| not ok = play (snd (freadline io)) seqname sequence (currentnum + 1) correct\r
+# (io, correct) = if (guessednumber == hd sequence) (io, correct+1) (io <<< "Incorrect...\n", correct) \r
+= play (snd (freadline io)) seqname sequence (currentnum + 1) correct\r
 \r
-Start :: *World -> ([Int], *World)\r
+Start :: *World -> *World\r
 Start world\r
 # (inputfilepath, world) = selectInputFile world\r
 | isNothing inputfilepath = abort "Please select a file"\r
-# (sequence, world) = loadSeq (fromJust inputfilepath) world\r
-| otherwise = (take 20 sequence, world)\r
+# (sequence, sequencename, world) = loadSeq (fromJust inputfilepath) world\r
+# (io, world) = stdio world\r
+# io = (play io sequencename sequence 0 0) <<< "Press any key to close"\r
+# (_, world) = fclose io world\r
+= world\r