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