view 2-Io/day2-self-study-8.io @ 55:3a20e8b49bc4

Write some self-study code with an additional stretch rule to find collaborations
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Sep 2017 20:26:45 +0100
parents 54608c26bda2
children
line wrap: on
line source

# 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}")