iPhoneマインスイーパー

今日は、簡易マインスイーパー作っていこうと思います。iPhoneアプリのサンプルコードです。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) UILabel *faceLabel;

@end

#define Width 320.0 / 9.0

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];

    [self createFace];

    [self createSheet];

}

– (void)createFace

{

    UILabel *face = [[UILabel alloc] initWithFrame:CGRectMake(125, 40, 70, 70)];

    face.backgroundColor = [UIColor yellowColor];

    face.text = @”-_-“;

    face.layer.cornerRadius = 35;

    face.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleCaption1] fontWithSize:30];

    face.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:face];

    

    self.faceLabel = face;

}

– (void)createSheet

{

    int bombCount = 0;

    for (int i=0; i<81; i++) {

        float x = (i % 9) * Width + Width/2.0;

        float y = (i / 9) * Width + 150;

        CALayer *cover = [self createCover];

        cover.position = CGPointMake(x, y);

        cover.zPosition = 1;

        [self.view.layer addSublayer:cover];

        

        float rand = arc4random() % 5;

        if (bombCount < 10 && rand == 0) {

            CALayer *bomb = [self createBomb];

            bomb.position = CGPointMake(x, y);

            bomb.zPosition = 0;

            [self.view.layer addSublayer:bomb];

            

            bombCount++;

        }

    }

}

– (CALayer*)createCover

{

    float w = Width;

    CALayer *cover = [CALayer layer];

    cover.frame = CGRectMake(0, 0, w, w);

    cover.name = @”cover”;

    cover.backgroundColor = [UIColor grayColor].CGColor;

    

    CALayer *bottom = [CALayer layer];

    bottom.frame = CGRectMake(0, w * 0.9, w, w * 0.1);

    bottom.backgroundColor = [UIColor lightGrayColor].CGColor;

    [cover addSublayer:bottom];

    CALayer *right = [CALayer layer];

    right.frame = CGRectMake(w * 0.9, 0, w * 0.1, w);

    right.backgroundColor = [UIColor lightGrayColor].CGColor;

    [cover addSublayer:right];

    

    CALayer *top = [CALayer layer];

    top.frame = CGRectMake(0, 0, w, w * 0.1);

    top.backgroundColor = [UIColor darkGrayColor].CGColor;

    [cover addSublayer:top];

    CALayer *left = [CALayer layer];

    left.frame = CGRectMake(0, 0, w * 0.1, w);

    left.backgroundColor = [UIColor darkGrayColor].CGColor;

    [cover addSublayer:left];

    

    return cover;

}

– (CALayer*)createBomb

{

    CALayer *bomb = [CALayer layer];

    bomb.name = @”bomb”;

    bomb.frame = CGRectMake(0, 0, 20, 20);

    bomb.backgroundColor = [UIColor blackColor].CGColor;

    bomb.cornerRadius = 10;

    return bomb;

}

– (void)checkCover:(CALayer*)cover

{

    CALayer *bomb = [self checkBombUnderCover:cover];

    if (bomb) {

        // game over

        self.faceLabel.text = @”x_x”;

        [self performSelector:@selector(restart) withObject:nil afterDelay:3.0];

        

    } else {

        // check around

        NSArray *bombArr = [self checkBombAround:cover];

        UILabel *label = [[UILabel alloc] initWithFrame:cover.frame];

        label.text = [@(bombArr.count) stringValue];

        label.backgroundColor = [UIColor clearColor];

        label.textColor = [UIColor greenColor];

        label.textAlignment = NSTextAlignmentCenter;

        label.font = [UIFont boldSystemFontOfSize:30];

        [self.view addSubview:label];

    }

    [cover removeFromSuperlayer];

}

– (CALayer*)checkBombUnderCover:(CALayer*)cover

{

    NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {

        if ([[evaluatedObject name] isEqualToString:@”bomb”]) {

            return CGRectContainsPoint(cover.frame, [evaluatedObject position]);

        }

        return NO;

    }];

    

    NSArray *arr = [self.view.layer.sublayers filteredArrayUsingPredicate:pred];

    return [arr count] ? arr[0] : nil;

}

– (NSArray *)checkBombAround:(CALayer*)cover

{

    float x = cover.frame.origin.x – cover.frame.size.width;

    float y = cover.frame.origin.y – cover.frame.size.height;

    float w = cover.frame.size.width * 3.0;

    float h = cover.frame.size.height * 3.0;

    CGRect around = CGRectMake(x, y, w, h);

    NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {

        if ([[evaluatedObject name] isEqualToString:@”bomb”]) {

            return CGRectContainsPoint(around, [evaluatedObject position]);

        }

        return NO;

    }];

    

    return [self.view.layer.sublayers filteredArrayUsingPredicate:pred];

}

– (void)restart

{

    [self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        [obj removeFromSuperview];

    }];

    

    self.view.layer.sublayers = nil;

    

    [self createFace];

    [self createSheet];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    

    NSPredicate *pred = [NSPredicate predicateWithFormat:@”name == %@”, @”cover”];

    [[self.view.layer.sublayers filteredArrayUsingPredicate:pred] enumerateObjectsUsingBlock:^(CALayer *l, NSUInteger idx, BOOL *stop) {

        if (CGRectContainsPoint(l.frame, p)) {

            [self checkCover:l];

        }

    }];

}

@end