-1

I'm creating a game with SpriteKit. I have 8 different colored balls positioned at 8 different designated CGPoints on the screen. Once the user gets to a certain score, I would like to randomize the colors of the balls to all be different colors, but I would like to get this result without any of the colors and types duplicating.

I added the balls as objects to a global NSMutableArray and set up a method to enumerate the array. I then wrote an arc4random method to pick a random color type from the array and then apply it to the old ball type. Unfortunately I am getting some duplicates. Does anybody have any suggestions to help me randomize my ball types without duplication?

FYI, I have taken ample time out to read other randomization methods and none of them seem to necessarily answer my question. I am on a deadline. Can somebody please help me?

-(void)ballRotation{


    NSLog(@"initial ball list: %@",_ballList);

    [_ballList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        int selectedIndex = arc4random() % _ballList.count;
        NSLog(@"Selected Index: %i", selectedIndex);

        //get a remain list of temp
        Ball *newBall = _ballList[idx];

        //get the ball at the current _ballList index
        Ball *oldBall = _ballList[selectedIndex];

        //change the ball in the old position to have the type & texture of the randomly selected ball
        oldBall.Type = newBall.Type;
        oldBall.texture = newBall.texture;

        NSLog(@"new ball list: %lu", newBall.Type);
        NSLog(@"new ball list: %@", newBall.texture);


        [_ballList removeObjectAtIndex:selectedIndex];

    }];
 }
0x141E
  • 12,613
  • 2
  • 41
  • 54
John
  • 23
  • 2

2 Answers2

0

Create a mutable array that stores the colors that get selected. Each time you randomize and get a color, compare that color to all the colors stored in the "alreadyChosenColor" array. If the colors are equal, randomize again until it finally does not match a color already existing in the array.

Code:

//Create an array named allColorArray with all colors in it that you will use
bool unique = NO;
while(unique==NO)
{
unique = YES;
//randomIndex is a random int in the range of the allColorArray
randomColor = [allColorArray objectAtIndex:randomIndex]
    for(int i=0;i<alreadyChosenColor.count;i++)
    {
        if([alreadyChosenColor objectAtIndex:i]== randomColor)
            unique=false;
    }
}
//Set SKSpritenode to use randomColor.
//add randomColor to alreadyChosenColor array.
meisenman
  • 1,818
  • 1
  • 15
  • 25
0

Create the array with all the colors. Shuffle it and assign to each ball in order. See Fisher-Yates Shuffle. Here's a category to do it:

#import <Foundation/Foundation.h>

@interface NSMutableArray (KD)
-(void) kd_shuffleArray;
@end

@implementation NSMutableArray (KD)

-(void) kd_shuffleArray {

    NSUInteger count = self.count;

    for (int i=count-1; i>0; i--) {
        int random = arc4random_uniform(i+1);
        [self exchangeObjectAtIndex:i withObjectAtIndex:random];
    }
}

@end

Oh, and you never want to add/remove to an array while enumerating. It's one of the deadly sins.

Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41