tengusa64のブログ

tengusa64のブログ

ブログの説明を入力します。

Amebaでブログを始めよう!
$tengusa64のブログ-トップ 
「2・4」番目の ほぼ究極の Jigsaw Game はどうでしたか。ブログの更新をサボっていましたが、間に合わせに、ほんの10分間で作れる簡単なアプリを作ってみました。(番外編)
むかし、青年だった年配の人には懐かしいゲームです。

$tengusa64のブログ-arg15a



やりかたは、 black cell に接している cell だけをうごかして、バラバラになった各数字を1~15まで並べ直すというゲームです。
「15並べとかいっていましたかね?」

$tengusa64のブログ-arg15b



コードをのせときます。


#import "ViewController.h"
#import &ltQuartzCore/QuartzCore.h>

@interface ViewController ()
{
NSMutableArray *posAry;
NSMutableArray *stateAry;
NSInteger tate,yoko,anum;
UIView *myView;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

myView=[[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 300)];

[self.view addSubview:myView];

UIButton *rstbtn = [UIButton buttonWithType:UIButtonTypeCustom];
rstbtn.layer.cornerRadius = 10.0f;
rstbtn.frame = CGRectMake(230,30,70,30);
[rstbtn addTarget:self action:@selector(reset) forControlEvents:UIControlEventTouchDown];
[rstbtn setTitle:@"reset" forState:UIControlStateNormal];
[rstbtn setTitleColor:[ UIColor blueColor] forState:UIControlStateNormal];
rstbtn.backgroundColor = [UIColor yellowColor];
[self.view addSubview:rstbtn];

self.view.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
[myView addGestureRecognizer:tap];

[self createTile];
[self reset];
}
-(void)createTile
{

posAry=[NSMutableArray array];
stateAry=[NSMutableArray array];

for (int v = 0; v < 4; v++) {
for (int h = 0; h < 4; h++) {

CGPoint pos = CGPointMake((37+75*h),
(37+75*v));
NSValue *val = [NSValue valueWithCGPoint:pos];
[posAry addObject:val];
[stateAry addObject:[NSString stringWithFormat:@"%d",h+4*v]];
}
}
anum=0;
for (int i=0; i<16; i++) {
CALayer *atile = [CALayer layer];// initWithFrame:CGRectMake(0, 0, 75, 75)];
atile.frame=CGRectMake(0, 0, 75, 75);
UILabel *tile=[[UILabel alloc]init];
tile.frame=CGRectMake(0, 0, 75, 75);
tile.backgroundColor = [UIColor cyanColor];
tile.layer.borderWidth = 2.0;
NSValue *value = [posAry objectAtIndex:i];
CGPoint cp = [value CGPointValue];
tile.text=[NSString stringWithFormat:@"%d",i];
tile.textAlignment = 1;
tile.font = [UIFont boldSystemFontOfSize:24];
tile.center = cp;
tile.tag=i+1;

if (i==0) {
tile.backgroundColor = [UIColor blackColor];
tile.text=@"x";

}
atile.contents=tile;
atile.position=cp;
[myView addSubview:tile];
}

}
- (void)tapView:(UITapGestureRecognizer*)gesture {


CGPoint pos = [gesture locationInView:myView];
NSInteger tilex=(pos.x)/75;
NSInteger tiley=(pos.y)/75;
NSInteger index=tiley*4+tilex;

NSInteger atile=[[stateAry objectAtIndex:index]intValue];

tate=anum%4;
yoko=anum/4;
NSValue *value = [posAry objectAtIndex:anum];
CGPoint nilcp = [value CGPointValue];

NSValue *exvalue = [posAry objectAtIndex:index];
CGPoint excp = [exvalue CGPointValue];


if (tate==index%4 || yoko==index/4) {
if (abs(index-anum)==1|| abs(index-anum)==4) {
UILabel *alabel=(UILabel* )[self.view viewWithTag: atile+1 ];
alabel.center=nilcp;

UILabel *nillabel=(UILabel* )[self.view viewWithTag: 1 ];
nillabel.center=excp;

[stateAry exchangeObjectAtIndex:index withObjectAtIndex:anum];
anum=index;

}

}

}
- (void)reset{

//シャッフル
int i;
stateAry=[NSMutableArray array];
for (i=0; i<16; i++) {
[stateAry addObject:[NSNumber numberWithInt:i]];
}
for (i=0; i<16*3; i++) {
int index=random() % 16;
id obj=[stateAry objectAtIndex:index];
[stateAry removeObjectAtIndex:index];
[stateAry addObject:obj];

}
for (i=0; i<16; i++) {
NSInteger tile=[[stateAry objectAtIndex:i]intValue];


NSValue *value = [posAry objectAtIndex:i];
CGPoint cp = [value CGPointValue];

UILabel *alabel=(UILabel* )[self.view viewWithTag: tile+1 ];
if (tile==0) {
anum=i;
}
alabel.center=cp;
}


}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end


暇なときの時間つぶしに。