ios2048

自学一段时间的ios,尝试着做了个2048,因为之前做过Android的2048,所以逻辑等都知道.做起来还算顺利,

之前出了一点小bug,都已经改过来了.

第一次自己尝试写写东西,没有开发经验,比较乱,不过已经尽力整理过了.

源码已经上传. http://download.csdn.net/download/ll_xyls/9249069点击打开链接

所有的代码都写在了ViewController文件

#import "ViewController.h"

/*
 * 用来存放图片的数字是多少,方便以后判断
 */
int a[4][4] = {0};

@interface ViewController () <UIAlertViewDelegate>

@property (nonatomic, strong) UIView *gameView;
@property (nonatomic, strong) NSMutableArray *cards;

@property (nonatomic, strong) UIView *scoreView;
@property (nonatomic, strong) UILabel *scoreLabel;
@property (nonatomic, strong) UILabel *bestScoreLabel;

// 用来判断游戏视图中图片是否移动了
@property (nonatomic, assign) BOOL moving;

// 手指接触屏幕时的左边
@property (nonatomic, assign) CGPoint startP;
// 手指离开屏幕时的左边
@property (nonatomic, assign) CGPoint endP;

@property (nonatomic, assign) int score;
@property (nonatomic, assign) int bestScore;

@end

@implementation ViewController

//
/**
 * 懒加载游戏视图
 */
- (UIView *)gameView
{
    if (_gameView == nil) {
        
        CGFloat x = 20;
        CGFloat w = self.view.bounds.size.width - x * 2;
        CGFloat h = w;
        CGFloat y = (self.view.bounds.size.height - w) / 2;
        _gameView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        _gameView.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:1 blue:1 alpha:1];
//        _gameView.backgroundColor = [UIColor blueColor];
        [self.view addSubview:_gameView];
    }
    return _gameView;
}

/**
 * 创建存放ImageView的数组
 * 
 * 游戏开始时创建出所有的ImageView,并设置hidden = YES
 */
- (NSMutableArray *)cards
{
    if (_cards == nil) {
        _cards = [NSMutableArray array];
        CGFloat imgX = 0;
        CGFloat imgY = 0;
        CGFloat imgW = self.gameView.bounds.size.width / 4;
        CGFloat imgH = imgW;
        for (int i = 0; i < 4; i++) {
            NSMutableArray *arrayM = [NSMutableArray array];
            for (int j = 0; j < 4; j++) {
                imgX =imgW * i;
                imgY =imgH * j;
                CGRect rect = CGRectMake(imgX, imgY, imgW, imgH);
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
                //                imageView.image = [UIImage imageNamed:@"tp2"];
                imageView.hidden = YES;
                [self.gameView addSubview:imageView];
                [arrayM addObject:imageView];
            }
            [_cards addObject:arrayM];
        }
    }
    return _cards;
}

/**
 * 存放分数label的View
 */
- (UIView *)scoreView
{
    if (_scoreView == nil) {
        //self.gameView.bounds.origin.y
        CGFloat x = self.view.bounds.size.width * 0.5;
        CGFloat y = 20;
        CGFloat w = x - 20;
        CGFloat h = 50;
        
        _scoreView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        _scoreView.backgroundColor = [UIColor grayColor];
        [self.view addSubview:_scoreView];
    }
    return _scoreView;
}

/**
 * 最高分
 */
- (UILabel *)bestScoreLabel
{
    if (_bestScoreLabel == nil) {
        CGFloat x = 5;
        CGFloat y = 5;
        CGFloat w = self.view.bounds.size.width * 0.5 - 30;
        CGFloat h = 20;
        _bestScoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        _bestScoreLabel.font = [UIFont systemFontOfSize:14];
        _bestScoreLabel.textColor = [UIColor whiteColor];
        [self.scoreView addSubview:_bestScoreLabel];
        _bestScoreLabel.text = @"Bsco:100000";
    }
    return _bestScoreLabel;
}

/**
 * 分数
 */
- (UILabel *)scoreLabel
{
    if (_scoreLabel == nil) {
        CGFloat x = 5;
        CGFloat y = 25;
        CGFloat w = self.view.bounds.size.width * 0.5 - 30;
        CGFloat h = 20;
        _scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        _scoreLabel.font = [UIFont systemFontOfSize:14];
        _scoreLabel.textColor = [UIColor whiteColor];
        [self.scoreView addSubview:_scoreLabel];
        _scoreLabel.text = @" sco:100000";
    }
    return _scoreLabel;
}

// 设置图片
- (void)setImage:(UIImageView *)imageView withNo:(int)No
{
    NSString *imageName = [NSString stringWithFormat:@"tp%d", No];
    imageView.image = [UIImage imageNamed:imageName];
}

// 随机添加图片
- (void)addImage
{
    int x = arc4random_uniform(4);
    int y = arc4random_uniform(4);
    UIImageView *imageView = self.cards[x][y];
    if (imageView.hidden == NO) {
        [self addImage];
    }else{
        int num = arc4random_uniform(100);
        if (num < 90) {
            num = 2;
            a[x][y] = 2;
        }else
        {
            a[x][y] = 4;
            num = 4;
        }
        
        [self setImage:imageView withNo:num];
        
        imageView.hidden = NO;
    }
}

