1

我有 2 个图像,我有 4 个图像视图我想在 2-2 个图像视图上随机设置 2 个图像。

就像纸牌游戏一样。在该用户选择 1 张卡时,如果图像相同,则用户还有 3 个选项可以打开相同的图像,然后用户在再次单击后赢得游戏。请帮帮我...

int r =arc4random()%4;
int z=arc4random()%4;
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:r];
imageView.image=[UIImage imageNamed:@"30-by-30-home"];

if(r!=z)
{
    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z];
    imageView.image=[UIImage imageNamed:@"30-by-30-home"];

}else{

     if(r==3)
     {
         UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z-2];
         imageView.image=[UIImage imageNamed:@"30-by-30-home"];
     }
    if(r==0)
    {
        UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z+2];
        imageView.image=[UIImage imageNamed:@"30-by-30-home"];


    }if(r==1)
    {
        UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z+2];
        imageView.image=[UIImage imageNamed:@"30-by-30-home"];
    }
    if(r==2)
    {
        UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z-2];
        imageView.image=[UIImage imageNamed:@"30-by-30-home"];
    }

}
4

2 回答 2

0

arc4random() 是生成随机数的函数的名称。

像 arc4random()%3 一样使用它来生成随机数。根据随机数选择图像。

您应该生成两个不同的随机数。

int r =arc4random()%4;
int z=arc4random()%4;

 while (r==z)
 {
      z = arc4random()%4;
      if(r != z)
         break;
 }

UIImageView *imageView = (UIImageView *)[self.view viewWithTag:r];
imageView.image=[UIImage imageNamed:@"30-by-30-home"];

UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z];
imageView.image=[UIImage imageNamed:@"30-by-30-home"];
于 2013-03-04T08:31:26.483 回答
0

对于两者r==3&r==2你使用[self.view viewWithTag:z-2].

对于两者r==0&r==1你使用[self.view viewWithTag:z+2].

这可能是它的原因吗?

于 2013-03-04T08:49:15.070 回答