HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Contra - 30 More Lives

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
livescontramore

Problem

Just for the fun of it, I wanted to have a hidden keypress function on my website that performs an event when the user types in the Contra code for thirty lives. I'm curious how the implementation for this simple piece of code holds up and where it could be improved.

Note: keypress doesn't seem to detect the arrow keys so I used the arrows from the numpad

var codeCount = 0;

//[up, up, down, down, left, right, left, right, b, a, start]
var contraCode = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13];

$(window).keydown(function(input) {
  var key = input.which;
  if (contraCode[codeCount] === key) {
     codeCount++;

     //if 11 keys have sequentially matched the cheat code 
     if (codeCount === 11) {
        alert('You got 30 lives!');
     }
  }
  else {
     codeCount = 0;
  }
});

Solution

One thing you could improve is to remove the jQuery dependency and just use addEventListener directly. This should be fine as all modern browsers support this already. Although, the tradeoff would be to normalize your keycodes yourself.

Also, I would suggest separating the code that updates codeCount from the code that checks if you achieved all keys in sequence. This way, there's clear separation in logic. The one problem I often come across with code not clearly separating logic is that in the long run, code becomes hard to untangle. Kill this habit while still early.

Taking it a bit further, you can assign the key values to an object. This way, you can represent the key in code using a enum-like structure and avoid hardcoding keycodes all over your code. This also allows you to change the keycode value without having to dive in to the code to find and change keycodes everywhere.

var codeCount = 0;

var Keys = {
  UP: 38,
  DOWN: 40,
  LEFT: 37,
  RIGHT: 39,
  B:66,
  A: 65,
  START: 13,
];

//[up, up, down, down, left, right, left, right, b, a, start]
var contraCode = [
  Keys.UP,
  Keys.UP,
  Keys.DOWN,
  Keys.DOWN,
  Keys.LEFT,
  Keys.RIGHT,
  Keys.LEFT,
  Keys.RIGHT,
  Keys.B,
  Keys.A,
  Keys.START,
];

window.addEventListener('keydown', function(event){

  var key = event.keyCode? event.keyCode : event.charCode;

  codeCount = contraCode[codeCount] === key ? codeCount + 1 : 0;

  if(codeCount === 11) alert('You got 30 lives!');

});

Code Snippets

var codeCount = 0;

var Keys = {
  UP: 38,
  DOWN: 40,
  LEFT: 37,
  RIGHT: 39,
  B:66,
  A: 65,
  START: 13,
];

//[up, up, down, down, left, right, left, right, b, a, start]
var contraCode = [
  Keys.UP,
  Keys.UP,
  Keys.DOWN,
  Keys.DOWN,
  Keys.LEFT,
  Keys.RIGHT,
  Keys.LEFT,
  Keys.RIGHT,
  Keys.B,
  Keys.A,
  Keys.START,
];

window.addEventListener('keydown', function(event){

  var key = event.keyCode? event.keyCode : event.charCode;

  codeCount = contraCode[codeCount] === key ? codeCount + 1 : 0;

  if(codeCount === 11) alert('You got 30 lives!');

});

Context

StackExchange Code Review Q#135540, answer score: 7

Revisions (0)

No revisions yet.