iphone开发3 一个扫雷的游戏

这周做了个扫雷的游戏下面贴上代码:

bombViewController.m:

 

- (void)viewDidLoad {

    [super viewDidLoad];

//初始化

extern lei mine[MYMAX][MYMAX];

int i1,j1;

int i, j, Row, Col;

for(i1=0;i1<MYMAX-1;i1++)

for(j1=0;j1<MYMAX-1;j1++){

mine[i1][j1].con='0';

mine[i1][j1].visual=0;

};

for(i1=0;i1<MYMAX;i1++){

mine[i1][MYMAX-1].con='a';

mine[i1][MYMAX-1].visual=0;

mine[MYMAX-1][i1].con='a';

mine[MYMAX-1][i1].visual=0;

}

srand(time(0));

for (i = 0; i < 10; i++){

Row = rand()%9; 

Col = rand()%9;

mine[Row][Col].con='*';

}

for ( i = 0; i < 9; i++){

for ( j = 0; j < 9; j++){

if (mine[i][j].con == '0')

if (i-1>=0 && j-1>=0 && mine[i-1][j-1].con=='*')   mine[i][j].con++;

if (j-1>=0 && mine[i][j-1].con=='*')     mine[i][j].con++;

if (i+1<=9 && j-1>=0 && mine[i+1][j-1].con=='*')   mine[i][j].con++;

if (i-1>=0 && mine[i-1][j].con=='*')     mine[i][j].con++;

if (i+1<=9 && mine[i+1][j].con=='*')     mine[i][j].con++;

if (i-1>=0 && j+1<=9 && mine[i-1][j+1].con=='*')   mine[i][j].con++;

if (j+1<=9 && mine[i][j+1].con=='*')     mine[i][j].con++;

if (i+1<=9 && j+1<=9 && mine[i+1][j+1].con=='*')   mine[i][j].con++;

}

}

}

win=5;

alive=1;

}

 

 

 

 

map.h:

 

#import <UIKit/UIKit.h>

#import "bombViewController.h"

 

@interface map : UIView <UIAlertViewDelegate>{

lei **mymine;

UIImage *image1;

UIImage *image2;

UIImage *image3;

UIImage *image4;

UIImage *image5;

UIImage *image6;

UIImage *image7;

UIImage *image8;

UIImage *imageflag;

UIImage *imageboom;

UIImage *imageenpty;

CGPoint touchstart;

UIView *containView;

UIImageView *firstp;

UIImageView *secondp;

 

}

-(CGPoint)xtopoint:(int)x 

  ytopoint:(int)y;

-(void)putoutx:(int) x

  putouty:(int) y;

-(int) ifout:(int) x;

-(IBAction)btnPressed;

-(void)showall;

-(void)init;

@property (retain,nonatomic)UIImage *imageenpty;

@property (retain,nonatomic)UIImage *image2;

@property (retain,nonatomic)UIImage *image3;

@property (retain,nonatomic)UIImage *image4;

@property (retain,nonatomic)UIImage *image5;

@property (retain,nonatomic)UIImage *image6;

@property (retain,nonatomic)UIImage *image7;

@property (retain,nonatomic)UIImage *image8;

@property (retain,nonatomic)UIImage *imageflag;

@property (retain,nonatomic)UIImage *imageboom;

@property (retain,nonatomic)UIImage *image1;

@property (retain,nonatomic)UIView *containView;

@property (retain,nonatomic)UIImageView *firstp;

@property (retain,nonatomic)UIImageView *secondp;

@end

map.m:

#import "map.h"

#import "bombViewController.h"

#import <QuartzCore/QuartzCore.h>

@implementation map

@synthesize imageenpty;

@synthesize image1;

@synthesize image2;

@synthesize image3;

@synthesize image4;

@synthesize image5;

@synthesize image6;

@synthesize image7;

@synthesize image8;

@synthesize imageflag;

@synthesize imageboom;

@synthesize containView;

@synthesize firstp;

@synthesize secondp;

-(void)showall{

extern lei mine[MYMAX][MYMAX];

int i1,j1;

for(i1=0;i1<MYMAX-1;i1++)

for(j1=0;j1<MYMAX-1;j1++){

mine[i1][j1].visual=1;

};

alive=-1;

[self drawRect:CGRectMake(0, 0, 360, 480)];

}

-(void)init{

extern lei mine[MYMAX][MYMAX];

int i1,j1;

int i, j, Row, Col;

for(i1=0;i1<MYMAX-1;i1++)

for(j1=0;j1<MYMAX-1;j1++){

mine[i1][j1].con='0';

mine[i1][j1].visual=0;

};

for(i1=0;i1<MYMAX;i1++){

mine[i1][MYMAX-1].con='a';

mine[i1][MYMAX-1].visual=0;

mine[MYMAX-1][i1].con='a';

mine[MYMAX-1][i1].visual=0;

}

srand(time(0));

for (i = 0; i < 10; i++){

Row = rand()%9; 

Col = rand()%9;

mine[Row][Col].con='*';

}

for ( i = 0; i < 9; i++){

for ( j = 0; j < 9; j++){

if (mine[i][j].con == '0')

if (i-1>=0 && j-1>=0 && mine[i-1][j-1].con=='*')   mine[i][j].con++;

if (j-1>=0 && mine[i][j-1].con=='*')     mine[i][j].con++;

if (i+1<=9 && j-1>=0 && mine[i+1][j-1].con=='*')   mine[i][j].con++;

if (i-1>=0 && mine[i-1][j].con=='*')     mine[i][j].con++;

if (i+1<=9 && mine[i+1][j].con=='*')     mine[i][j].con++;

if (i-1>=0 && j+1<=9 && mine[i-1][j+1].con=='*')   mine[i][j].con++;

if (j+1<=9 && mine[i][j+1].con=='*')     mine[i][j].con++;

if (i+1<=9 && j+1<=9 && mine[i+1][j+1].con=='*')   mine[i][j].con++;

}

}

}

