How to properly implement cheat codes?

Posted by Axarydax on Stack Overflow See other posts from Stack Overflow or by Axarydax
Published on 2010-03-24T10:29:24Z Indexed on 2010/03/24 10:33 UTC
Read the original article Hit count: 393

Filed under:
|
|

Hi, what would be the best way to implement kind of cheat codes in general? I have WinForms application in mind, where a cheat code would unlock an easter egg, but the implementation details are not relevant.

The best approach that comes to my mind is to keep index for each code - let's consider famous DOOM codes - IDDQD and IDKFA, in a fictional C# app.

string[] CheatCodes = { "IDDQD", "IDKFA"};
int[] CheatIndexes = { 0, 0 };
const int CHEAT_COUNT = 2;
void KeyPress(char c)
{
    for (int i = 0; i < CHEAT_COUNT; i++) //for each cheat code
    {
        if (CheatCodes[i][CheatIndexes[i]] == c)
        { //we have hit the next key in sequence
            if (++CheatIndexes[i] == CheatCodes[i].Length) //are we in the end?
            {
                //Do cheat work
                MessageBox.Show(CheatCodes[i]);
                //reset cheat index so we can enter it next time
                CheatIndexes[i] = 0; 
            }
        }
        else //mistyped, reset cheat index
            CheatIndexes[i] = 0; 
    }
}

Is this the right way to do it?

© Stack Overflow or respective owner

Related posts about cheat

Related posts about keystroke