### Unintended Inspiration

During a project planning meeting a while back, one of our clients made the comment “Wouldn’t it be cool if we had a cheat code in the app?” At the time everyone laughed it off, but I took a note and knew we had one more deliverable for the next release. It was a no brainer that the Konami Code would be making an appearance.

### Setup

The following is an example application that implements two codes or multi-step commands via the IOnGestureListener interface. For the example, I figured that implementing both the Konami Code and the old Mortal Kombat cheat menu code that was used on the SEGA Genesis would be helpful as they have a different number of commands. The logic for this implementation is also inspired by the JavaScript version found at snaptortoise.

### Codes

To capture the codes, I setup a simple helper class. This class defines the direction and touch keys and, also, the two cheat codes.

**(Insert code here)**

### Command Buffer

Since each cheat code has a different length, I decided to setup a simple class to capture and buffer the previous commands. The _CommandBuffer_ class will capture a fixed number of commands as specified in the constructor. The command buffer essentially acts as a wrapper around a linked list. New commands go to the end of the list and if the command list grows larger than the specified size, then commands are removed from the front. The _ProcessCommandList_ method allows a list of commands to be compared to the current buffer. Since commands are added to the end of the queue, I compare the end of my command queue to the command list provided to the method. The _SequenceEqual_ method uses the default equality comparer for an object, so if using a custom object, ensure that this has been implemented correctly. If there is a match, the buffer is cleared.

**(Insert code here)**

### Bringing It All Together

To capture the events in my activity, I decided to use the GestureDetector.IOnGestureListener interface as it provided a pretty easy way to get my movements. The OnFling and OnSingleTapUp methods do all command detection.

**(Insert code here)**

The ProcessCommand method processes the individual commands and performs an action depending on the code provided.

**(Insert code here)**

### Additional Lives

So, after a quick weekend coding session, I let the client know that their new release was going to have some additional goodies. They were skeptical at first, but a quick demo later and the client was sold. I am not sure if they ever shared the fact that the solution had a code in it, but I like to think that word of it spread over time.

The code for this project can be found on github at [EightBot.MonoDroid.CheatCode](http://github.com/michaelstonis/EightBot.MonoDroid.CheatCode).

Feel free to leave us a comment and let us know what you think. If you would like to add some cheats to your apps, contact us [here](/content/lets-talk/index.html).
