key handling with Borland C++ 5.0 (easter egg)

Hi everybody,

I want to implement a 'easter egg'inside my program created with Borland C++, I want the user to enter a string when the main form has the focus e.g. "easter egg", this string has to be buffered and checked, if the string entered is "easter egg" a messagebox has to be displayed.

But how must I implement this with BC++ 5?
I can use the 'press key' event on the Main form, but I don't have an idea of how to buffer all the keystrokes and check for a valid string?

Thanx a lot for all ideas !


Comments

  • The most efficient way is to use a state machine.  It's initialized with the current pattern (the cheat).  The state machine can be thought of as having as many states as there are character in the pattern. Each new incoming character that successfully matches the current state (the current character in the pattern) cause the machine to advance to the next state (the next character in the pattern).  If we advance through all possible states (all the characters in the pattern) and reach the final state (the null terminator) we know we have a match.  Any input character that does not match the current state, cause a state transition to state 0 (the first character in the pattern).

    If you're not familiar with state machines, this may sound complicated, but it's not.  There are other ways to so this, such as buffering the characters and doing string compares, but they are less efficient.  This method requires you to compare only a single character as each new input character comes in, so it scales up well. If you wanted to check 100 cheat codes with each keystroke, you could do it this way without adversely affecting the efficiency of your input code.

    Here's how you might encapsulate that in a class:

    class CheatMatcher
    {
    public:
      CheatMatcher (const char* cheat)
      : m_cheat (cheat)
      , m_match (false)
      {}

      bool check (char c)
      {
        m_match = false;

        if (m_cheat[m_cur] != c)
        {
            m_cur = 0;
        }
        
        if (m_cheat[m_cur] == c)
        {
            ++m_cur;

            if (m_cheat[m_cur] == '')
            {
                m_cur = 0;
                m_match = true; // match!
            }
        }
        else
        {
            m_cur = 0;
        }
        
        return m_match;
      }

      bool isMatch()
      {
        return m_match;
      }

    private:
      string m_cheat; // the cheat we're trying to match
      size_t m_cur;   // the current character we're comparing
      bool   m_match; // have we matched the entire pattern?
    };

    int main()
    {
      CheatMatcher egg ("easter egg");

      while (!egg.check (getche()))
        ;

      puts ("
    Congrats! You unlocked the easter egg!");

      return 0;
    }

    Of course, since we've put it in a class, we could match any number of cheats...

    int main()
    {
      CheatMatcher cheat1 ("easter egg");
      CheatMatcher cheat2 ("beelzebub");
      
      int c = getche();
      while (!cheat1.check(c) && !cheat2.check(c))
      {
        c = getche();
      }

      if (cheat1.isMatch())
      {
        puts ("
    Congrats! You unlocked the easter egg!");
      }
      else
      {
        puts ("
    Congrats! You unlocked the gates of hell!");
      }

      return 0;
    }

    I used something like this to implement cheat codes in Looney Tunes Racing. ;)

    Cheers,
    Eric




  • Thanks a lot Eric,

    I sure is an easy way of easter egg handling !!
    I will try if i can implement it in my program !!

    Thank you for the nice description !!


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories