Skip to content

Instantly share code, notes, and snippets.

@jabgibson
Last active July 27, 2018 20:27
Show Gist options
  • Save jabgibson/eccbbf39512d699cf9a434ffaaa7ca73 to your computer and use it in GitHub Desktop.
Save jabgibson/eccbbf39512d699cf9a434ffaaa7ca73 to your computer and use it in GitHub Desktop.
Guess Number Game [Multi Language Exercise.. not about line count]
-- this Haskell version is not quite the same as the Nim and Racket versions since I am pulling in a Random
-- number to be guessed, rather than hardcoding the number as seen on Nim and Racket versions on line 3. I am also
-- adding comments to the functions in this Haskell version. This might prompt me to go through and revamp other
-- solutions in the future.
import System.Random
import System.Environment
import Text.Regex
import Text.Regex.Base
-- strint converts a string to an integer, defaulting to 0 if the string doesn't represent a valid integer.
strint :: String -> Integer
strint x = do
if Nothing == (matchOnce (makeRegex "^[0-9]+$" :: Regex) x)
then 0
else (read x :: Integer)
guess :: Integer -> IO ()
guess x = do
putStrLn "Guess a number from 1 to 20. [0 to quit]"
i <- getLine
let answer = strint i
if answer == 0
then putStrLn "Bye!!"
else do
if answer == x
then putStrLn "Thats Right"
else
do
if answer > x
then putStrLn "No. Try a lower number"
else putStrLn "No. Try a higher number"
guess x
main :: IO ()
main = do
x <- randomRIO (1,20)
guess x
import strutils
const answer = 0b1011
proc getInput(): (int, bool) =
var rawInput: TaintedString
try:
rawInput = stdin.readLine
let intInput = parseInt(rawInput)
return (intInput, false)
except ValueError:
if rawInput == "x":
return (0, true)
return (0, false)
proc game() =
block game:
while true:
echo "Guess a number between 1 and 10:"
let (input, exit) = getInput()
if exit:
echo "ok exiting guessing game"
break game
case input:
of 1..(answer - 1):
echo "Try a higher number"
of (answer + 1)..20:
echo "Try a lower number"
of answer:
echo format("You are right!! The answer is $1", input)
break game
else:
echo "invalid number. 1 - 20"
game()
#lang racket
(define answer #b10100)
(define (RUN)
(define rv #f)
(define player-input (question))
(define guess (string->number player-input))
(when (not (number? guess))
(when (not(string=? player-input "x"))
(displayln (format "You guessed a non number value: ~a" player-input))
(set! rv #t)))
(when (number? guess)
(when (and (<= guess 20) (>= guess 1))
(when (= guess answer)
(displayln (format "You are right!!! The answer was ~a" player-input))
(set! rv #f))
(when (< guess answer)
(displayln "Try a higher number")
(set! rv #t))
(when (> guess answer)
(displayln "Try a lower number")
(set! rv #t)))
(when (or (> guess 20) (< guess 1))
(displayln (format "You need to choose a number between 1 and 20. Try again :)"))
(set! rv #t)))
rv)
(define (question)
(display (format "~a" "Pick a number from 1 - 20: "))
(define input (read-line))
input)
; game is the loop the runs the game until the answer is found.
(define (game bool)
(when bool
(define continue? (RUN))
(game continue?)))
(game #t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment