changeset 42:54608c26bda2

Add Day 2 Q8 code
author IBBoard <dev@ibboard.co.uk>
date Mon, 18 Sep 2017 20:27:16 +0100
parents f4ea1f637f22
children d2764720ea17
files 2-Io/day2-self-study-8.io
diffstat 1 files changed, 35 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/2-Io/day2-self-study-8.io	Mon Sep 18 20:27:16 2017 +0100
@@ -0,0 +1,35 @@
+# It would be nice to do this as a "do while", but Io doesn't support that
+target_num := Random value(100) floor asNumber
+"Guess the number (attempt 1): " interpolate print
+guess := File standardInput readLine asNumber
+old_guess := -1
+old_diff := 100
+diff := 100
+
+if (guess == target_num,
+    "CORRECT! Wow! Very lucky!" println,
+    diff = target_num - guess
+    for (i, 2, 10,
+        "Guess the number (attempt #{i}): " interpolate print
+        old_guess = guess
+        old_diff = diff
+        guess = File standardInput readLine asNumber
+        diff = (target_num - guess) abs
+        
+        if (guess == target_num,
+            "Correct." println
+            break)
+
+        if (diff == old_diff,
+            "Same." println,
+            if (diff < old_diff,
+                "Warmer!" println,
+                "Colder." println
+            )
+        )
+    )
+)
+
+# FIXME: Would be nice to say "nope", but changes seem to be wrapped by context
+# So the value doesn't change outside the loops
+if (diff != 0, "Sorry, the number was #{target_num}")
\ No newline at end of file