iPhoneチューリングマシン 1を足す

チューリングマシンで1を足すというのはこんな感じで動くのでは、、というiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *tape;

@property (nonatomic, weak) UIView *anchor;

@property (nonatomic) int index;

@property (nonatomic, strong) SEL (^condition)();

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1];

    

    self.index = 2;

    

    id __weak weakSelf = self;

    self.condition = ^SEL{ return [weakSelf conditionA]; };

    

    [self createInitialTape];

    [self updateAnchor];

    [self createStartButton];

}

– (void)createInitialTape

{

    NSString* (^randOneToNine)() = ^NSString* {

        return [@(arc4random() % 9 + 1) stringValue];

    };

    // random number 111 ~ 999

    NSArray *digit = @[randOneToNine(),randOneToNine(),randOneToNine()];

    

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

        UITextField *t = [[UITextField alloc] initWithFrame:CGRectMake(i*50+35, 150, 50, 50)];

        t.tag = i + 1;

        t.font = [UIFont boldSystemFontOfSize:50];

        t.textAlignment = NSTextAlignmentCenter;

        t.textColor = [UIColor lightGrayColor];

        

        t.backgroundColor = [UIColor blackColor];

        t.layer.borderWidth = 5;

        t.layer.borderColor = [UIColor darkGrayColor].CGColor;

        

        if (i > 0 && i< 4) {

            t.text = digit[i – 1];

        }

        [self.view addSubview:t];

    }

}

– (void)updateAnchor

{

    UIView *t = [self.view viewWithTag:self.index];

    

    if (!self.anchor) {

        UILabel *anchor = [[UILabel alloc] init];

        anchor.font = [UIFont boldSystemFontOfSize:50];

        anchor.text = @”▲”;

        [anchor sizeToFit];

        self.anchor = anchor;

        [self.view addSubview:anchor];

    }

    

    dispatch_async(dispatch_get_main_queue(), ^{

        self.anchor.center = CGPointMake(t.center.x, t.center.y + 50);

    });

}

– (void)createStartButton

{

    UIButton *start = [UIButton buttonWithType:UIButtonTypeSystem];

    start.titleLabel.font = [UIFont systemFontOfSize:30];

    [start setTitle:@”step” forState:UIControlStateNormal];

    [start sizeToFit];

    start.center = CGPointMake(160, 350);

    [self.view addSubview:start];

    

    [start addTarget:self action:@selector(step) forControlEvents:UIControlEventTouchUpInside];

}

– (void)step

{

    SEL f = self.condition();

    if ([self respondsToSelector:f]) {

        [self performSelector:f withObject:nil afterDelay:0.0];

    }

}

#pragma mark – A

– (SEL)conditionA

{

    UITextField *t = (UITextField *)[self.view viewWithTag:self.index];

    if (t.text.length == 0) {

        return @selector(functionA0);

    }

    return @selector(functionA1);

}

– (void)functionA0

{

    self.index–;

    [self updateAnchor];

    

    id __weak weakSelf = self;

    self.condition = ^SEL{ return [weakSelf conditionB]; };

}

– (void)functionA1

{

    self.index++;

    [self updateAnchor];

}

#pragma mark – B

– (SEL)conditionB

{

    UITextField *t = (UITextField *)[self.view viewWithTag:self.index];

    

    if (t.text.length == 0) {

        return @selector(functionB0);

        

    } else if ([t.text isEqual:@”9″]) {

        return @selector(functionB1);

    }

    

    return @selector(functionB2);

}

– (void)functionB0

{

    UITextField *t = (UITextField *)[self.view viewWithTag:self.index];

    t.text = @”1″;

    // finish

}

– (void)functionB1

{

    UITextField *t = (UITextField *)[self.view viewWithTag:self.index];

    t.text = @”0″;

    

    self.index–;

    [self updateAnchor];

}

– (void)functionB2

{

    UITextField *t = (UITextField *)[self.view viewWithTag:self.index];

    t.text = [NSString stringWithFormat:@”%d”, [t.text intValue] + 1];

    

    self.index–;

    [self updateAnchor];

    

    id __weak weakSelf = self;

    self.condition = ^SEL{ return [weakSelf conditionC]; };

}

#pragma mark – C

– (SEL)conditionC

{

    UITextField *t = (UITextField *)[self.view viewWithTag:self.index];

    if (t.text.length == 0) {

        return @selector(functionC0);

    }

    return @selector(functionC1);

}

– (void)functionC0

{

    self.index++;

    [self updateAnchor];

    

    id __weak weakSelf = self;

    self.condition = ^SEL{ return [weakSelf conditionA]; };

}

– (void)functionC1

{

    self.index–;

    [self updateAnchor];

}

@end