Last active
November 27, 2020 11:52
-
-
Save cpbotha/0065a88898ed6ffe68e3baf6a1b5d945 to your computer and use it in GitHub Desktop.
cpbotha's nim n00b version of togglebit's guessing game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# guess a number inspired by togglebit! https://www.twitch.tv/togglebit | |
# this nim version by nim n00b Charl P. Botha https://charlbotha.com/ | |
# if I build this with nim 1.4 on ubuntu 20.04 with: | |
# nim c -d:release --opt:size guessing_game.nim | |
# the resultant binary is 62 kBytes | |
import random, strutils | |
# init random seed | |
randomize() | |
# this should be inclusive | |
let secret = rand(0..100) | |
stdout.write "Guess a number: " | |
var guess: int | |
# the ;guess below turns the assignment into an expression | |
# iow, this gives us the equivalent of the Python := walrus operator | |
while (if (guess = stdin.readline.parseInt; guess) == secret: | |
echo "Yay you win!" | |
false | |
elif guess > secret: | |
stdout.write "Too high, guess again: " | |
true | |
else: | |
stdout.write "Too low, guess again: " | |
true): discard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment