Object-C
题 目:基于
IOS
平台的游戏“五子棋”
程序设计
学 院: 软件学院
专 业: 软件工程
学生姓名:
学 号:
指导教师:
职 称:
题目类型:
理论研究
实验研究
工程设计
工程技术研究 软件开发
2012 年 5 月 30 日
摘 要
本论文主要阐述以 iOS 开发平台为基础,通过使用 Xcode 开发工具以及 objective-c 和 c++语言开发
的一款运行在 iPad 上的智力游戏“五子棋”。五子棋是一种两人对弈的纯策略型棋类游戏,棋具与围棋通
用,是起源于中国古代的传统黑白棋种之一。近年来,随着智能手机的流行,出现了许多在移动操作系
统上的手机应用。所以,开发一款可以在 iOS 系统上运行的五子棋游戏是非常有意义的。
在开发的过程中,我首先学习了 objective-c 语言的相关语法,objective-c 语言是在 C 语言上进行一
些面向对象的扩充,学习它大概用了一周的时间。接下来,开始学习 iOS 应用的构建原理(学习视图控
制器与视图的使用方法,程序委托的使用等)以及一些常用控件的使用,并尝试在 Xcode 工具上搭建一
些简单的界面。这时候的界面是用 xib 文件来搭建的,通过直接拖拉控件来产生界面。之后,开始尝试
用代码写控件来代替 xib 文件。在做好这些基础的准备后,我开始动手搭建五子棋的界面,五子棋界面
除了一些常用的控件(按钮以及标签)外,重要的是画出棋盘以及棋子,棋盘和棋子不能用普通控件来
显示,需要使用绘图的方法把它画出来。在这些工作完成了之后,界面就可以显示出来了。
接下来,就要在棋盘类上面进行一些处理工作,比如点击各个按钮触发的事件,在游戏过程中玩
家点击棋盘触发的事件,判断游戏输赢,悔棋和认输功能的实现等等。之后,就开始设计与实现五子棋
算法了。经过多年的发展,五子棋的算法已经较为完善,我做的工作是在理解这些算法原理的基础上,
用自己的话来实现,并加入一些新的东西。五子棋算法一般包括估值算法以及搜索算法,估值算法的实
现虽然代码量较大但是理解起来不是特别难,而我在学习搜索算法的过程中却在一开始的时候无法理解
他的 alpha-beta 剪枝以及负极大值算法的意思。而在一开始写完算法部分代码之后,也还是存在许多问
题,无法搜索出正确的落子点,在经过一些排错与完善之后,算法的实现可以与前面的棋盘进行结合了。
这样,整个设计就差不多完成了,接下来就需要进行测试并进行一些小的修改。
在本论文中,主要阐述了开发过程中的一些细节,遇到的问题,解决的方法以及自己的一些感悟。
关键词:objective-c;人工智能;算法
Abstract
This paper mainly expounded a mental game gobang .It is on the basis of iOS development platform, using
the Xcode development tools and objective - c and c + + language to develop,and runed in the iPad. Gobang is a
game of two pure strategy type chess game. Chess can be used in the game of go.Gobang is one of the traditional
reversi which is originated from the ancient Chinese . In recent years, with the popularity of smart phones, Many
mobile applications on mobile operating system appeared.So, developing a Gobang game can be run on iOS is
meaningful.
In the process of development, the first thing is learning the syntax of objective-c language,the objective-c
language is in the basis of c language and add some Object oriented expansion , it took me about a week's time
to study.Next, I start to learn the building principle of iOS app (learning the usage of view controller and view ,
the use of application of delegate and so on ) and the use of some common controls, and try to make some
simple interface on Xcode tool structures.At this time the xib file interface is used to build, through direct drag
controls to generate the interface.Next, I began to try to replace the xib file by writting codes.After doing this
things, I start to build gobang interface, in addition to some commonly used controls (buttons and labels), it is
important to draw the chessboard and chess ,the board and chess can not use normal controls to display, I need to
draw them.After the work finished, interface can be displayed.
Next, I should do some work on board, such as the events by click each button , the events by click board,
the function of judes success ,giving up and regret and so on.Then,I began to design and realize the gobang
algorithm. Gobang algorithm has been more mature, my work is understanding the algorithm and relize it, and I
will add some new things.Gobang algorithm generally includes valuation algorithm and search algorithm, the
code of valuation algorithm is large but I do not hard to undstand it .Instead ,I can't understand the alpha beta
pruning and negative maximum value algorithm. And after finished the algorithm , it still exist many problems,
it is unable to search the right move place, after some troubleshooting and improvement, the realization of the
algorithm can be combined with the front board.So, the whole design is almost finished, you need to test and
make a few small changes.
This paper mainly expounds some details in the development process, implementation methods,
difficulties, and some of my own feeling.
Key words:objective-c;Artificial intellegence;algorithm
目 录
引言................................................................................................................................................ 1
1 手机五子棋游戏介绍.......................................................................................................... 2
1.1 五子棋游戏规则介绍....................................................................................................................... 2
1.2 五子棋游戏术语介绍....................................................................................................................... 3
1.3 手机五子棋特色................................................................................................................................. 3
2 开发环境及工具介绍.......................................................................................................... 3
2.1 开发环境............................................................................................................................................... 3
2.2 运行环境............................................................................................................................................... 4
2.3 工具介绍............................................................................................................................................... 4
2.3.1 iOS 介绍............................................................................................................................................................4
2.3.2 objective-c 介绍............................................................................................................................................4
3 需求分析与总体设计.......................................................................................................... 5
3.1 需求分析............................................................................................................................................... 5
3.2 系统设计思想...................................................................................................................................... 5
3.3 系统总体设计...................................................................................................................................... 5
3.4 系统模块及功能................................................................................................................................. 6
3.4.1 系统主要模块................................................................................................................................................6
3.4.2 系统主要流程................................................................................................................................................7
4 五子棋 AI 算法分析与实现............................................................................................... 8
4.1 算法总体概况...................................................................................................................................... 8
4.2 估值算法分析与实现....................................................................................................................... 8
4.3 搜索算法的分析与实现................................................................................................................ 13
4.4 算法设计的不足以及改进方法................................................................................................. 18
5 APP 应用详细设计............................................................................................................ 18
5.1 APP 设计介绍................................................................................................................................... 18
5.1.1 main 函数介绍............................................................................................................................................18
5.1.2 应用程序委托介绍....................................................................................................................................19
5.1.3 视图控制器介绍.........................................................................................................................................19
5.1.4 视图类介绍..................................................................................................................................................19
5.1.5 MVC 模型介绍............................................................................................................................................20
5.1.6 主要框架介绍:.........................................................................................................................................20
5.2 详细设计概述................................................................................................................................... 20
5.3 视图控制器类设计......................................................................................................................... 21
5.3.1 积分榜的设计..............................................................................................................................................21
5.3.2 委托类的设计与使用...............................................................................................................................21
5.3.3 按钮的设计...................................................................................................................................................21
5.4 棋盘类的设计................................................................................................................................... 21
5.4.1 棋盘类变量设计.........................................................................................................................................21
5.4.2 棋盘的绘制...................................................................................................................................................22
5.4.3 玩家和机器人下子....................................................................................................................................22
5.4.4 棋子类的实现..............................................................................................................................................23
5.4.5 判断胜负功能..............................................................................................................................................23
5.4.6 游戏新局功能的实现...............................................................................................................................23
5.4.7 认输功能的实现.........................................................................................................................................24
5.4.8 悔棋功能的实现.........................................................................................................................................24
5.4.9 人人对战的实现.........................................................................................................................................24
6 开发过程中遇到的问题................................................................................................... 25
6.1 五子棋 AI 算法设计问题.............................................................................................................. 25
6.2 棋盘类设计遇到的问题................................................................................................................ 25
7 测试......................................................................................................................................... 26
8 总结......................................................................................................................................... 31
谢 辞........................................................................................................................................... 33
参考文献................................................................................................................................... 34