/*
 * 交换图片位置
 *
 * 每张图片分别对应数字数组,和imageView的数组,所以一并交换
 * 此方法只有在图片移动时才会调用,图片上的数字相同合并时,不会调用
 */
- (void)swipWithX1:(int)x1 Y1:(int)y1 andX2:(int)x2 Y2:(int)y2
{
    UIImageView *imgV1 = self.cards[x1][y1];
    UIImageView *imgV2 = self.cards[x2][y2];
    
    // 交换视图位置
    CGRect frame = imgV1.frame;
    imgV1.frame = imgV2.frame;
    imgV2.frame = frame;
    
    // 交换图片数组中的位置
    self.cards[x1][y1] = imgV2;
    self.cards[x2][y2] = imgV1;
    
    // 交换数字数组中的元素
    a[x1][y1] = a[x2][y2];
    a[x2][y2] = 0;
    
}

// 向上划
- (void)swipTop
{
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            for (int jj = j+1; jj < 4; jj++) {
                UIImageView *imgV1 = self.cards[i][j];
                UIImageView *imgV2 = self.cards[i][jj];
                if (imgV2.hidden == NO) {
                    if (imgV1.hidden == YES) { // 图片上边是空白时:
                        [self swipWithX1:i Y1:j andX2:i Y2:jj];
                        //a[i][jj] = 0;
                        self.moving = YES;
                        j--;
                    }else if(imgV1.hidden == NO && a[i][j] == a[i][jj]){// 两张图片相同时
                        a[i][jj] = 0;
                        a[i][j] = a[i][j] * 2;
                        [self changeScore:a[i][j]];
                        [self setImage:imgV1 withNo:a[i][j]];
                        imgV2.hidden = YES;
                        self.moving = YES;
                    }else{
                        
                    }
                    break;
                }
            }
        }
    }
    // 每一移动一次判断是否结束游戏
    [self isGameOver];
}

// 向下划
- (void)swipBottom
{
    for (int i = 0; i < 4; i++) {
        for (int j = 3; j >= 0; j--) {
            for (int jj = j-1; jj >= 0; jj--) {
                UIImageView *imgV1 = self.cards[i][j];
                UIImageView *imgV2 = self.cards[i][jj];
                if (imgV2.hidden == NO) {
                    if (imgV1.hidden == YES) {
                        [self swipWithX1:i Y1:j andX2:i Y2:jj];
                        //a[i][jj] = 0;
                        self.moving = YES;
                        j++;
                    }else if(imgV1.hidden == NO && a[i][j] == a[i][jj]){
                        a[i][jj] = 0;
                        a[i][j] = a[i][j] * 2;
                        [self changeScore:a[i][j]];
                        [self setImage:imgV1 withNo:a[i][j]];
                        imgV2.hidden = YES;
                        self.moving = YES;
                    }else{
                        
                    }
                    break;
                }
                
            }
        }
    }
    [self isGameOver];
}

// 向左划
- (void)swipLeft
{
    for (int j = 0; j < 4; j ++) {
        for (int i = 0; i < 4; i++) {
            for (int ii = i+1; ii < 4; ii++) {
                UIImageView *imgV1 = self.cards[i][j];
                UIImageView *imgV2 = self.cards[ii][j];
                if (imgV2.hidden == NO) {
                    if (imgV1.hidden == YES) {
                        [self swipWithX1:i Y1:j andX2:ii Y2:j];
                        //a[ii][j] = 0;
                        self.moving = YES;
                        i--;
                    }else if(imgV1.hidden == NO && a[i][j] == a[ii][j]){
                        a[ii][j] = 0;
                        a[i][j] = a[i][j] * 2;
                        [self changeScore:a[i][j]];
                        [self setImage:imgV1 withNo:a[i][j]];
                        imgV2.hidden = YES;
                        self.moving = YES;
                    }else{
                        
                    }
                    
                    break;
                }
                
            }
        }
    }
    [self isGameOver];
}

// 向右划
- (void)swipRight
{
    for (int j = 3; j >= 0; j --) {
        for (int i = 3; i >= 0; i--) {
            for (int ii = i-1; ii >= 0; ii--) {
                UIImageView *imgV1 = self.cards[i][j];
                UIImageView *imgV2 = self.cards[ii][j];
                if (imgV2.hidden == NO) {
                    if (imgV1.hidden == YES) {
                        [self swipWithX1:i Y1:j andX2:ii Y2:j];
                        //a[ii][j] = 0;
                        self.moving = YES;
                        i++;
                    }else if(imgV1.hidden == NO && a[i][j] == a[ii][j]){
                        a[ii][j] = 0;
                        a[i][j] = a[i][j] * 2;
                        [self changeScore:a[i][j]];
                        [self setImage:imgV1 withNo:a[i][j]];
                        imgV2.hidden = YES;
                        self.moving = YES;
                    }else{
                        
                    }
                    
                    break;
                }
                
            }
        }
    }
    [self isGameOver];
}


