Building Combinations From Array(s) (Recursion?)

Raven65

Well-known member
Joined
Oct 4, 2006
Messages
305
Programming Experience
3-5
Now, this is going to be a good bit confusing I think so bear with me:

I have a need to do the following:

Assume I have 5 "slots", and for each slot I have 5 items that can go in it.

I'm building a list of all possible combinations, so I end up with a list much like:

Slot1Item1 Slot2Item1 Slot3Item1 Slot4Item1 Slot5Item1
Slot1Item1 Slot2Item1 Slot3Item1 Slot4Item1 Slot5Item2
Slot1Item1 Slot2Item1 Slot3Item1 Slot4Item1 Slot5Item3
Slot1Item1 Slot2Item1 Slot3Item1 Slot4Item1 Slot5Item4
Slot1Item1 Slot2Item1 Slot3Item1 Slot4Item1 Slot5Item5
Slot1Item1 Slot2Item1 Slot3Item1 Slot4Item2 Slot5Item1
... all the way to
Slot1Item5 Slot2Item5 Slot3Item5 Slot4Item5 Slot5Item4
Slot1Item5 Slot2Item5 Slot3Item5 Slot4Item5 Slot5Item5

This I have built, and its working fine.

Now:

For each "set" (a set is 1 "item" from each "slot"), there is a variable number of "mods" that can be added. So, "set1" might have space for 3 "mods", and "set2" might have room for 2 "mods". The number of "mods" is known as part of the "set" structure. A mod is repeatable, and posistion DOES matter (see below).

I have a list of available "mods" stored to the side (so this number is known @ run time), and now need to try each combination of "mods" for each constructed "set".

My current approach is this:

Build the mod list based on the highest number of potenial mods needed by any set, so I get an array like this:

Mod1 Mod2 Mod3 Mod4 Mod5
Mod1 Mod2 Mod3 Mod4 Mod5
Mod1 Mod2 Mod3 Mod4 Mod5

Where the number of rows is enough to cover the highest number of potential mods.

My thinking is that I could drive down the list and get the following:

I would start with the set:

Slot1Item1 Slot2Item1 Slot3Item1 Slot4Item1 Slot5Item1

And determine the mods, lets say this one was room for 3 mods:

Mod1 Mod1 Mod1 (this is allowed remember)
Mod1 Mod1 Mod2
Mod1 Mod1 Mod3
Mod1 Mod2 Mod1 (this is not the same as Mod1 Mod1 Mod2 above)
Mod1 Mod2 Mod2
Mod1 Mod2 Mod3
to ..
Mod5 Mod5 Mod4
Mod5 Mod5 Mod5


But, my brain is fried. I know that I need (or im 99% sure) to use recursion to crawl the array like this, but I simply cannot picture the code ATM.

Any thoughts from anyone? (Or thoughts on a better way?)

Hope I was clear (enough) !
 
Back
Top