随机化方形颜色

时间:2014-07-29 22:49:33

标签: ios objective-c

我的屏幕上有四个正方形,我希望每个正方形每次为每个正方形显示不同的图像。我有它工作,但由于代码重复有时会有延迟。我不确定我做错了什么。

这是我的主要代码

   #import "HomeScreenViewController.h"

@interface HomeScreenViewController ()

@end

@implementation HomeScreenViewController

-(void)startRandomSquares
{

    self.greenImage = [UIImage imageNamed:@"GreenSquare.png"];
    self.redImage = [UIImage imageNamed:@"RedSquare"];
    self.blueImage = [UIImage imageNamed:@"BlueSquare"];
    self.purpleImage = [UIImage imageNamed:@"PurpleSquare"];

    self.arrayOfColors = [NSMutableArray arrayWithObjects:self.greenImage,self.redImage, self.blueImage, self.purpleImage, nil];

}
-(IBAction)Button1:(id)sender
{
    self.startLabel.hidden = YES;
}
-(IBAction)Button2:(id)sender
{

}
-(IBAction)Button3:(id)sender
{

}
-(IBAction)Button4:(id)sender
{

}

-(void)randomizeAllSquares
{
    [self randomizedSquare1];
    [self randomizedSquare2];
    [self randomizedSquare3];
    [self randomizedSquare4];
}

-(void)randomizedSquare1
{

    [self startRandomSquares];

    randomColor = arc4random() % [self.arrayOfColors count];
    square1RandomNumber = randomColor;


   // NSLog(@"Square 1 : %d", square1RandomNumber);
    //NSLog(@"Square 1 colors : %@", self.arrayOfColors);

    switch (square1RandomNumber) {
        case 0:
            self.square1.image = self.arrayOfColors[0];
            break;

        case 1:
            self.square1.image = self.arrayOfColors[1];
            break;
        case 2:
           self.square1.image = self.arrayOfColors[2];
            break;
        case 3:
            self.square1.image = self.arrayOfColors[3];
            break;

        default:
            break;
    }


    if (square1RandomNumber == square2RandomNumber ) {
        [self randomizedSquare1];
        NSLog(@"Square 1-2 MATCH!!!");
    }
    if (square1RandomNumber == square4RandomNumber) {
        [self randomizedSquare1];
        NSLog(@"Square 1-4 Match!!!");
   }

}

-(void)randomizedSquare2
{
    [self startRandomSquares];


    randomColor = arc4random() % [self.arrayOfColors count];
    square2RandomNumber = randomColor;


     //NSLog(@"Square 2 : %d", square2RandomNumber);
    //NSLog(@"Square 2 colors : %@", self.arrayOfColors);

    switch (square2RandomNumber) {
        case 0:
            self.square2.image = self.arrayOfColors[0];
            break;

        case 1:
            self.square2.image = self.arrayOfColors[1];
            break;
        case 2:
            self.square2.image = self.arrayOfColors[2];
            break;
        case 3:
            self.square2.image = self.arrayOfColors[3];
            break;

        default:
            break;
    }

    if (square2RandomNumber == square1RandomNumber ) {
        [self randomizedSquare2];
        NSLog(@"Square 2-1 Match!!!");
    }
    if (square2RandomNumber == square4RandomNumber) {
        [self randomizedSquare2];
        NSLog(@"Square 2-4 Match!!!");
    }

}

