最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS游戲開發(fā)之五子棋OC版

 更新時間:2016年08月09日 17:06:39   投稿:daisy  
五子棋是大家比較熟悉的一款小游戲,相信很多人用多種語言寫過五子棋小游戲,本文試著用OC實現(xiàn)了一下,在這里給大家分享一下。有需要的可以參考學習。

先上效果圖

- 功能展示

- 初高級棋盤切換效果

實現(xiàn)思路及主要代碼詳解

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)根據(jù)落子位置求出該棋子的行號與列號.

     2)判斷落子位置是否已經(jīng)有棋子,有則不能下.如果沒有,將棋子保存在字典中,以列號和行號組合起來的字符串為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;
      //檢查游戲結(jié)果
      [self checkResult:col andRow:row andColor:self.isBlack];
      self.isBlack = !self.isBlack;
    }
  }

3.檢測游戲結(jié)果

每落一個棋子就要多游戲結(jié)果進行一次檢查,判斷四個方向上是否有大于等于5個同色的棋子連成一線,有則提示游戲輸贏結(jié)果,無則游戲繼續(xù).算法為,從當前棋子位置向前遍歷,直到遇到與自己不同色的棋子,累加同色棋子的個數(shù),再往后遍歷,直到遇到與自己不同色的棋子,累加同色棋子的個數(shù).得到該方向相連同色棋子的總個數(shù)

代碼如下

//判斷是否大于等于五個同色相連
  - (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) {
      //水平方向檢查結(jié)果
      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.這樣處理,在判斷某行某列是否有棋子就非常簡單了。

總結(jié)

以上就是iOS游戲開發(fā)之五子棋OC版的全部內(nèi)容,希望本文對大家開發(fā)IOS有所幫助,如果本文有不足之處,歡迎大家提供建議和指點!

相關(guān)文章

  • iOS NSDate中關(guān)于夏令時的坑

    iOS NSDate中關(guān)于夏令時的坑

    這篇文章主要給大家介紹了關(guān)于iOS NSDate中夏令時的坑,以及iOS 時間字符串&NSDate&時間戳 相互轉(zhuǎn)換的相關(guān)內(nèi)容,分享出來供大家參考學習,需要的朋友可以參考借鑒,下面隨小編來一起學習學習吧
    2018-08-08
  • MAUI模仿iOS多任務(wù)切換卡片滑動的交互實現(xiàn)代碼

    MAUI模仿iOS多任務(wù)切換卡片滑動的交互實現(xiàn)代碼

    這篇文章主要介紹了[MAUI]模仿iOS多任務(wù)切換卡片滑動的交互實現(xiàn),使用.NET MAU實現(xiàn)跨平臺支持,本項目可運行于Android、iOS平臺,需要的朋友可以參考下
    2023-05-05
  • iOS開發(fā)-實現(xiàn)大文件下載與斷點下載思路

    iOS開發(fā)-實現(xiàn)大文件下載與斷點下載思路

    本篇文章主要介紹了iOS開發(fā)-實現(xiàn)大文件下載與斷點下載思路,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-01-01
  • iOS系統(tǒng)緩存方面開發(fā)的相關(guān)基礎(chǔ)

    iOS系統(tǒng)緩存方面開發(fā)的相關(guān)基礎(chǔ)

    這篇文章主要介紹了iOS系統(tǒng)緩存方面開發(fā)的相關(guān)基礎(chǔ),示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-10-10
  • 超全的iOS各種設(shè)備信息獲取方法總結(jié)(包括iPhone8/iPhone X)

    超全的iOS各種設(shè)備信息獲取方法總結(jié)(包括iPhone8/iPhone X)

    這篇文章主要給大家介紹了關(guān)于iOS各種設(shè)備信息獲取方法,iPhone8/iPhone X的后驅(qū)詳細信息也已更新,文中給出了詳細的示例代碼供大家參考學習,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-12-12
  • iOS實現(xiàn)爆炸的粒子效果示例代碼

    iOS實現(xiàn)爆炸的粒子效果示例代碼

    之前在網(wǎng)上看到了一個Android實現(xiàn)的爆炸效果,感覺非常不錯,所以自己嘗試用iOS來實現(xiàn)下效果,現(xiàn)在將實現(xiàn)的過程、原理以及遇到的問題分享給大家,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-10-10
  • 舉例講解iOS應(yīng)用開發(fā)中對設(shè)計模式中的策略模式的使用

    舉例講解iOS應(yīng)用開發(fā)中對設(shè)計模式中的策略模式的使用

    這篇文章主要介紹了iOS應(yīng)用設(shè)計中對設(shè)計模式中的策略模式的使用,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下
    2016-03-03
  • iOS中常用的宏定義總結(jié)

    iOS中常用的宏定義總結(jié)

    這篇文章主要給大家介紹了關(guān)于iOS中常用的宏定義的相關(guān)資料,例如UI元素、Log、系統(tǒng)、顏色類等等的示例代碼,文中介紹地方非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧
    2018-08-08
  • IOS 開發(fā)中畫扇形圖實例詳解

    IOS 開發(fā)中畫扇形圖實例詳解

    這篇文章主要介紹了IOS 開發(fā)中畫扇形圖實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • iOS實現(xiàn)類似格瓦拉電影的轉(zhuǎn)場動畫

    iOS實現(xiàn)類似格瓦拉電影的轉(zhuǎn)場動畫

    這篇文章主要給大家介紹了利用iOS如何實現(xiàn)類似格瓦拉電影的轉(zhuǎn)場動畫,文中給出了詳細步驟實現(xiàn)代碼,對大家的學習和理解很有幫助,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-11-11

最新評論

葫芦岛市| 宜都市| 苍梧县| 玛多县| 长武县| 永年县| 金川县| 盘山县| 永修县| 乌鲁木齐县| 永川市| 宝山区| 沂源县| 民乐县| 开化县| 谢通门县| 宁津县| 三亚市| 邢台市| 沈阳市| 高要市| 嘉祥县| 达拉特旗| 厦门市| 河南省| 湛江市| 华亭县| 平安县| 通山县| 登封市| 泽州县| 大余县| 乐山市| 百色市| 延寿县| 大洼县| SHOW| 莎车县| 金沙县| 阳城县| 梅州市|