I am looking to have a C# application implement the Konami Code to display an Easter Egg. http://en.wikipedia.org/wiki/Konami_Code
What is the best way to do this?<
I've read all the answers, and found that repeated input of the initial of the sequence was a common problem of implementations. The following is a simple implementation without encountering the repeat of initial problem. No special cases, nothing is really hard-coded, the integers specified in the class is only for a default value.
public partial class KonamiCode {
public bool IsCompletedBy(int keyValue) {
for(var i=sequence.Count; i-->0; ) {
if(sequence[i]!=keyValue) {
if(0==i)
count=0;
continue;
}
if(count!=i)
continue;
++count;
break;
}
var isCompleted=sequence.Count==count;
count=isCompleted?0:count;
return isCompleted;
}
public KonamiCode(int[] sequence=default(int[])) {
this.sequence=
sequence??new[] { 38, 38, 40, 40, 37, 39, 37, 39, 66, 65 };
}
int count;
IList sequence;
public static readonly KonamiCode Default=new KonamiCode();
}