Skip to content

Instantly share code, notes, and snippets.

@claytical
Created January 26, 2014 21:08
Show Gist options
  • Save claytical/8639537 to your computer and use it in GitHub Desktop.
Save claytical/8639537 to your computer and use it in GitHub Desktop.
Recursive function for finding matches in a vector grid
void testApp::findMatchingColors(Candy &c) {
float upNeighborX = c.position.x;
float upNeighborY = c.position.y - GRID_SQUARE_SIZE;
float downNeighborX = c.position.x;
float downNeighborY = c.position.y + GRID_SQUARE_SIZE;
float leftNeighborX = c.position.x - GRID_SQUARE_SIZE;
float leftNeighborY = c.position.y;
float rightNeighborX = c.position.x + GRID_SQUARE_SIZE;
float rightNeighborY = c.position.y;
for (int i = 0; i < candies.size() - 1; i++) {
if (!candies[i].matched) {
//UP
if (candies[i].position.x == upNeighborX && candies[i].position.y == upNeighborY) {
if (candies[i].type == c.type) {
candies[i].matched = true;
}
}
//DOWN
if (candies[i].position.x == downNeighborX && candies[i].position.y == downNeighborY) {
if (candies[i].type == c.type) {
candies[i].matched = true;
}
}
//LEFT
if (candies[i].position.x == leftNeighborX && candies[i].position.y == leftNeighborY) {
if (candies[i].type == c.type) {
candies[i].matched = true;
}
}
//RIGHT
if (candies[i].position.x == rightNeighborX && candies[i].position.y == rightNeighborY) {
if (candies[i].type == c.type) {
candies[i].matched = true;
}
}
if (candies[i].matched) {
candiesCollected++;
c.matched = true;
candySounds[candies[i].subtype].play();
findMatchingColors(candies[i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment