我正在尝试用6个不同字符中的4个填充数组,其中数组中的每个字符都不同。放入数组的字符基于switch语句。然后我想回到数组,以确保我刚刚放置的角色不在数组中,如果是,我必须放入一个不同的角色。我该怎么做呢?我不想重新填充整个阵列,只是这一点。
这是我到目前为止的代码,评论包括:
-(void) fillarray{
for (int i=0; i<4; i++)
{
int randomNum = arc4random() % 6; // generates a random number
switch (randomNum) // based on the number generated, it choses what color will be placed into the slot
{
case 1:
colorarray[i] ='r';
break;
case 2:
colorarray[i] ='o';
break;
case 3:
colorarray[i] ='y';
break;
case 4:
colorarray[i] ='g';
break;
case 5:
colorarray[i] = 'b';
case 6:
colorarray[i] = 'p';
default:
break;
}
for (int j=0; j<4; j++) // runs through the array to ensure that the recently placed color does not match previous placed colors
{
while (j /= i)
if (colorarray[i]==colorarray[j]) // if the color is already in the array
//try again to place another color in this location
}
答案 0 :(得分:1)
首先你拿一个数组(你也可以使用NSArray,我想你想要使用C风格的数组)来提取所有可能的字符:
char characters[]={'r','o','y','g','b','p'};
int length=6;
然后每次提取一个字符时,减少长度变量并交换最后一个提取的字符,以确保它不会再次使用:
int randomNum = arc4random() % length;
< put characters[randomNum] somewhere>
char temp=characters[randomNum];
characters[randomNum]=characters[length-1];
characters[length-1]=temp;
length--;
PS:%操作数返回商的余数,因此数字%N永远不会是N,它的范围从0到N-1。
答案 1 :(得分:1)
我会根据Fisher-Yates shuffling algorithm
使用此功能NSArray * array = @[ @"r", @"o", @"y", @"g", @"b", @"p" ] ;
array = [ [ array shuffledArray ] subarrayWithRange:(NSRange){ .length = 4 } ] ;
通过以下方式将-shuffledArray
添加到NSArray中:
@implementation NSArray ( Shuffling )
-(NSArray*)shuffledArray
{
NSMutableArray * result = [ NSMutableArray arrayWithCapacity:self.count ] ;
[ result addObject:self[0] ] ;
for( NSUInteger index=1, count = self.count; index < count; ++index )
{
int j = arc4random_uniform( (unsigned int)index ) ;
[ result addObject:result[j] ] ;
[ result replaceObjectAtIndex:j withObject:self[ index ] ] ;
}
return result ;
}
@end
答案 2 :(得分:0)
该函数应如下所示:
int found = 1;
for (int i=0; i<4; i++)
{
while(found) {
int randomNum = arc4random() % 6;
switch (randomNum)
{
case 1:
colorarray[i] ='r';
break;
case 2:
colorarray[i] ='o';
break;
case 3:
colorarray[i] ='y';
break;
case 4:
colorarray[i] ='g';
break;
case 5:
colorarray[i] = 'b';
break;
case 6:
colorarray[i] = 'p';
default:
break;
}
found = 0;
for (int j = i-1; i >= 0; j--) {
if (colorarray[i] == colorarray[j])
found = 1;
}
}
}
答案 3 :(得分:0)
此外,如果您不想使用switch或(基本上)通过递减长度变量来删除从数组中收到的字符,您可以始终执行此操作:
char colorarray[5] = {0,0,0,0,'\0'};
char possibleCharacters[6] = {'r', 'o', 'y', 'g', 'b', 'p'};
BOOL isNotUnique = NO;
for(int i = 0; i < 4; i++){
do{
colorarray[i] = possibleCharacters[arc4random() % 6];
for(int j = i - 1; j > -1; --j){
if(colorarray[i] == colorarray[j]){
isNotUnique = YES;
break;
}
else isNotUnique = NO;
}
}while(isNotUnique);
}
NSLog(@"%s", colorarray);
获取字符的实际循环基本上是:
1 for each spot in the colors arrays
2 generate a random number and use it to get a character
3 check to see if the character is identical to any spot before this one
4 if our new character matched any previous one, repeat from 2 on
答案 4 :(得分:0)
这是另一种改组数组的方法(获得随机的前X个元素)。基本上你做Ramy Alzury和nielsbot的回应。算法是这样的。
Do this 10 times the length of the array
swap the last element with a random element chosen from the array
最后这应该产生一个随机洗牌的数组(你将它洗牌10次)
这是目标C中的示例函数。
-(NSArray *) shuffleArray:(NSArray *) arr{
NSMutableArray * mArr = [NSMutableArray arrayWithArray:arr];
for(int i=0;i<arr.count*10;i++){
int num = arc4random()%arr.count;
NSString * temp = (NSString *)[mArr objectAtIndex:arr.count-1];
[mArr replaceObjectAtIndex: arr.count-1 withObject:[mArr objectAtIndex:num]];
[mArr replaceObjectAtIndex:num withObject:temp];
}
return [NSArray arrayWithArray:mArr];
}