May 23 2008

Time for a Little Golf: Number Guessing Game

Published by Robert Fischer at 5:44 am under To Be Categorized

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% [?]

23 Responses to “Time for a Little Golf: Number Guessing Game”

  1. Hamlet D\'Arcyon 23 May 2008 at 11:59 am

    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

  2. Ted Naleidon 23 May 2008 at 4:43 pm

    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

  3. Ted Naleidon 23 May 2008 at 4:46 pm

    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

  4. Robert Fischeron 23 May 2008 at 4:51 pm

    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. :)

  5. Ted Naleidon 23 May 2008 at 7:54 pm

    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.

  6. Ilmari Heikkinenon 23 May 2008 at 10:36 pm

    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

  7. Ilmari Heikkinenon 23 May 2008 at 11:11 pm

    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)

  8. Ilmari Heikkinenon 23 May 2008 at 11:24 pm

    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)

  9. Ted Naleidon 24 May 2008 at 2:43 pm

    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

  10. Robert Fischeron 25 May 2008 at 7:06 am

    @Ted

    Oh, that’s just sick.

    @Ilmari

    Why does that make you want to cry?

  11. Robert Fischeron 25 May 2008 at 3:12 pm

    I tried WP-Syntax a while back and it broke my blog. I’ll try it again now and see what I get.

  12. Ilmari Heikkinenon 27 May 2008 at 12:06 am

    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.

  13. Ilmari Heikkinenon 27 May 2008 at 12:51 am

    @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

  14. Ted Naleidon 27 May 2008 at 7:19 am

    @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):

    n=rand 100
    puts"guess? (0-99)"
    puts %w(correct higher lower)[nm=gets.to_i]while m!=n

    TextMate tells me that’s only 87 characters long :).

  15. Robert Fischeron 27 May 2008 at 7:50 am

    Hey, why didn’t your WP-Syntax stuff work?

  16. Robert Fischeron 27 May 2008 at 7:54 am

    Hrm. Worked out for me. Did the <pre> tag get stripped out for you?

  17. Ted Naleidon 27 May 2008 at 8:00 am

    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.

  18. Robert Fischeron 27 May 2008 at 8:01 am

    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).

  19. Ilmari Heikkinenon 27 May 2008 at 4:29 pm

    @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

  20. Robert Fischeron 28 May 2008 at 7:45 pm

    @Ilmari

    Dude, exceptions? Really?

    Dear God, no!!

  21. Evan DeMondon 13 Jun 2008 at 2:39 pm

    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

  22. Evan DeMondon 13 Jun 2008 at 2:43 pm

    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

  23. Sobeon 22 Jul 2008 at 1:42 pm

    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”)}

Trackback URI | Comments RSS

Leave a Reply

Green Web Hosting! This site hosted by DreamHost.