Pick 4 random elements from an NSArray of count 6
I am new to Objective C. I am coding the game Mastermind where the
computer chooses 4 random colors out of 6 and the user tries to guess the
4 colors in 6 tries.
I have an NSArray to represent all the six possible colors here:
NSArray * allColors = [[NSArray alloc] initWithObjects:@"r", @"g",
@"b", @"y", @"p", @"o", nil];
//Computer choose 4 random colors:
NSArray * computersSelection = [[NSArray alloc] init];
I need to write code to choose 4 UNIQUE random colors from the array. Is
there a smart way to do this?
I can create four int variables and use a while loop to generate four
random numbers and then pull the objects from the NSArray based on the
four random integer values and put them in the computerSelection array but
I am wondering if there's any simpler way of doing things?
Thanks
No comments:
Post a Comment