summaryrefslogtreecommitdiff
path: root/fp2/week3/mart/GetallenRaden.icl
blob: c1b2bb061a81f13829bf707790fb522e1b3fada6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Mart Lubbers s4109503, Camil Staps s4498062

module GetallenRaden

import StdEnv
import StdDynamic, StdDynamicFileIO
import StdFileSelect

// Example generation program
//fib :: [Int]
//fib = [fib` i \\ i <- [0..]]
//	where 
//		fib` 0 = 0
//		fib` 1 = 1
//		fib` n = fib` (n-1) + fib` (n-2)
//
//Start :: *World -> (Bool, *World)
//Start world = writeDynamic "fib" (dynamic fib) world

makeSeq :: Dynamic -> [Int]
makeSeq (x :: [Int]) = x
makeSeq _ = abort "You selected a file not containing a sequence"

basename :: String Int -> String
basename s 0 = toString s.[0]
basename s n = let c = toString s.[n] in if (c == "\\") "" ((basename s (n-1)) +++ c)

loadSeq :: String *World -> *([Int], String, *World)
loadSeq s world
# s = let l = size s in s % (0, l-5)
# (ok, dyn, world) = readDynamic s world
| not ok = abort "You didn't select a dynamic file"
| otherwise = (makeSeq dyn, basename s ((size s)-1), world)

play :: *File String [Int] Int Int -> *File
play io _ _ _ 5 = io <<< "Congratulations, you had 5 correct answers\n"
play io seqname [nextnum:sequence] currentnum correct
# io = io <<< (seqname +++ "[" +++ toString currentnum +++ "] = " +++ toString nextnum +++ "\n")
# (ok, guessednumber, io) = freadi io
| not ok = play (snd (freadline io)) seqname sequence (currentnum + 1) correct
# (io, correct) = if (guessednumber == hd sequence) (io, correct+1) (io <<< "Incorrect...\n", correct) 
= play (snd (freadline io)) seqname sequence (currentnum + 1) correct

Start :: *World -> *World
Start world
# (inputfilepath, world) = selectInputFile world
| isNothing inputfilepath = abort "Please select a file"
# (sequence, sequencename, world) = loadSeq (fromJust inputfilepath) world
# (io, world) = stdio world
# io = (play io sequencename sequence 0 0) <<< "Press any key to close"
# (_, world) = fclose io world
= world