May 23 2008
Time for a Little Golf: Number Guessing Game
Write a game which randomly generates a number between 0 (inclusive) and 100 (exclusive), and then prompts the user for a number: if the user guessed the number, exit; else, tells them if they should guess higher or lower.
And do it in as few characters as possible.
(Ruby impls particularly desired.)
Popularity: 4% [?]








By “Ruby” I can only assume you meant F#. God, let’s hope the formatting is preserved on this. I know this could be shortened but I need to get back to work!
#light
let random = new System.Random()
let seed = random.Next 99
printfn “Enter a number between -1 and 100: ”
let rec evalGuess (guess : int32) =
if guess=seed then
printfn “%d is the correct number!” guess
guess
elif guess<seed then
printfn “You guessed too low! Guess again:”
let newGuess = Int32.of_string(System.Console.ReadLine())
evalGuess newGuess
else
printfn “You guessed too high! Guess again:”
let newGuess = Int32.of_string(System.Console.ReadLine())
evalGuess newGuess
let newGuess = Int32.of_string(System.Console.ReadLine())
evalGuess newGuess
System.Console.ReadKey(true) //pause
Ugh, my ruby is really rust after all the groovy I’ve been doing recently. Here’s my quick attempt (which I’m sure can be improved):
def askAgain?(result)
result 0 ? (print “Too high! Guess again: “) : true
end
target = rand(99)
print “Guess a number (0 through 99): ”
while !askAgain?(((STDIN.gets).to_i target)); end
Shoot, wordpress ate some code, let’s try this again with escaped items:
def askAgain?(result)
result < 0 ? (print “Too low! Guess again: “) : result > 0 ? (print “Too high! Guess again: “) : true
end
target = rand(99)
print “Guess a number (0 through 99): ”
while !askAgain?(((STDIN.gets).to_i target)); end
Here’s a pastie link if it eats it again: http://pastie.caboo.se/202596
Pastie is probably the way to go. WordPress is being a bastard, and I’m not sure how to make it stop.
Put more precisely, I’m sure that figuring out how to make it stop is going to involve hacking PHP, so I’m not going to do that, and Pastie is easier. :)
I’m using wordpress on my website and have been using the WP-syntax plugin: http://wordpress.org/extend/plugins/wp-syntax/
It allows you to use the “pre” tag and set the lang property to whatever language and will autoformat. I’ve been pretty happy with it.
You can see it in action on this comment that I posted here, where I just used the normal comment box and a “pre lang=’groovy’” tag:
http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comment-218
Eating tags and badly formatting code is the main reason that I went away from a wordpress.com blog and started hosting it on my slicehost account.
Here’s a bit of Ruby nastiness, have fun :)
n,m=rand 100
g=%w(correct lower higher)
puts”guess a number between 0 and 99″
while m!=n
m=gets.to_i
puts g[[-1,m-n,1].sort[1]]
end
And OCaml for the heck of it:
open Random
let p=print_endline
let rec g n=
match compare n(read_int())with
-1->p”lower”;g n
|1->p”higher”;g n
|_->p”correct”
let _=
self_init();
p”guess a number between 0 and 99″;
g(int 100)
This one makes me cry:
open Random
let p=print_endline
let rec g n=match”lower”,”higher”,compare n(read_int())with
m,_,-1|_,m,1->p m;g n|_->p”correct”
let _=self_init();p”guess a number between 0 and 99″;g(int 100)
Nice one Ilmari, I took what you did, tweaked it a little and came up with this:
n,m=rand 100
puts “guess? (0-99)”
puts %w(lower correct higher)[(n<=>(m=gets.to_i))+1] while m!=n
pastie: http://pastie.caboo.se/202845
@Ted
Oh, that’s just sick.
@Ilmari
Why does that make you want to cry?
I tried WP-Syntax a while back and it broke my blog. I’ll try it again now and see what I get.
Because of n(read_int()), m,_,-1|_,m,1->, and int 100. And lack of whitespace around some keywords.
First looks like a function call, second is a pattern matching hack and third is from polluting the namespace with open. Granted, I was laughing maniacally when writing it.
I do think that open is a pox though, especially when module functions override ones from Pervasives.
@Ted
Nice! A couple small changes:
n=rand 100
puts”guess? (0-99)”
puts %w(correct higher lower)[nm=gets.to_i]while m!=n
@Ilmari
Wow, I’m pretty sure we won’t get it any smaller than that without using shorter strings or getting Matz to compile something new into the language :)
I think the spaceship operator got stripped from your code between n and m, but I’m assuming this is what you meant (trying out the new WP-Syntax plugin):
TextMate tells me that’s only 87 characters long :).
Hey, why didn’t your WP-Syntax stuff work?
Hrm. Worked out for me. Did the <pre> tag get stripped out for you?
Did you manually add it for me? It looks like it worked above for me.
Here’s a test:
alias foo=’bar’
Feel free to delete this test Robert.
I manually added it for you, yeah.
Looks like it is stripping it out on you. Interesting….
Let me see if I can find the magic switch to flip to allow anonymous people to use HTML markup: it’s already working for logged-in people (see Brian’s post on the front page).
@Ted
Yes, the spaceship got stripped, thanks for the correction.
Here’s an over-engineered OCaml version to balance my karma (assumption being that two wrongs make a right): http://pastie.caboo.se/204343
@Ilmari
Dude, exceptions? Really?
Dear God, no!!
Here’s 140 characters of Lua with terrible, terrible spacing.
n=math.random(0,99)
while(1)do print(”guess 0-99″) g=tonumber(io.read())
if g==n then break end print(gn and”lower”)end
Sorry, looks like my greater-than and less-than got strip_tags()’ed! Maybe it won’t do it if I reverse the order?
n=math.random(0,99)
while(1)do print(”guess 0-99″) g=tonumber(io.read())
if g==n then break end print(g>n and”lower”or g<n and”higher”)end
Sorry to be so late… In Ruby (one (cheat-)line, 95 char) :
n=(100*rand).to_i;loop{d=gets.to_i;(d==n)?(puts “bravo !”;break):puts((d>n)?”lower”:”greater”)}