I'm working on a tile based game idea in Javascript. It's a math puzzle game where players move around tiles with numbers on them, and the goal is to connect groups of tiles that have a sum of certain goal number, for example 5 or 7 or 9.
However, I'm stuck on the algorithm to detect these sum groups. I know how to do a flood fill algorithm using recursion to detect a group of adjacent tiles, like in the same game. But I don't know how to iterate through all the possible permutations of possible groups, and what is the best way to approach this?
Because the problem is that adjacent tiles form a group, but within that group of tiles there are many possibilities for sub groups. See tiles example below
Tile letters Contains numbers Represented as graph
+---+---+---+ +---+---+---+
| A | B | C | | 1 | 5 | 2 | 1---5---2
+---+---+---+ +---+---+---+ | |
| D | E | . | | 2 | 1 | . | 2---1
+---+---+---+ +---+---+---+ |
| F | . | . | | 4 | . | . | 4
+---+---+---+ +---+---+---+
So the group is all the tiles A though F, and a subgroup could be A, B and E (1+5+1) but also A, D and F (1+2+4). But A, C and F is not a possible group because those tiles are not adjacent. I suspect it could maybe be approached like it's a graph problem. But then I still don't know how to handle the many possible circular nodes.
So my question is; How to systematically go through and evaluate all possible permutations? Is there an algorithm for something like this? Or can someone explain how to approach this problem?
EDIT: I've put my code in the jsfiddle in the link below. It uses flood fill from a starting tile, in the example tile with nr 2. Then for each cell it counts to 15 and uses the bits of that counter to flood-fill to the 4 adjacent tiles (up,down,left,right). However it doesn't work properly because it doesn't consider branching paths. For example, in the jsfiddle example the combination 3-2-4 is never evaluated. https://jsfiddle.net/wu6p49r2/
I know how to do it by hand, see examples below. But what would the algorithm look like, to systematically gather these possible adjacent combinations starting at A?
Tiles ex.1 Tiles ex.2 Tiles ex.3
+---+---+ +---+---+---+ +---+---+---+
| A | B | | A | B | C | | A | B | C |
+---+---+ +---+---+---+ +---+---+---+
| D | | D | E | | D | E |
+---+ +---+---+ +---+---+
| F |
+---+
All possible adjacent combinations,
starting at A:
A A A
A-B A-B A-B
A-D A-B-C A-B-C
A-B-D A-B-E A-B-E
A-B-C-E A-B-C-E
A-B-E-D A-B-E-D
A-B-C-E-D A-B-C-E-D
A-D A-B-E-D-F
A-D-E A-B-C-E-D-F
A-D
A-D-E
A-D-F
A-D-E-F