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];
}];
}