服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - IOS游戏开发之五子棋OC版

IOS游戏开发之五子棋OC版

2021-01-23 16:57iOS开发网 IOS

五子棋是大家比较熟悉的一款小游戏,相信很多人用多种语言写过五子棋小游戏,本文试着用OC实现了一下,在这里给大家分享一下。有需要的可以参考学习。

先上效果图

- 功能展示

IOS游戏开发之五子棋OC版

- 初高级棋盘切换效果

IOS游戏开发之五子棋OC版

实现思路及主要代码详解

1.绘制棋盘

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- (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值.

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//点击棋盘,下棋
  - (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个同色的棋子连成一线,有则提示游戏输赢结果,无则游戏继续.算法为,从当前棋子位置向前遍历,直到遇到与自己不同色的棋子,累加同色棋子的个数,再往后遍历,直到遇到与自己不同色的棋子,累加同色棋子的个数.得到该方向相连同色棋子的总个数

代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//判断是否大于等于五个同色相连
  - (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;
  }

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

重新开始

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (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;
  }

悔棋

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//撤回至上一步棋
  - (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;
  }

切换初高级键盘

?
1
2
3
4
5
6
7
8
9
10
//改变键盘级别
  - (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有所帮助,如果本文有不足之处,欢迎大家提供建议和指点!

延伸 · 阅读

精彩推荐
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17