IOS游戏开发之五子棋OC版

2020-01-15 17:38:26丽君

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

重新开始


- (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开发频道。