// 判断是否结束游戏
- (void)isGameOver
{
    BOOL die = YES;
    for (int i = 0; i < 4 && die; i++) {
        for (int j = 0; j < 4 ; j++) {
//            NSLog(@"i = %d, j = %d", i, j);
            if (i == 0 && j == 0) {
                if (a[i][j] == 0 || a[i][j] == a[i+1][j] || a[i][j] == a[i][j+1]) {
                    die = NO;
                    break;
                }
            }else if (i == 0 && j == 3) {
                if (a[i][j] == 0 || a[i][j] == a[i+1][j] || a[i][j] == a[i][j-1]) {
                    die = NO;
                    break;
                }
            } else if (i == 3 && j == 0) {
                if (a[i][j] == 0 || a[i][j] == a[i-1][j] || a[i][j] == a[i][j+1]) {
                    die = NO;
                    break;
                }
            } else if (i == 3 && j == 3) {
                if (a[i][j] == 0 || a[i][j] == a[i-1][j] || a[i][j] == a[i][j-1]) {
                    die = NO;
                    break;
                }
            } else if (i > 0 && i < 3 && j > 0 && j < 3) {
                if (a[i][j] == 0 || a[i][j] == a[i-1][j] || a[i][j] == a[i+1][j] || a[i][j] == a[i][j-1] || a[i][j] == a[i][j+1]) {
                    die = NO;
                    break;
                }
            } else if (i == 0) {
                if (a[i][j] == 0 || a[i][j] == a[i][j+1] || a[i][j] == a[i][j-1] || a[i][j] == a[i+1][j]) {
                    die = NO;
                    break;
                }
            } else if (j == 0) {
                if (a[i][j] == 0 || a[i][j] == a[i+1][j] || a[i][j] == a[i-1][j] || a[i][j] == a[i][j+1]) {
                    die = NO;
                    break;
                }
            } else if (i == 3) {
                if (a[i][j] == 0 || a[i][j] == a[i][j+1] || a[i][j] == a[i][j-1] || a[i][j] == a[i-1][j]) {
                    die = NO;
                    break;
                }
            } else if (j == 3) {
                if (a[i][j] == 0 || a[i][j] == a[i+1][j] || a[i][j] == a[i-1][j] || a[i][j] == a[i][j-1]) {
                    die = NO;
                    break;
                }
            }
            
        }
    }
    
    if (die) {
        NSLog(@"die = %d", die);
        // 结束游戏时弹出提示框
        [[[UIAlertView alloc] initWithTitle:@"重新开始" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
    }
}

/**
 * 更改分数
 */
- (void)changeScore:(int)score
{
    self.score += score;
    self.scoreLabel.text = [NSString stringWithFormat:@" sco:%d", self.score];
    if (self.score > self.bestScore) {
        self.bestScore = self.score;
        self.bestScoreLabel.text = [NSString stringWithFormat:@"Bsco:%d", self.bestScore];
    }
}



- (void)viewDidLoad {
    self.bestScoreLabel.text = @"Bsc:0";
    self.scoreLabel.text = @" sc:0";
    [self gameView];
//    [self cards];
    [self addImage];
    [self addImage];
    
}

// 记录下接触屏幕时的位置
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches) {
        self.startP = [touch locationInView:self.view];
    }
}

/**
 * 该方法可得到离开屏幕时的位置,并且根据开始时的位置判断划的方向
 */
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches) {
        self.endP = [touch locationInView:self.view];
    }
    
    CGFloat moveX = self.endP.x - self.startP.x;
    CGFloat moveY = self.endP.y - self.startP.y;
    
    if (fabs(moveX) > 5 || fabs(moveY) > 5) { // 5是误差
        if (fabs(moveX) > fabs(moveY)) {
            if (moveX > 0) {
                [self swipRight];
                if (self.moving) {
                    self.moving = NO;
                    [self addImage];
                }
            }else{
                [self swipLeft];
                if (self.moving) {
                    self.moving = NO;
                    [self addImage];
                }
            }
        }else{
            if (moveY > 0) {
                [self swipBottom];
                if (self.moving) {
                    self.moving = NO;
                    [self addImage];
                }
            }else{
//                NSLog(@"上");
                [self swipTop];
                if (self.moving) {
                    self.moving = NO;
                    [self addImage];
                }
            }
        }
    }
}

#pragma mark - alertView代理方法
/**
 * 游戏结束时,提示是否重新开始
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%d", (int)buttonIndex);
    if ((int)buttonIndex == 1) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                UIImageView *imgV = self.cards[i][j];
                imgV.hidden = YES;
                a[i][j] = 0;
            }
        }
        self.score = 0;
        self.scoreLabel.text = @" sco:0";
        [self addImage];
        [self addImage];
    }
}



@end

运行效果如图:


版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-11-06 23:14  ll_xyls  阅读(228)  评论(0编辑  收藏  举报