I am writing an app in javascript to try to figure out item builds for characters in a video game. There are about 25 top-tier items, and you can carry 6 at a time. They have very different effects, which leads me to believe that while one item may seem like it isn't very good by itself, it can become much stronger when combined with others. I can elaborate on that if there is interest.
Questions:
How can I get a list of all the different distinct combinations of 6 items? How many combinations will there be? Is it just 25c6 (~134k)? Or do I need to remove duplicates? (Sorry, I've been out of math class awhile.)
How would you implement something like this in Javascript? Is there already a math library that can do this? (Specifically, iterate through all of the possible combinations of items.)
Does it seem possible to brute force calculate the damage of all the possible combinations and save the top item combinations? If not, is there a better algorithm to find strong combinations?
Here's my code, based on everyone's input:
function getAllCombinations(n, k, callback)
{
var iterate = function(remaining, args)
{
var len = args.length;
for (var i = args[len - 1]; i < n; i++)
{
args.splice(len);
args[len - 1] = i;
if (remaining)
{
args.push(i);
iterate(remaining - 1, args);
}
else
{
callback.apply(null, args);
}
}
}
iterate(k - 1, [0]);
}
var itemsCount = 25;
var itemSlots = 6;
getAllCombinations(itemsCount, itemSlots, function(a, b, c, d, e, f)
{
// calculateDamage(hero, arguments);
});
1) Yes, it is just 25 choose 6
2) Well, if you only have to do this once, you can do it with nested loops. The key is to have each of the inner loops not start from zero, but from the outer counter.
for (int i = 0; i < 25; i++) {
for (int j = i; j < 25; j++) { // note j=i not j=0
// etc
foo(i,j,k,l,m,n);
}
}
If you need a generic solution for generic values of 25 and 6 it shouldn't be hard to write a recursive function with similar effects.
3) I think your only option is brute force. It may take a few minutes, but it should complete. I think it will be fastest in Chrome and unusable in IE. Other options like "local search techniques" don't seem like they would work for you because your space is not particularly continuous.