1

I have trouble finding a good solution (and a good title) to the following problem:

Say I have a blueprint of a sphere. This sphere-blueprint contains an array of unique colors. Each sphere will take on a random color of its color array after spawning.

Now I have a Spawner, which holds a list of say 100 different sphere-blueprints it could spawn. Each of these can have differing color arrays. The Spawner now tries to spawn 10 spheres from that list.

After spawning its now time for the spheres to take on their colors. However, no sphere can take on a color another sphere has taken on before! The first sphere takes on a random color from its color array. Each subsequent sphere is are able to see the color previous spheres have taken on and are able to chose a color that hasn't been chosen by any previous sphere.

How can I make sure that I don't spawn a sphere, which could be left with no more color-choices, because all colors within its color-array have been already taken by other spheres?

I tried to prototype this using sticky notes with numbers on them, but it sure is not a trivial problem

Florian Wolf
  • 171
  • 7
  • What is a "blueprint of a sphere"? What does spawning mean? The spawning tag does not seem to have any information! – ravenspoint Jun 24 '22 at 14:49
  • 1
    You have a lot of unfamiliar, undefined terms in your question ( blueprint, sphere, spawning, ... ) As far as I can make out they are all irrelevant to the question, which concerns the creation of color arrays and selection of a color. It seems that you could describe your question referring only to color arrays and color selection. Please try doing that. – ravenspoint Jun 24 '22 at 14:57
  • If you have lots of colors and lots of spheres, with high probability you'll get an answer in a few random tries. Is that good enough, or do you need a solution that will work when the constraint eliminates the vast majority of random guesses? – btilly Jun 24 '22 at 16:06
  • You have some colors, and you have some spheres that may take on those colors. What you want is called a "maximum bipartite matching" between those spheres and colors. – Matt Timmermans Jun 24 '22 at 18:30
  • You are thinking of [picking a random ball from a bag without replacement](https://math.stackexchange.com/q/1928063). – Neil Jun 24 '22 at 20:11

1 Answers1

2

Each of your unique "blueprint" needs to contain the following...

  • ColorArray - pre shuffle it so it's already random
  • ColorPickerIndex - 0 to size of Color Array

Once your list of "100 blueprints" is created you need to combine all of the unique colors into a GlobalColorHashSet.

Each "spawn" (instance) of a "blueprint" needs to contain the following...

  • Index - into the list of "100 blueprints"
  • Color - the chosen unique color

Example spawn blueprint method....

// list of  "100 blueprints"
var blueprints = ...
// set of all unique colors in all of the "blueprints" 
var globalColorsHashSet = ...
// list of "spawn" instances
var spawned = [];

// spawn a blueprint or throw an error
function spawnBlueprint(blueprintIndex) {
  // get the requested blueprint
  var bp = blueprints[blueprintIndex];
  // attempt to spawn the blueprint
  while(true) {
    // blueprint has exhausted its colors so throw an error
    if(bp.colorPickerIndex >= bp.colorArray.length) {
      throw "No Colors Left for this blueprint.";
    }
    // store possible spawn color
    var color = bp.colorArray[colorPickerIndex];
    // increment the color picker index
    bp.colorPickerIndex++;
    // if the color hasn't been used yet, then spawn an instance
    if(globalColorHashSet.has(color)) {
      // remove the color from the global list
      globalColorHashSet.delete(color);
      // create the instance
      var spawn = new Spawn(blueprintIndex, color);
      // save the spawn instance
      spawned.push(spawn);
      // return the spawn instance
      return spawn;
    }
    // loop - try the next color
  }
}
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
  • This is a good solution, but I think it's overly complex. Anything with a [permutation pre-calculated](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) will give the behaviour. Just increment the index every time would be fine. (No need for `globalColorHashSet`, as all the colours are already unique?) – Neil Jun 24 '22 at 20:14
  • 1
    @Neil - from the question: "Now I have a Spawner, which holds a list of say 100 different sphere-blueprints it could spawn. Each of these can have differing color arrays" This *seems* like the color arrays can have overlapping colors in them. Blueprint 1 has red, white, blue; Blueprint2 has white, blue, green. If this is not the case, then I agree if every Blueprint's color array was completely unique with no overlapping colors from other Blueprints then the global hash set would be unnecessary. – Louis Ricci Jun 24 '22 at 20:48