如何创建多个图像的随机数字在iOS中随机显示而不重复?
我在SpriteKit.my中创建了GridView,要求是在网格视图中随机混洗图像。如何创建多个图像的随机数字在iOS中随机显示而不重复?
这是我的代码,随机显示图像没有重复,但此代码仅适用于两个images.for多个图像此代码不起作用。
int RandomRowMainImage = arc4random_uniform(3);
int RandomColumnMainImage = arc4random_uniform(3);
//
int RandomRowOtherImage = arc4random_uniform(3);
int RandomColumnOtherImage = arc4random_uniform(3);
NSLog(@"RandomRowMain:%d \n Random Column :%d \n RandomRow1:%d \n randomColumn1 :%d",RandomRowMainImage,RandomColumnMainImage,RandomRowOtherImage,RandomColumnOtherImage);
//
BOOL checkStatus = [self checkRandomNumberColumRowLogic:RandomRowMainImage withMainRow:RandomColumnMainImage withOtherColumn:RandomColumnOtherImage withOtherRow:RandomRowOtherImage];
if (checkStatus) {
imgIcons.position = [self GridPosition:MainRowCount Column:MainColumnCount];
imgOtherImage.position = [self GridPosition:otherRowCount Column:otherColumnCount];
}
比代码图像
//Grid Position
-(CGPoint)GridPosition:(int)Row Column:(int)Column
{
CGFloat offset = SizeOfGrid/2.0 + 0.5;
CGFloat x = Column * SizeOfGrid - (SizeOfGrid*TotalCol)/2.0 + offset;
CGFloat y = (TotalRows-Row-1) * SizeOfGrid -(SizeOfGrid*TotalRows)/2.0 + offset;
return CGPointMake(x, y);}
//代码的位置,以消除重复防止随机数的重复的两个图像。
- (BOOL)checkRandomNumberColumRowLogic:(int)MainColumn withMainRow:(int)mainRow withOtherColumn:(int)otherColumn withOtherRow:(int)otherRow {
BOOL CompareRow = false;
BOOL CompareColumn = false;
if (mainRow == otherRow) {
int otherRow = 0;
for (int i = 0; i < TotalCol; i++) {
otherRow = [self checkRandomNumberCompare:otherRow];
if (MainColumn == otherRow) {
CompareRow = true;
break;
}
}
MainColumnCount = mainRow;
otherColumnCount = otherRow;
}
else {
CompareRow = true;
MainRowCount = mainRow;
otherRowCount = otherRow;
}
if (MainColumn == otherColumn) {
int otherCol = 0;
for (int i = 0; i < TotalCol; i++) {
otherCol = [self checkRandomNumberCompare:otherColumn];
if (MainColumn == otherCol) {
CompareColumn = true;
break;
}
}
MainColumnCount = MainColumn;
otherColumnCount = otherCol;
}
else {
CompareColumn = true;
MainColumnCount = MainColumn;
otherColumnCount = otherColumn;
}
if(CompareRow == CompareColumn) {
return true;
} else {
return false;
}
}
-(int)checkRandomNumberCompare:(int)compareRow {
int compareDiff = arc4random_uniform(TotalRows);
return compareDiff;
}
可以请你帮不重复显示多个图像?像节点中的一次一个图像
对不起,但你的checkRandomNumberColumRowLogic:
方法的逻辑让我感到困惑。给定两个坐标(X1,Y1)和(x2,y2)那么它们代表相同的点当且仅当X1 == X2和Y1 == Y2,如果这不是再落下,他们代表不同的点。
下面是解决您的问题的可能算法的大纲。首先考虑给定矩形网格的单元格,其中行和列的编号从开始,然后可以通过将其行索引乘以列数并将其添加到列索引中来为每个单元格分配唯一编号。一图胜过千言万语,给定一个3×3格你的编号:
0 1 2
3 4 5
6 7 8
注意,给予细胞数行&列它代表可以用整数除法和余数来计算。
做上述降低您的问题,以生产从数字来rowCount时X colCount - 在某些随机顺序1。
有许多的方法,使你能做到这一点,这里是一个:
- 设置UPPERLIMIT到rowCount时X colCount - 1
- 产生一个随机数[R之间和UPPERLIMIT
- 检查电池[R被占用,如果是加到- [R并重复此步骤
- 将下一图像成细胞- [R
- 减去从UPPERLIMIT 和转到步骤2如果结果是更大的比(当然“GOTO”这里转换为一个“而”在代码)
它们键避免重复是步骤3中,并且该算法保证了步骤3总是会找到一个空闲的细胞 - 证明这只是一个练习。
HTH
我同意上面的答案,您的逻辑过于复杂。如果您按照建议为每个图像提供索引,例如对于一个3x4的网格:
0 1 2
3 4 5
6 7 8
9 10 11
然后,您可以随机化电网和交换这些图像。下面的代码将实现这一点:
-(void)createRandomGridArrays
{
NSInteger columnLength = 3;
NSInteger rowLength = 4;
// Create an array of NSNumbers up to the max then randomise
NSMutableArray *indexes = [NSMutableArray new];
for (NSInteger i=0; i<columnLength*rowLength; i++) {
[indexes addObject:@(i)];
}
NSArray *randomIndexes = [self shuffleArray:indexes];
NSLog(@"%@ -> %@", indexes, randomIndexes);
// (0,1,2,3,4,5,6,7,8,9,10,11) -> (1,0,6,10,4,2,7,11,9,5,8,3)
// Convert indexes back to row/columns
for (NSNumber *randomIndex in randomIndexes) {
NSInteger index = [randomIndex integerValue];
NSInteger row = index % rowLength;
NSInteger column = index % columnLength;
NSLog(@"%ld row:%ld, column:%ld", index, row, column);
}
}
-(NSArray*)shuffleArray:(NSArray*)array
{
NSMutableArray *shuffledArray = [array mutableCopy];
for (NSInteger i=shuffledArray.count-1; i>0; i--) {
[shuffledArray exchangeObjectAtIndex:i withObjectAtIndex:arc4random_uniform(i+1)];
}
return shuffledArray;
}
如果你有图像的一个NSMutableArray,你会然后就在index
与图像在[randomIndexes[index] integerValue]
交换图像。
比使用此代码后如何赋予图像位置 –
我给像这样的位置:imgIcons.position = [self GridPosition:MainRowCount Column: MainColumnCount]; ,如何给出行和列值 –
已更新答案,以包括使用模运算来计算行/列。 –
非常感谢。我会试着实现,并会让你知道 –