Skip to content

Instantly share code, notes, and snippets.

@i-van
Created March 20, 2015 17:00
Show Gist options
  • Save i-van/08a60c8948c3615cd146 to your computer and use it in GitHub Desktop.
Save i-van/08a60c8948c3615cd146 to your computer and use it in GitHub Desktop.
Game.prototype.hasCombo = function() {
for (var index = 0, l = this.state.length; index < l; index++) {
var combo = [],
skip = [],
current,
next;
current = index;
while (true) {
combo.push(current);
if (combo.length >= MIN_SELECTION) {
return true;
}
// move right
next = current + 1;
if (this.state[current] === this.state[next] &&
(current + 1) % WIDTH !== 0 &&
!~combo.indexOf(next) &&
!~skip.indexOf(next)
) {
current = next;
continue;
}
// move down
next = current + WIDTH;
if (this.state[current] === this.state[next] &&
next < WIDTH * HEIGHT &&
!~combo.indexOf(next) &&
!~skip.indexOf(next)
) {
current = next;
continue;
}
// move left
next = current - 1;
if (this.state[current] === this.state[next] &&
current % WIDTH !== 0 &&
!~combo.indexOf(next) &&
!~skip.indexOf(next)
) {
current = next;
continue;
}
// try again w/o this index
if (combo.length > 1) {
skip.push(current);
current = index;
combo = [];
continue;
}
// try next index
break;
}
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment