IOS游戏开发之五子棋OC版

 更新时间:2016年08月09日 17:06:39   投稿:daisy  
五子棋是大家比较熟悉的一款小游戏,相信很多人用多种语言写过五子棋小游戏,本文试着用OC实现了一下,在这里给大家分享一下。有需要的可以参考学习。

先上效果图

- 功能展示

- 初高级棋盘切换效果

实现思路及主要代码详解

1.绘制棋盘

利用Quartz2D绘制棋盘.代码如下

- (void)drawBackground:(CGSize)size{
     
    self.gridWidth = (size.width - 2 * kBoardSpace) / self.gridCount;
     
    //1.开启图像上下文
    UIGraphicsBeginImageContext(size);
    //2.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
     
    CGContextSetLineWidth(ctx, 0.8f);
    //3.1 画16条竖线
    for (int i = 0; i <= self.gridCount; i ++) {
      CGContextMoveToPoint(ctx, kBoardSpace + i * self.gridWidth , kBoardSpace);
      CGContextAddLineToPoint(ctx, kBoardSpace + i * self.gridWidth , kBoardSpace + self.gridCount * self.gridWidth);
    }
    //3.1 画16条横线
    for (int i = 0; i <= self.gridCount; i ++) {
      CGContextMoveToPoint(ctx, kBoardSpace, kBoardSpace + i * self.gridWidth );
      CGContextAddLineToPoint(ctx, kBoardSpace + self.gridCount * self.gridWidth , kBoardSpace + i * self.gridWidth);
    }
    CGContextStrokePath(ctx);
     
    //4.获取生成的图片
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    //5.显示生成的图片到imageview
    UIImageView * imageView = [[UIImageView alloc]initWithImage:image];
    [self addSubview:imageView];
    UIGraphicsEndImageContext();
}

2.点击棋盘落子

     1)根据落子位置求出该棋子的行号与列号.

     2)判断落子位置是否已经有棋子,有则不能下.如果没有,将棋子保存在字典中,以列号和行号组合起来的字符串为key值.

代码如下:

//点击棋盘,下棋
  - (void)tapBoard:(UITapGestureRecognizer *)tap{
   
    CGPoint point = [tap locationInView:tap.view];
    //计算下子的列号行号
    NSInteger col = (point.x - kBoardSpace + 0.5 * self.gridWidth) / self.gridWidth;
    NSInteger row = (point.y - kBoardSpace + 0.5 * self.gridWidth) / self.gridWidth;
    NSString * key = [NSString stringWithFormat:@"%ld-%ld",col,row];
    if (![self.chessmanDict.allKeys containsObject:key]) {
      UIView * chessman = [self chessman];
      chessman.center = CGPointMake(kBoardSpace + col * self.gridWidth, kBoardSpace + row * self.gridWidth);
      [self addSubview:chessman];
      [self.chessmanDict setValue:chessman forKey:key];
      self.lastKey = key;
      //检查游戏结果
      [self checkResult:col andRow:row andColor:self.isBlack];
      self.isBlack = !self.isBlack;
    }
  }

3.检测游戏结果

每落一个棋子就要多游戏结果进行一次检查,判断四个方向上是否有大于等于5个同色的棋子连成一线,有则提示游戏输赢结果,无则游戏继续.算法为,从当前棋子位置向前遍历,直到遇到与自己不同色的棋子,累加同色棋子的个数,再往后遍历,直到遇到与自己不同色的棋子,累加同色棋子的个数.得到该方向相连同色棋子的总个数

代码如下