win=5;

alive=1;

}

-(void)btnPressed{

UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"sorry" message:@"youlose" delegate:self cancelButtonTitle:@"RESTART?" otherButtonTitles:nil];

[self showall];

[alert show];

}

- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

 

    }

    return self;

}

-(int)ifout:(int)x{

if(x<0||x>MYMAX-1)

return MYMAX;

else return x;

}

-(CGPoint)xtopoint:(int)x 

  ytopoint:(int)y{

CGPoint p;

p.x=30*x;

p.y=30*y;

return p;

}

-(void)putoutx:(int)x putouty:(int)y{

if(mine[x][y].visual==1)

return;

if(x==10||y==10)

return;

mine[x][y].visual=1;

if(mine[x][y].con=='0'){

    [self putoutx:[self ifout:x+1] putouty:[self ifout:y]];

[self putoutx:[self ifout:x]   putouty:[self ifout:y+1]];

[self putoutx:[self ifout:x]   putouty:[self ifout:y-1]];

[self putoutx:[self ifout:x-1] putouty:[self ifout:y]];

[self putoutx:[self ifout:x-1] putouty:[self ifout:y+1]];

[self putoutx:[self ifout:x-1] putouty:[self ifout:y-1]];

[self putoutx:[self ifout:x+1] putouty:[self ifout:y-1]];

[self putoutx:[self ifout:x+1] putouty:[self ifout:y+1]];

}

return;

}

- (void)drawRect:(CGRect)rect {

    // Drawing code

int j,i;

 

extern lei mine[MYMAX][MYMAX];

// Initialization code

image1=[UIImage imageNamed:@"1.png"];

image2=[UIImage imageNamed:@"2.png"];

image3=[UIImage imageNamed:@"3.png"];

image4=[UIImage imageNamed:@"4.png"];

image5=[UIImage imageNamed:@"5.png"];

image6=[UIImage imageNamed:@"6.png"];

image7=[UIImage imageNamed:@"7.png"];

image8=[UIImage imageNamed:@"8.png"];

imageboom=[UIImage imageNamed:@"boom.png"];

imageflag=[UIImage imageNamed:@"frag.png"];

imageenpty=[UIImage imageNamed:@"empty.png"];

for(i=0;i<MYMAX-1;i++)

for(j=0;j<MYMAX-1;j++){

self.containView=[[UIView alloc]initWithFrame:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

[self addSubview:self.containView];

firstp=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, XSTEP, YSTEP)];

secondp=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, XSTEP, YSTEP)];

//

//[self.containView addSubview:secondp];

if(mine[i][j].visual==0 )

[self.imageenpty drawInRect:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

else{

switch(mine[i][j].con){

case '*':

firstp.image=imageenpty;

[self.containView addSubview:self.firstp];

secondp.image=imageboom;

[UIView beginAnimations:nil context:nil];

//设置动画时间

[UIView setAnimationDuration:0.75f];

[UIView setAnimationTransition:([self.firstp superview]? UIViewAnimationTransitionFlipFromRight:UIViewAnimationTransitionFlipFromLeft) forView:self.containView cache:YES];

if([firstp superview]){

[firstp removeFromSuperview];

[containView addSubview:secondp];

}

else {

[secondp removeFromSuperview];

[containView addSubview:firstp];

}

[UIImageView commitAnimations];

if(alive!=-1)

alive=0;

break;

case '1':[self.image1 drawInRect:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

break;

case '2':[self.image2 drawInRect:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

break;

case '3':[self.image3 drawInRect:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

break;

case '4':[self.image4 drawInRect:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

break;

case '5':[self.image5 drawInRect:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

break;

case '6':[self.image6 drawInRect:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

break;

case '7':[self.image7 drawInRect:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

break;

case '8':[self.image8 drawInRect:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

break;

case 'f':[self.imageflag drawInRect:CGRectMake(i*XSTEP, j*YSTEP, XSTEP, YSTEP)];

break;

}

}

}

if (alive==0) {

[self showall];

[self setNeedsDisplay];

[self drawRect:CGRectMake(0, 0, 360, 480)];

[self btnPressed];

//[self drawRect:CGRectMake(0, 0, 360, 480)];

}

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touch = [touches anyObject];

CGPoint location = [touch locationInView:self];

touchstart.x=location.x;

touchstart.y=location.y;

[self setNeedsDisplay];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

extern lei mine[MYMAX][MYMAX];

CGPoint end;

int i,j;

UITouch *touch = [touches anyObject];

CGPoint location = [touch locationInView:self];

end.x=location.x;

end.y=location.y;

i=touchstart.x/XSTEP;

j=touchstart.y/YSTEP;

//UP

if (end.y-touchstart.y<-10.0f&&end.x-touchstart.x<50.0f&&end.x-touchstart.x>-50.0f) {

if(mine[i][j].con=='*')

win--;

mine[i][j].con='f';

mine[i][j].visual=1;

}

else {

[self putoutx:(i)

  putouty:(j)];

}

 

[self setNeedsDisplay];

}

 

- (void)dealloc {

    [super dealloc];

}

 

 

@end

 

 

 

 

运行结果:

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值