如何阻止我的随机图像生成器重复?

时间:2012-11-18 14:29:10

标签: objective-c

我正在尝试创建一个简单的应用程序,允许您从卡组中挑选一张卡片。我需要它做的是不要重复相同的卡。我看过整个互联网,我不清楚如何做到这一点。

以下是我现在的代码:

-(IBAction) randomize: (id)sender {

    int randomcards = rand() % 53 + 1;
    switch (randomcards) {
        case 0:
            imageview.image = [UIImage imageNamed:@"1.png"];
            break;
        case 1:
            imageview.image = [UIImage imageNamed:@"2.png"];
            break;
        case 2:
            imageview.image = [UIImage imageNamed:@"3.png"];
            break;
        case 3:
            imageview.image = [UIImage imageNamed:@"4.png"];
            break;
        case 4:
            imageview.image = [UIImage imageNamed:@"5.png"];
            break;
        case 5:
            imageview.image = [UIImage imageNamed:@"6.png"];
            break;
        case 6:
            imageview.image = [UIImage imageNamed:@"7.png"];
            break;
        case 7:
            imageview.image = [UIImage imageNamed:@"8.png"];
            break;
        case 8:
            imageview.image = [UIImage imageNamed:@"9.png"];
            break;
        case 9:
            imageview.image = [UIImage imageNamed:@"10.png"];
            break;
        case 10:
            imageview.image = [UIImage imageNamed:@"11.png"];
            break;
        case 11:
            imageview.image = [UIImage imageNamed:@"12.png"];
            break;
        case 12:
            imageview.image = [UIImage imageNamed:@"13.png"];
            break;
        case 13:
            imageview.image = [UIImage imageNamed:@"14.png"];
            break;
        case 14:
            imageview.image = [UIImage imageNamed:@"15.png"];
            break;
        case 15:
            imageview.image = [UIImage imageNamed:@"16.png"];
            break;
        case 16:
            imageview.image = [UIImage imageNamed:@"17.png"];
            break;
        case 17:
            imageview.image = [UIImage imageNamed:@"18.png"];
            break;
        case 18:
            imageview.image = [UIImage imageNamed:@"19.png"];
            break;
        case 19:
            imageview.image = [UIImage imageNamed:@"20.png"];
            break;
        case 20:
            imageview.image = [UIImage imageNamed:@"21.png"];
            break;
        case 21:
            imageview.image = [UIImage imageNamed:@"22.png"];
            break;
        case 22:
            imageview.image = [UIImage imageNamed:@"23.png"];
            break;
        case 23:
            imageview.image = [UIImage imageNamed:@"24.png"];
            break;
        case 24:
            imageview.image = [UIImage imageNamed:@"25.png"];
            break;
        case 25:
            imageview.image = [UIImage imageNamed:@"26.png"];
            break;
        case 26:
            imageview.image = [UIImage imageNamed:@"27.png"];
            break;
        case 27:
            imageview.image = [UIImage imageNamed:@"28.png"];
            break;
        case 28:
            imageview.image = [UIImage imageNamed:@"29.png"];
            break;
        case 29:
            imageview.image = [UIImage imageNamed:@"30.png"];
            break;
        case 30:
            imageview.image = [UIImage imageNamed:@"31.png"];
            break;
        case 31:
            imageview.image = [UIImage imageNamed:@"32.png"];
            break;
        case 32:
            imageview.image = [UIImage imageNamed:@"33.png"];
            break;
        case 33:
            imageview.image = [UIImage imageNamed:@"34.png"];
            break;
        case 34:
            imageview.image = [UIImage imageNamed:@"35.png"];
            break;
        case 35:
            imageview.image = [UIImage imageNamed:@"36.png"];
            break;
        case 36:
            imageview.image = [UIImage imageNamed:@"37.png"];
            break;
        case 37:
            imageview.image = [UIImage imageNamed:@"38.png"];
            break;
        case 38:
            imageview.image = [UIImage imageNamed:@"39.png"];
            break;
        case 39:
            imageview.image = [UIImage imageNamed:@"40.png"];
            break;
        case 40:
            imageview.image = [UIImage imageNamed:@"41.png"];
            break;
        case 41:
            imageview.image = [UIImage imageNamed:@"42.png"];
            break;
        case 42:
            imageview.image = [UIImage imageNamed:@"43.png"];
            break;
        case 43:
            imageview.image = [UIImage imageNamed:@"44.png"];
            break;
        case 44:
            imageview.image = [UIImage imageNamed:@"45.png"];
            break;
        case 45:
            imageview.image = [UIImage imageNamed:@"46.png"];
            break;
        case 46:
            imageview.image = [UIImage imageNamed:@"47.png"];
            break;
        case 47:
            imageview.image = [UIImage imageNamed:@"48.png"];
            break;
        case 48:
            imageview.image = [UIImage imageNamed:@"49.png"];
            break;
        case 49:
            imageview.image = [UIImage imageNamed:@"50.png"];
            break;
        case 50:
            imageview.image = [UIImage imageNamed:@"51.png"];
            break;
        case 51:
            imageview.image = [UIImage imageNamed:@"52.png"];
            break;
        case 52:
            imageview.image = [UIImage imageNamed:@"53.png"];
            break;
        case 53:
            imageview.image = [UIImage imageNamed:@"54.png"];
            break;
    }

任何人都可以给我一些明确的指示,说明在牌组用完之前如何阻止同一张牌出现两次?我将不胜感激。

3 个答案:

答案 0 :(得分:4)

不是挑选随机卡,而是将所有卡分配到一个数组中,然后随机播放阵列。之后,您可以遍历数组,从而随机选择卡片。

请参阅此问题whats-the-best-way-to-shuffle-an-nsmutablearray

看看Gregory Goltsov使用arc4random_uniform()的答案

答案 1 :(得分:1)

使用您所有图片的名称(或用于指示唯一卡片的任何图片)预加载阵列。

对于每张卡:根据阵列中剩余的卡数计算随机数,选择卡,从阵列中删除其参考。

答案 2 :(得分:0)

正如其他人所指出的,最简单的方法是创建一个图像名称数组并删除已经使用过的名称。这是一个例子。在标题中声明这样的属性。

@property (strong, nonatomic) NSMutableArray *availableCards;

在实施的顶部合成它。

@synthesize availableCards = _availableCards;

使用方便的方法让你初始化/重置它。

- (void) resetAvailableCards {
    NSMutableArray *resetArray = [[NSMutableArray alloc] init];
    //the temporary array to store what we'll set our property to at the end

    for (int i = 1; i < 55; ++i){//for every card in the deck
        NSString* imageName = [NSString stringWithFormat:@"%d.png",i];//make the image name
        [resetArray addObject:imageName];//store it in the array
    }

    self.availableCards = resetArray;//set our property to the array
}

在控制器的viewDidLoad中调用它以初始化状态。

[self resetAvailableCards];

您还可以在需要再次使用所有卡时调用它。现在,为您的原始方法。

- (IBAction) randomize: (id) sender {
    int cardIndex = arc4random() % [self.availableCards count];//random number within the bounds of the array; no need to add 1 as count is 1 higher than highest available index
    NSString* cardImageName = [self.availableCards objectAtIndex: cardIndex];//get the name of the card we chose
    imageview.image = [UIImage imageNamed: cardImageName];//set the image
    [self.availableCards removeObjectAtIndex:cardIndex];//remove the card name so we can't get it again
    return;
}

注意我使用的是arc4random而不是rand。它具有两倍的有效范围,并且倾向于提供更好的分布以及不需要播种。如果您想有效地使用rand,则需要使用srand为时间或其他内容播种。我还建议你删除动作中的sender参数,因为它没有被使用,虽然你也必须在其他地方更改它,所以你可能想把它保留。

只是想知道,但是imageview是什么?既然你没有使用self.imageview,那它是一个伊娃吗?它是一个属性,你忘了使用点符号,并直接访问ivar支持它?无论哪种方式,您都应该将其声明为某种属性/出口,并通过访问器访问它。