0

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

这是我现在拥有的代码:

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

谁能给我一些明确的指示,告诉我如何在牌组用完之前阻止同一张牌出现两次?我将不胜感激。

4

3 回答 3

4

与其随机选择卡片,不如将所有卡片分配到一个数组中,然后将数组洗牌。之后,您可以遍历数组,从而随机选择卡片。

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

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

于 2012-11-18T14:36:29.513 回答
1

预加载包含所有图像名称的数组(或用于指示唯一卡的任何名称)。

对于每张卡片:根据数组中剩余的卡片数量计算随机数,选择卡片,从数组中删除其引用。

于 2012-11-18T14:34:26.150 回答
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吗?它是一个属性,您忘记使用点表示法并直接访问支持它的 ivar 吗?无论哪种方式,您都应该将其声明为某种属性/出口并通过访问器访问它。

于 2012-11-18T17:21:33.923 回答