Skip to content

Instantly share code, notes, and snippets.

@cpbotha
Last active November 27, 2020 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpbotha/0065a88898ed6ffe68e3baf6a1b5d945 to your computer and use it in GitHub Desktop.
Save cpbotha/0065a88898ed6ffe68e3baf6a1b5d945 to your computer and use it in GitHub Desktop.
cpbotha's nim n00b version of togglebit's guessing game
# 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