74678aae633a0a1a3e02d6b4cbe7e28e18871ace
[fp1415.git] / fp2 / week3 / mart / GetallenRaden.icl
1 module GetallenRaden
2
3 import StdEnv
4 import StdDynamic, StdDynamicFileIO
5 import StdFileSelect
6
7 // Example generation program
8 //fib :: [Int]
9 //fib = [fib` i \\ i <- [0..]]
10 // where
11 // fib` 0 = 0
12 // fib` 1 = 1
13 // fib` n = fib` (n-1) + fib` (n-2)
14 //
15 //Start :: *World -> (Bool, *World)
16 //Start world = writeDynamic "fib" (dynamic fib) world
17
18 makeSeq :: Dynamic -> [Int]
19 makeSeq (x :: [Int]) = x
20 makeSeq _ = abort "You selected a file not containing a sequence"
21
22 basename :: String Int -> String
23 basename s 0 = toString s.[0]
24 basename s n = let c = toString s.[n] in if (c == "\\") "" ((basename s (n-1)) +++ c)
25
26 loadSeq :: String *World -> *([Int], String, *World)
27 loadSeq s world
28 # s = let l = size s in s % (0, l-5)
29 # (ok, dyn, world) = readDynamic s world
30 | not ok = abort "You didn't select a dynamic file"
31 | otherwise = (makeSeq dyn, basename s ((size s)-1), world)
32
33 play :: *File String [Int] Int Int -> *File
34 play io _ _ _ 5 = io <<< "Congratulations, you had 5 correct answers\n"
35 play io seqname [nextnum:sequence] currentnum correct
36 # io = io <<< (seqname +++ "[" +++ toString currentnum +++ "] = " +++ toString nextnum +++ "\n")
37 # (ok, guessednumber, io) = freadi io
38 | not ok = play (snd (freadline io)) seqname sequence (currentnum + 1) correct
39 # (io, correct) = if (guessednumber == hd sequence) (io, correct+1) (io <<< "Incorrect...\n", correct)
40 = play (snd (freadline io)) seqname sequence (currentnum + 1) correct
41
42 Start :: *World -> *World
43 Start world
44 # (inputfilepath, world) = selectInputFile world
45 | isNothing inputfilepath = abort "Please select a file"
46 # (sequence, sequencename, world) = loadSeq (fromJust inputfilepath) world
47 # (io, world) = stdio world
48 # io = (play io sequencename sequence 0 0) <<< "Press any key to close"
49 # (_, world) = fclose io world
50 = world