Nバック課題というワーキングメモリーを鍛えるゲームを自作してみます。今回作るのは3バックです。アルファベットを順番に表示して、3つ前の文字が同じだったら表示されたアルファベットをタップするという風にしてみます。作って自分で試してみたのですが、3バック課題は難関でした。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
ポイント
ソースの nback = 3; としているところを nback = 2 にすると2バック課題に変えられるので、私のように3バックは無理。と思った人は変更してみると意外と行けるかもしれません。int型のcurrentPositionというのを基準の数値として、タイマーで呼び出すたびにコレをインクリメントすることで、文字を順番に出しています。表示する文字は、wordsというNSMutableArrayのなかに、アルファベットをランダムで突っ込んでいます。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
NSMutableArray *words;
int nback;
int currentPosition;
UILabel *startbtn;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
nback = 3;
self.view.backgroundColor = [UIColor whiteColor];
[self createWords];
[self createPanels];
[self createStartButton];
// title
UILabel *title = [[UILabel alloc] init];
title.text = @”3-back task”;
title.textColor = [UIColor lightGrayColor];
title.font = [UIFont fontWithName:@”Marker Felt” size:40];
[title sizeToFit];
title.center = CGPointMake(160, 360);
[self.view addSubview:title];
}
– (void)createWords
{
currentPosition = 0;
words = [[NSMutableArray alloc] init];
NSArray *strings = [@”A B C D E F G H I J K L M N O P Q R S T U V W X Y Z” componentsSeparatedByString:@” “];
for (int i=0; i<20; i++) {
// random word
NSString *word = [strings objectAtIndex:arc4random() % [strings count]];
[words addObject: word];
}
// add manualy nth back position
for (int i=0; i<2; i++) {
int position = arc4random() % 8 + (8 * i + 4);
NSString *sameWord = [words objectAtIndex:position];
[words insertObject:sameWord atIndex:position – (nback – 1)];
}
}
– (void)createPanels
{
for (int i = 0 ; i<27; i++) {
float size = 320.0 / 9.0;
float x = (i % 9) * size;
float y = (i / 9) * size;
UIView *panel = [[UIView alloc] initWithFrame:CGRectMake(x, y, size, size)];
panel.backgroundColor = [UIColor colorWithWhite:i%2 alpha:1];
panel.tag = i + 100;
[self.view addSubview:panel];
}
}
– (void)createStartButton
{
startbtn = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 50)];
startbtn.text = @”start”;
startbtn.center = CGPointMake(160, 300);
startbtn.textAlignment = 1; // center
startbtn.font = [UIFont fontWithName:@”Marker Felt” size:40];
startbtn.layer.borderColor = [UIColor blackColor].CGColor;
startbtn.layer.borderWidth = 5;
[self.view addSubview:startbtn];
startbtn.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(start)];
[startbtn addGestureRecognizer:tap];
}
– (void)start
{
// remove panels
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[UILabel class]] && v.center.y < 120) {
[v removeFromSuperview];
}
}
[self createWords];
[UIView animateWithDuration:0.2 animations:^{
startbtn.transform = CGAffineTransformMakeTranslation(-200, 0);
} completion:^(BOOL finished) {
[startbtn removeFromSuperview];
}];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showWord:) userInfo:nil repeats:YES];
}
– (void)showWord:(NSTimer*)sender
{
if (currentPosition >= [words count]) {
[sender invalidate];
[self createStartButton];
return;
}
UILabel *wordPanel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
wordPanel.center = CGPointMake(160, 300);
wordPanel.transform = CGAffineTransformMakeTranslation(200, 0);
wordPanel.text = [words objectAtIndex:currentPosition];
wordPanel.textAlignment = 1; // center
wordPanel.font = [UIFont fontWithName:@”Marker Felt” size:70];
[self.view addSubview:wordPanel];
// shadow
wordPanel.layer.masksToBounds = NO;
wordPanel.layer.shadowOffset = CGSizeMake(15, 20);
wordPanel.layer.shadowRadius = 5;
wordPanel.layer.shadowOpacity = 0.5;
// tag for relation with panels
wordPanel.tag = currentPosition;
[UIView animateWithDuration:0.3 animations:^{
wordPanel.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[self performSelector:@selector(displayPanels:) withObject:wordPanel afterDelay:1.0];
}];
// add gesture
wordPanel.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[wordPanel addGestureRecognizer:tap];
currentPosition++;
}
– (void)displayPanels:(UIView*)v
{
int panelIndex = v.tag + 100;
UIView *target = [self.view viewWithTag:panelIndex];
[UIView animateWithDuration:0.3 animations:^{
v.transform = CGAffineTransformMakeScale(0.3, 0.3);
v.center = target.center;
}];
// check missing
if ([v.backgroundColor isEqual:[UIColor whiteColor]] && [self checkNBackEqual:v.tag]) {
v.backgroundColor = [UIColor redColor];
}
}
– (void)tap:(UITapGestureRecognizer*)gr
{
if ([self checkNBackEqual:gr.view.tag]) {
gr.view.backgroundColor = [UIColor blueColor];
} else {
gr.view.backgroundColor = [UIColor redColor];
}
}
– (BOOL)checkNBackEqual:(int)i
{
if (i – nback >= 0) {
NSString *nbackString = [words objectAtIndex:i – nback];
NSString *current = [words objectAtIndex:i];
return [nbackString isEqual:current];
}
return NO;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end