//判断是否大于等于五个同色相连
  - (BOOL)checkResult:(NSInteger)col andRow:(NSInteger)row andColor:(BOOL)isBlack andDirection:(GmkDirection)direction{
   
    if (self.sameChessmanArray.count >= 5) {
      return YES;
    }
    UIColor * currentChessmanColor = [self.chessmanDict[[NSString stringWithFormat:@"%ld-%ld",col,row]] backgroundColor];
    [self.sameChessmanArray addObject:self.chessmanDict[self.lastKey]];
    switch (direction) {
      //水平方向检查结果
      case GmkHorizontal:{
        //向前遍历
        for (NSInteger i = col - 1; i > 0; i --) {
          NSString * key = [NSString stringWithFormat:@"%ld-%ld",i,row];
          if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor) break;
          [self.sameChessmanArray addObject:self.chessmanDict[key]];
        }
        //向后遍历
        for (NSInteger i = col + 1; i < kGridCount; i ++) {
          NSString * key = [NSString stringWithFormat:@"%ld-%ld",i,row];
          if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor) break;
          [self.sameChessmanArray addObject:self.chessmanDict[key]];
        }
        if (self.sameChessmanArray.count >= 5) {
          [self alertResult];
          return YES;
        }
        [self.sameChessmanArray removeAllObjects];
         
      }
        break;
      case GmkVertical:{
        //向前遍历
        for (NSInteger i = row - 1; i > 0; i --) {
          NSString * key = [NSString stringWithFormat:@"%ld-%ld",col,i];
          if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor) break;
          [self.sameChessmanArray addObject:self.chessmanDict[key]];
        }
        //向后遍历
        for (NSInteger i = row + 1; i < kGridCount; i ++) {
          NSString * key = [NSString stringWithFormat:@"%ld-%ld",col,i];
          if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor) break;
          [self.sameChessmanArray addObject:self.chessmanDict[key]];
        }
        if (self.sameChessmanArray.count >= 5) {
          [self alertResult];
          return YES;
        }
        [self.sameChessmanArray removeAllObjects];
         
      }
        break;
      case GmkObliqueDown:{
         
        //向前遍历
        NSInteger j = col - 1;
        for (NSInteger i = row - 1; i >= 0; i--,j--) {
          NSString * key = [NSString stringWithFormat:@"%ld-%ld",j,i];
          if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor || j < 0) break;
          [self.sameChessmanArray addObject:self.chessmanDict[key]];
        }
        //向后遍历
        j = col + 1;
        for (NSInteger i = row + 1 ; i < kGridCount; i++,j++) {
          NSString * key = [NSString stringWithFormat:@"%ld-%ld",j,i];
          if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor || j > kGridCount) break;
          [self.sameChessmanArray addObject:self.chessmanDict[key]];
        }
        if (self.sameChessmanArray.count >= 5) {
          [self alertResult];
          return YES;
        }
        [self.sameChessmanArray removeAllObjects];
         
       
      }
        break;
      case GmkObliqueUp:{
        //向前遍历
        NSInteger j = col + 1;
        for (NSInteger i = row - 1; i >= 0; i--,j++) {
          NSString * key = [NSString stringWithFormat:@"%ld-%ld",j,i];
          if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor || j > kGridCount) break;
          [self.sameChessmanArray addObject:self.chessmanDict[key]];
        }
        //向后遍历
        j = col - 1;
        for (NSInteger i = row + 1 ; i < kGridCount; i++,j--) {
          NSString * key = [NSString stringWithFormat:@"%ld-%ld",j,i];
          if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor || j < 0) break;
          [self.sameChessmanArray addObject:self.chessmanDict[key]];
        }
        if (self.sameChessmanArray.count >= 5) {
          [self alertResult];
          return YES;
        }
        [self.sameChessmanArray removeAllObjects];
         
      }
        break;
    }
    return NO;
  }

对外提供,重新开始,悔棋,切换初高级棋盘的三个接口

重新开始

- (void)newGame{
     
    self.isOver = NO;
    self.lastKey = nil;
    [self.sameChessmanArray removeAllObjects];
    self.userInteractionEnabled = YES;
    [self.chessmanDict removeAllObjects];
    for (UIView * view in self.subviews) {
      if ([view isKindOfClass:[UIImageView class]]) {
        continue;
      }
      [view removeFromSuperview];
    }
    self.isBlack = NO;
  }

悔棋

//撤回至上一步棋
  - (void)backOneStep:(UIButton *)sender{
   
    if(self.isOver) return;
     
    if (self.lastKey == nil) {
      sender.enabled = NO;
      CGFloat width = SCREEN_WIDTH * 0.4 * SCREEN_WIDTH_RATIO;
      UIView * tip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 0.6 * width)];
      tip.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
      tip.layer.cornerRadius = 8.0f;
      [self addSubview:tip];
      tip.center = CGPointMake(self.width * 0.5, self.height * 0.5);
      UILabel * label = [[UILabel alloc]init];
      label.text = self.chessmanDict.count > 0 ? @"只能悔一步棋!!!" : @"请先落子!!!";
      label.font = [UIFont systemFontOfSize:15];
      [label sizeToFit];
      label.center = CGPointMake(tip.width * 0.5, tip.height * 0.5);
      [tip addSubview:label];
       
      self.userInteractionEnabled = NO;
      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.userInteractionEnabled = YES;
        sender.enabled = YES;
        [tip removeFromSuperview];
         
      });
      return;
    }
    [self.chessmanDict removeObjectForKey:self.lastKey];
    [self.subviews.lastObject removeFromSuperview];
    self.isBlack = !self.isBlack;
    self.lastKey = nil;
  }

