Tuesday, May 3, 2011

Ruby - memory game

Hey,

I am trying to build a very simple game using Ruby that will be on the web. I am am trying to:

user goes to page and a string appears for X amount of seconds and then after X amount hide it and ask for the user input then check to see how far the person got (how many they guessed right)

I would have strings for:

timeout in seconds text string user input string

Then I am not sure how to compare both strings..thoughts?

Thanks,

Ryan

From stackoverflow
  • Some suggestions as to the algorithms you can use:

    • Levenshtein distance to measure how correct the user's input were to the originals.
    • Look at Fuzzy string matching as well if you are really brave.
    • Depending on the language of the strings, it may be fun to hack up a Soundex based matcher.
    Coughlin : Went WAY over my head, is there any other approach to this or any other game you can think of? Something to get my hands on and get some experience coding. I already did a guess a number game. Ryan
    dirkgently : To start off, try a naive string comparison. If your original text was "apple", the user has to enter "apple" to get any points. Next, take a step further by giving him some points, hints about what part he got right etc.
  • For a simplistic approach just off top of my head, you can treat it as an char array comparison instead of a string comparison.

    Let's say the string "abcdefg" was shown and user typed in "abdcefg", he is considered to remember 5 out of 7 of the given string.

  • This assumes you start with strings, but handle the data in arrays. (Includes the string to array method.) You can use this for anything that can fit in an array. simply call with two strings and get the score back. This works just as well when either string is shorter.

    #string to char array
    def s_to_a(s)
      a = [] 
      s.each_char{|c| a << c}
      a
    end
    
    def memory_rating(s1, s2)
      score = 0
    
      #convert both strings to arrays
      a1 = s_to_a(s1)
      a2 = s_to_a(s2)
    
      a1.each do |e|
        # shift allows easy access to all elements of a2 
        # inside the iterator for a1
        score += (e == a2.shift) ? 1 : 0
      end
      score
    end
    

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.