diff options
Diffstat (limited to 'fp2/week6/mart/GuessWhat.icl')
-rw-r--r-- | fp2/week6/mart/GuessWhat.icl | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/fp2/week6/mart/GuessWhat.icl b/fp2/week6/mart/GuessWhat.icl new file mode 100644 index 0000000..8e6284a --- /dev/null +++ b/fp2/week6/mart/GuessWhat.icl @@ -0,0 +1,96 @@ +module GuessWhat
+
+/* Instructions:
+
+(1) copy this main file (GuessWhat.icl), QA_shapes.(d/i)cl, and QA.(d/i)cl in the folder:
+ {iTasks-SDK}\Experiments\SVG_tests\
+
+(2) create a new project and set de environment to 'iTasks'
+
+(3) Bring-Up-To-Date and start generated application
+
+(4) Open a browser and navigate to localhost.
+ The application creates a task that randomly selects a number of image-name pairs and asks the user to
+ give the right name to the right image. Once this is done correctly, the task terminates, otherwise the
+ user can try again.
+
+(5) Create a new set of QA-shapes. You can choose one of the following:
+ (i) Dutch traffic signs. See attached document:
+ Reglement-verkeersregels-en-verkeersteke.pdf, appendix 1, hoofdstuk A upto L.
+
+ Implement **at least 15** traffic signs. In your solution, clearly indicate at each traffic sign
+ which one you have implemented (use the numbers in the right-most column in the above document).
+
+
+ (ii) European flags. See the following wikipedia page:
+ http://nl.wikipedia.org/wiki/Lijst_van_vlaggen_van_Europa
+
+ Implement **at least 15** flags. In your solution, clearly indicate at each flag which one you
+ have implemented by the name of the nation or organization.
+*/
+
+import iTasks // de algemene iTask API
+import iTasks.API.Extensions.SVG.SVGlet // specialiseer task editors
+from Data.List import zipWith
+
+import QA_shapes // the QA elements that have to be guessed
+
+nr_of_qas :== 10
+
+Start :: *World -> *World
+Start world = startEngine [publish "/" (WebApp []) (\_ -> play queries)] world
+
+play :: [QA] -> Task String
+play []
+ = viewInformation "missing queries" [] "No queries are available"
+play qas
+ = sequence "throw dice" (repeatn (nr_of_qas + length qas) (get randomInt))
+ >>= \nrs -> let (nrs1,nrs2) = splitAt nr_of_qas nrs
+ shuffled_qas = shuffle nrs2 qas
+ (qs,as) = unzip (take nr_of_qas shuffled_qas)
+ sas = shuffle nrs1 as
+ in keep_guessing qs as sas
+
+keep_guessing :: [Image ()] [String] [String] -> Task String
+keep_guessing qs as sas
+ = allTasks [guess i q sas \\ q <- qs & i <- [1 ..]]
+ >>* [ OnAction (Action "Check" []) (hasValue (check_answers qs as sas))
+ , OnAction ActionQuit (always (return "Goodbye"))
+ ]
+
+check_answers :: [Image ()] [String] [String] [String] -> Task String
+check_answers qs as sas nas
+| ok == nr_of_qas
+ = viewInformation "Tada!" [] "Congratulations! All correct!"
+| otherwise
+ = (viewInformation "Ouch!" [] ("Too bad: there are " <+++ nr_of_qas - ok <+++ " mistakes.")
+ ||-
+ allTasks [ ((show_image i q <<@ ArrangeHorizontal)
+ ||-
+ (viewInformation "isn't" [] a <<@ ArrangeHorizontal)
+ ) <<@ ArrangeHorizontal
+ \\ wrong <- zipWith (<>) as nas
+ & q <- qs
+ & a <- nas
+ & i <- [1 ..]
+ | wrong
+ ]
+ ) >>* [ OnAction (Action "Try again" []) (always (keep_guessing qs as sas))
+ , OnAction ActionQuit (always (return "Goodbye"))
+ ]
+where
+ ok = length [() \\ a <- as & b <- nas | a == b]
+
+show_image :: Int (Image ()) -> Task ()
+show_image i q = viewInformation ("image " <+++ i) [imageView (\_ _ -> q) (\_ _ -> Nothing)] ()
+
+guess :: Int (Image ()) [String] -> Task String
+guess i q sas
+ = ( (show_image i q <<@ ArrangeHorizontal)
+ ||-
+ (enterChoice "is:" [ChooseWith (ChooseFromComboBox id)] sas <<@ ArrangeHorizontal)
+ ) <<@ ArrangeHorizontal
+
+shuffle :: [Int] [a] -> [a]
+shuffle rnrs as
+ = fst (unzip (sortBy (\(_,r1) (_,r2) -> r1 < r2) (zip2 as rnrs)))
|