切换初高级键盘

//改变键盘级别
  - (void)changeBoardLevel{
     
    for (UIView * view in self.subviews) {
      [view removeFromSuperview];
    }
    [self newGame];
    self.isHighLevel = !self.isHighLevel;
    [self drawBackground:self.bounds.size];
  }

Demo中的一个小技巧

用字典存放棋子,以棋子的列号和行号组合起来的字符串为key值,value值为棋子view.这样处理,在判断某行某列是否有棋子就非常简单了。

总结

以上就是iOS游戏开发之五子棋OC版的全部内容,希望本文对大家开发IOS有所帮助,如果本文有不足之处,欢迎大家提供建议和指点!

相关文章

  • iOS NSDate中关于夏令时的坑

    iOS NSDate中关于夏令时的坑

    这篇文章主要给大家介绍了关于iOS NSDate中夏令时的坑,以及iOS 时间字符串&NSDate&时间戳 相互转换的相关内容,分享出来供大家参考学习,需要的朋友可以参考借鉴,下面随小编来一起学习学习吧
    2018-08-08
  • MAUI模仿iOS多任务切换卡片滑动的交互实现代码

    MAUI模仿iOS多任务切换卡片滑动的交互实现代码

    这篇文章主要介绍了[MAUI]模仿iOS多任务切换卡片滑动的交互实现,使用.NET MAU实现跨平台支持,本项目可运行于Android、iOS平台,需要的朋友可以参考下
    2023-05-05
  • iOS开发-实现大文件下载与断点下载思路

    iOS开发-实现大文件下载与断点下载思路

    本篇文章主要介绍了iOS开发-实现大文件下载与断点下载思路,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-01-01
  • iOS系统缓存方面开发的相关基础

    iOS系统缓存方面开发的相关基础

    这篇文章主要介绍了iOS系统缓存方面开发的相关基础,示例代码基于传统的Objective-C,需要的朋友可以参考下
    2015-10-10
  • 超全的iOS各种设备信息获取方法总结(包括iPhone8/iPhone X)

    超全的iOS各种设备信息获取方法总结(包括iPhone8/iPhone X)

    这篇文章主要给大家介绍了关于iOS各种设备信息获取方法,iPhone8/iPhone X的后驱详细信息也已更新,文中给出了详细的示例代码供大家参考学习,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • iOS实现爆炸的粒子效果示例代码

    iOS实现爆炸的粒子效果示例代码

    之前在网上看到了一个Android实现的爆炸效果,感觉非常不错,所以自己尝试用iOS来实现下效果,现在将实现的过程、原理以及遇到的问题分享给大家,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-10-10
  • 举例讲解iOS应用开发中对设计模式中的策略模式的使用

    举例讲解iOS应用开发中对设计模式中的策略模式的使用

    这篇文章主要介绍了iOS应用设计中对设计模式中的策略模式的使用,示例代码为传统的Objective-C语言,需要的朋友可以参考下
    2016-03-03
  • iOS中常用的宏定义总结

    iOS中常用的宏定义总结

    这篇文章主要给大家介绍了关于iOS中常用的宏定义的相关资料,例如UI元素、Log、系统、颜色类等等的示例代码,文中介绍地方非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
    2018-08-08
  • IOS 开发中画扇形图实例详解

    IOS 开发中画扇形图实例详解

    这篇文章主要介绍了IOS 开发中画扇形图实例详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • iOS实现类似格瓦拉电影的转场动画

    iOS实现类似格瓦拉电影的转场动画

    这篇文章主要给大家介绍了利用iOS如何实现类似格瓦拉电影的转场动画,文中给出了详细步骤实现代码,对大家的学习和理解很有帮助,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-11-11

最新评论