Created
March 20, 2015 17:00
-
-
Save i-van/08a60c8948c3615cd146 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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