-(void)randomizedSquare3
{
    [self startRandomSquares];

    randomColor = arc4random() % [self.arrayOfColors count];
    square3RandomNumber = randomColor;

    //NSLog(@"Square 3 : %d", square3RandomNumber);
    //NSLog(@"Square 3 colors : %@", self.arrayOfColors);

    switch (square3RandomNumber) {
        case 0:
            self.square3.image = self.arrayOfColors[0];
            break;

        case 1:
            self.square3.image = self.arrayOfColors[1];
            break;
        case 2:
            self.square3.image = self.arrayOfColors[2];
            break;
        case 3:
            self.square3.image = self.arrayOfColors[3];
            break;

        default:
            break;

    }
    if (square3RandomNumber == square2RandomNumber  ){
        [self randomizedSquare3];
        NSLog(@"Square 3 - 2 MATCH!!!");
    }
    if (square3RandomNumber == square1RandomNumber) {
        [self randomizedSquare3];
        NSLog(@"Square 3- 1 MATCH!!");
    }

}
-(void)randomizedSquare4
{
    [self startRandomSquares];

    randomColor = arc4random() % [self.arrayOfColors count];
    square4RandomNumber = randomColor;

    //NSLog(@"Square 4 : %d", square4RandomNumber);
    //NSLog(@"Square 4 colors : %@", self.arrayOfColors);

    switch (square4RandomNumber) {
        case 0:
            self.square4.image = self.arrayOfColors[0];
            break;

        case 1:
            self.square4.image = self.arrayOfColors[1];
            break;
        case 2:
            self.square4.image = self.arrayOfColors[2];
            break;
        case 3:
            self.square4.image = self.arrayOfColors[3];
            break;

        default:
            break;
    }
    if (square4RandomNumber == square2RandomNumber ) {
        [self randomizedSquare4];
        NSLog(@"Square 4-2 MATCH!!");
}
    if (square4RandomNumber == square1RandomNumber) {
        [self randomizedSquare4];
        NSLog(@"SQuare 4-1 MATCH!!");
    }
    if (square4RandomNumber == square3RandomNumber) {
        [self randomizedSquare4];
        NSLog(@"Square 4-3 Match");
    }
}

3 个答案:

答案 0 :(得分:2)

哎呀,这就是很多代码重复。一般情况下,如果您发现自己多次编写类似的代码片段,那么将其概括为最佳实践并不是最佳实践,但您最终可能会得到更好的解决方案。

你的数组上的一个简单的shuffle函数以及一个简单的重新分配就可以了,并且会给N一个运行时(即每次调用时循环只会通过一次数组)。你现在所拥有的技术会尝试多次创建一个正方形的颜色,如果它找到一个匹配,这是你的缓慢的原因。 Square 4有3/4的几率使它与另一个方块的颜色相同,因此在停止之前它可能至少运行3次。

- (void)shuffleImages
{
    //uses the Fisher-Yates shuffle algorithm
    for (NSUInteger i = self.arrayOfColors.count - 1; i >= 1; i--)
    {
        int index = arc4random_uniform(i+1);
        [self.arrayOfColors exchangeObjectAtIndex:index withObjectAtIndex:i];
    }
}

- (void)updateImages
{
    [self shuffleImages];
    self.square1.image = self.arrayOfColors[0];
    self.square2.image = self.arrayOfColors[1];
    self.square3.image = self.arrayOfColors[2];
    self.square4.image = self.arrayOfColors[3];
}

当您想要改变颜色时,您需要做的就是调用updateImages函数。

答案 1 :(得分:0)

延迟来自每次随机结束时的检查。生成的数字在其他3个方格中的一个中有75%的可能性,因此它将循环并循环,直到它生成唯一的数字。

更好的解决方案是将图像数组混洗,然后按顺序将每个图像分配给每个视图。

e.g。 (伪代码)

shuffle array of images

view 1 = array[0]
view 2 = array[1]
...
view 4 = array[3]

答案 2 :(得分:0)

Brandon对于疯狂的代码重复是正确的,并且使用fischer-yates算法绝对是正确的方法,但听起来你也想要重复这一点,所以你应该使用NSTimer

@property (strong, nonatomic) NSTimer *randomizeColorTimer;

- (void)startRandomizing
{
    _randomizeColorTimer = [NSTimer scheduledTimerWithTimeInterval:0.25
                                                            target:self
                                                          selector:@selector(shuffleImages)
                                                          userInfo:nil
                                                           repeats:YES];
}

- (void)stopRandomizing
{
    [_randomizeColorTimer invalidate];
    _randomizeColorTimer = nil;
}