Konami Code Detection With Mono For Android


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.

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.

Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
Xamarin Partner
LINKS
ABOUT

contact@eightbot.com

Tel: 1-855-727-8246

318 W Adams St

Chicago, IL 60606

SOCIAL
  • White Facebook Icon
  • White Twitter Icon
  • White LinkedIn Icon
  • White Google+ Icon

© 2017 | Eight-Bot, Inc.