安卓大作业(共28页)_第1页
安卓大作业(共28页)_第2页
安卓大作业(共28页)_第3页
安卓大作业(共28页)_第4页
安卓大作业(共28页)_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上Android应用程序开发大作业题 目: 五子棋游戏的设计与实现 专 业: 网络工程 学 号: 学生姓名: 朱一萍 指导教师: 齐兵辉 完成日期: 2015-11-25 专心-专注-专业目录1开发背景每个人手机上都有手机游戏,上下班以及无聊闲暇时,游戏是人们娱乐的一种方式。Android平台下的手机游戏更是受到顾客的青睐,手机游戏不仅玩起来比较方便,还有助于开发我们的思维,使大家在娱乐的同时也增长了见识,拓展了思维。五子棋对思维及协调能力有益,能使我们注意力集中,耐心也有明显的提升,培养我们的逻辑思维能力,对智力及记忆力都很有帮助。2.需求分析2.1功能需求1.适合不

2、同阶段的玩家(新手和熟手)2.玩家和手机对弈2.2性能需求软件环境:1.硬件环境:PC机2Windows XP、Android 2.1 以上3.概要设计五子棋分三个模块:开始游戏、设置、退出。1. 开始游戏:点击进入游戏2. 游戏设置:音效设置:开启/关闭背景音乐、开启/关闭音效难度设置:简单,中等,较难3. 退出游戏关闭游戏4.棋盘、棋子的绘制本设计中棋盘的设置采用drawLine(float startX,float startY,float stopX,float stopY,Paint paint)画线,参数一起始点的X轴位置,参数二起始点的Y轴位置,参数三终点的水平位置,参数四垂直位

3、置,最后的一个参数为Paint画刷对象。本设计中棋子是用drawCircle在坐标上绘制一个经典几何图形。先判断是否有棋,是黑子还是白子,然后判断是那个坐标,然后调用drawCircle绘制棋子canvas.drawCircle(float,float,float,Paint)方法用于画圆,前两个参数代表圆心坐标,第三个参数为圆半径,第四个参数是画笔。5.游戏核心设计5.1手机选择落子位置评估当前棋局中,某个位置的得分。五子棋要赢,必然要有五个棋子在一起成线,那么我们就可以计算棋盘中每一个五格相连的线,一下称之为五元组。一般情况(包括专业五子棋)下棋盘是15*15的。那么应该是572个五元组。

4、同时,针对五元组中黑子和白子的数量(可以不考虑相对位置)的不同,给该五元组评不同的分。然后每一个位置的得分就是包含这个位置的所有五元组的得分之和。说明:当五元组为空,分数为7,不为零的原因是,还有跟糟的情况:五元组中既有黑子又有白子,五元组也就是无效了,这时才给0分。根据得到的评分,手机选出最佳位置,进行落子。5.2判断游戏的输赢在判断输赢时,用到的一个巧妙的方法。从所周知,无论谁下棋赢了以后总会成五子连线,而成五子连线时,第五颗棋总是会着落在棋盘上,所以我们在判断是否有五子连线时,并不是全盘扫描,而是在每着落一子后就扫描该棋子左右、上下、以及斜着的四个方向。如果扫描出来了那个方向上的棋子首先

5、到达五子,就返回给上级调用,由上级调用者作出相应判断。如果是其它子,如果某个方向上成了六子或六子以上时,不能算作它赢得了棋,而必是五子才能赢得了棋。如果是五子以下的棋子时,将要根局相应棋子成子情况,作出相应反应,如加权、减权等操作。6.运行截图首界面:开始游戏:设置:判断输赢:7.核心代码7.1首界面public class StartActivity extends ActivityOverrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);this.setCont

6、entView(R.layout.activity_start);Button button1 = (Button) findViewById(R.id.button1);button1.setOnClickListener(new View.OnClickListener()Overridepublic void onClick(View arg0) / TODO Auto-generated method stubIntent intent1 = new Intent();intent1.setClass(StartActivity.this, MainActivity.class);st

7、artActivity(intent1);StartActivity.this.finish(););Button button2 = (Button) findViewById(R.id.button2);button2.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) / TODO Auto-generated method stubIntent intent2 = new Intent();intent2.setClass(StartActivity.this, settin

8、gActivity.class);startActivity(intent2);StartActivity.this.finish(););Button button3 = (Button) findViewById(R.id.button3);button3.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View arg0) / TODO Auto-generated method stubStartActivity.this.finish(););7.2棋盘、棋子的初始化等private

9、ChessType chessMap = new ChessTypeROWSCOLS;private static float PADDING = (float) (screenWidth) / (COLS - 1) / 2;private static float PADDING_LEFT = (float) (screenWidth) / (COLS - 1) / 2;private static float PADDING_TOP = (float) (screenHeight) / (ROWS - 1) / 2;private static float ROW_MARGIN = (sc

10、reenHeight - PADDING * 2)/ (ROWS - 1);private static float COL_MARGIN = (screenWidth - PADDING * 2)/ (COLS - 1);private static float MARGIN;public GameView(Context context) super(context);this.context = context;this.setBackgroundResource(R.drawable.bg);PADDING_LEFT = (screenWidth) / (COLS - 1) / 2;P

11、ADDING_TOP = (screenHeight) / (ROWS - 1) / 2;PADDING = PADDING_LEFT < PADDING_TOP ? PADDING_LEFT : PADDING_TOP;ROW_MARGIN = (screenHeight - PADDING * 2) / (ROWS - 1);COL_MARGIN = (screenWidth - PADDING * 2) / (COLS - 1);MARGIN = ROW_MARGIN < COL_MARGIN ? ROW_MARGIN : COL_MARGIN;PADDING_LEFT =

12、(screenWidth - (COLS - 1) * MARGIN) / 2;PADDING_TOP = (screenHeight - (ROWS - 1) * MARGIN) / 2;/ 对棋子进行初始化initChess();/ System.out.println(PADDING_LEFT + " " + PADDING_TOP);/对棋子进行初始化public void initChess() for (int i = 0; i < ROWS; i+) for (int j = 0; j < COLS; j+) chessMapij = ChessT

13、ype.NONE;invalidate(); /画棋盘Overrideprotected void onDraw(Canvas canvas) super.onDraw(canvas);Paint paint = new Paint();paint.setColor(Color.WHITE);/ 打印行for (int i = 0; i < ROWS; i+) canvas.drawLine(PADDING_LEFT, i * MARGIN + PADDING_TOP, (COLS - 1)* MARGIN + PADDING_LEFT, i * MARGIN + PADDING_TOP

14、, paint);/ 打印列for (int i = 0; i < COLS; i+) canvas.drawLine(PADDING_LEFT + i * MARGIN, PADDING_TOP,PADDING_LEFT + i * MARGIN, MARGIN * (ROWS - 1)+ PADDING_TOP, paint);for (int r = 0; r < ROWS; r+) for (int c = 0; c < COLS; c+) / System.out.print(chessMaprc + " ");if (chessMaprc =

15、ChessType.NONE)continue;if (chessMaprc = ChessType.BLACK) paint.setColor(Color.BLACK);canvas.drawCircle(r * MARGIN + PADDING_LEFT, c * MARGIN+ PADDING_TOP, MARGIN / 2, paint); else if (chessMaprc = ChessType.WHITE) paint.setColor(Color.WHITE);canvas.drawCircle(r * MARGIN + PADDING_LEFT, c * MARGIN+

16、PADDING_TOP, MARGIN / 2, paint);/ System.out.println();7.3 判断输赢public boolean hasWin(int r, int c) ChessType chessType = chessMaprc;System.out.println(chessType);int count = 1;/ 纵向搜索for (int i = r + 1; i < r + 5; i+) if (i >= GameView.ROWS)break;if (chessMapic = chessType) count+; elsebreak;fo

17、r (int i = r - 1; i > r - 5; i-) if (i < 0)break;if (chessMapic = chessType)count+;elsebreak;/ System.out.println(count +" "+"1");if (count >= 5)return true;/ 横向搜索count = 1;for (int i = c + 1; i < c + 5; i+) if (i >= GameView.COLS)break;if (chessMapri = chessType)c

18、ount+;elsebreak;for (int i = c - 1; i > c - 5; i-) if (i < 0)break;if (chessMapri = chessType)count+;elsebreak;/ System.out.println(count +" " +"2");if (count >= 5)return true;/ 斜向""count = 1;for (int i = r + 1, j = c + 1; i < r + 5; i+, j+) if (i >= Game

19、View.ROWS | j >= GameView.COLS) break;if (chessMapij = chessType)count+;elsebreak;for (int i = r - 1, j = c - 1; i > r - 5; i-, j-) if (i < 0 | j < 0)break;if (chessMapij = chessType)count+;elsebreak;/ System.out.println(count +" " +"3");if (count >= 5)return true;

20、/ 斜向"/"count = 1;for (int i = r + 1, j = c - 1; i < r + 5; i+, j-) if (i >= GameView.ROWS | j < 0)break;if (chessMapij = chessType)count+;elsebreak;for (int i = r - 1, j = c + 1; i > r - 5; i-, j+) if (i < 0 | j >= GameView.COLS)break;if (chessMapij = chessType)count+;els

21、ebreak;/ System.out.println(count +" " +"4");if (count >= 5)return true;return false;7.4手机落子算法 /* 得到最优点 * * return 得到最优的点 */private Point getBestPoint() initValue();for (int r = 0; r < GameView.ROWS; r+) for (int c = 0; c < GameView.COLS; c+) if (chessMaprc = ChessType.N

22、ONE) puterMaprc = getValue(r, c, puterType);this.playerMaprc = getValue(r, c, this.playerType);int pcMax = 0, playerMax = 0;Random rd = new Random();Point pcPoint = new Point(-1, -1);Point playerPoint = new Point();/ 分别选出pc估分和玩家估分的最大值for (int r = 0; r < GameView.ROWS; r+) for (int c = 0; c < G

23、ameView.COLS; c+) / 选出电脑估分的最大值if (pcMax = computerMaprc) if (rd.nextInt(10) % 2 = 0) pcMax = computerMaprc;pcPoint.x = r;pcPoint.y = c; else if (pcMax < computerMaprc) pcMax = computerMaprc;pcPoint.x = r;pcPoint.y = c;/ 选出玩家估分的最大值if (playerMax = playerMaprc) if (rd.nextInt(10) % 2 = 0) playerMax

24、= playerMaprc;playerPoint.x = r;playerPoint.y = c; else if (playerMax < playerMaprc) playerMax = playerMaprc;playerPoint.x = r;playerPoint.y = c;/ 两者都在90100分之间,优先选择PCif (pcMax >= 90 && pcMax < 100 && playerMax >= 90 && playerMax < 100) return pcPoint; else retu

25、rn playerMax > pcMax ? playerPoint : pcPoint; /* 得到该位置的分数 * * param r * 该棋子所在的行数 * param c * 该棋子所在的列数 * param chessType * 棋子的类型 * return 得到的分数 */private int getValue(int r, int c, ChessType chessType) int dir = new int4;dir0 = this.getHorCount(r, c, chessType);dir1 = this.getVerCount(r, c, chessT

26、ype);dir2 = this.getSloRCount(r, c, chessType);dir3 = this.getSloLCount(r, c, chessType);/ 成五for (int i = 0; i < dir.length; i+) if (diri >= 5)return ScoreTable.FIVE;int temp = 0;/ 双活四for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi != ChessStatus.DIED)temp+;if (temp

27、 = 2)return ScoreTable.DOUBLE_ALIVE_FOUR;int t1 = 0, t2 = 0;/ 活四死四for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.ALIVE)t1 = 1;if (diri = 4 && chessStatusi != ChessStatus.DIED)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.ALIVE_FOUR_AND_DEAD_

28、FOUR;/ 活四活三t1 = 0;t2 = 0;for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi != ChessStatus.DIED)t1 = 1;if (diri = 3 && chessStatusi = ChessStatus.ALIVE)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.ALIVE_FOUR_AND_ALIVE_THREE;/ 活四死三t1 = 0;t2 = 0;for (int i = 0

29、; i < dir.length; i+) if (diri = 4 && chessStatusi != ChessStatus.DIED)t1 = 1;if (diri = 3 && chessStatusi != ChessStatus.DIED)t2 = 1;if (t1 = 1 && t2 = 1) return ScoreTable.ALIVE_FOUR_AND_DEAD_THREE;/ 活四活二t1 = 0;t2 = 0;for (int i = 0; i < dir.length; i+) if (diri = 4 &

30、amp;& chessStatusi != ChessStatus.DIED)t1 = 1;if (diri = 2 && chessStatusi != ChessStatus.ALIVE)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.ALIVE_FOUR_AND_ALIVE_TWO;/ 活四for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.ALIVE) return Score

31、Table.ALIVE_FOUR;/ 双死四temp = 0;for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.DIED)temp+;if (temp = 2)return ScoreTable.DOUBLE_DEAD_FOUR;/ 死四活3t1 = 0;t2 = 0;for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.DIED)t1 =

32、1;if (diri = 3 && chessStatusi != ChessStatus.DIED)t2 = 1;if (t1 = 1 && t2 = 1) return ScoreTable.DEAD_FOUR_AND_ALIVE_THREE;/ 死四活2t1 = 0;t2 = 0;for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.DIED)t1 = 1;if (diri = 2 && chessStatusi !

33、= ChessStatus.DIED)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.DEAD_FOUR_AND_ALIVE_TWO;/ 双活三temp = 0;for (int i = 0; i < dir.length; i+) if (diri = 3 && chessStatusi != ChessStatus.DIED)temp+;if (temp = 2)return ScoreTable.DOUBLE_ALIVE_THREE;/ 活死三t1 = 0;t2 = 0;for (int i = 0; i

34、< dir.length; i+) if (diri = 3 && chessStatusi = ChessStatus.ALIVE)t1 = 1;if (diri = 3 && chessStatusi = ChessStatus.DIED)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.ALIVE_THREE_AND_DEAD_THREE;/ 活三for (int i = 0; i < dir.length; i+) if (diri = 3 && chessStatus

35、i = ChessStatus.ALIVE)return ScoreTable.ALIVE_THREE;/ 死四for (int i = 0; i < dir.length; i+) if (diri = 4 && chessStatusi = ChessStatus.DIED)return ScoreTable.DEAD_FOUR;/ 半活死3t1 = 0;t2 = 0;for (int i = 0; i < 4; i+) if (diri = 3 && chessStatusi = ChessStatus.DIED)t1 = 1;if (diri

36、 = 3 && chessStatusi = ChessStatus.HALFALIVE)t2 = 1;if (t1 = 1 && t2 = 1)return ScoreTable.ALIVE_THREE_AND_DEAD_THREE;/ 双活2temp = 0;for (int i = 0; i < 4; i+) if (diri = 2 && chessStatusi = ChessStatus.ALIVE)temp+;if (temp = 2)return ScoreTable.DOUBLE_ALIVE_TWO;/ 死3for (in

37、t i = 0; i < 4; i+)if (diri = 3 && chessStatusi = ChessStatus.DIED)return ScoreTable.DEAD_THREE;/ 活2for (int i = 0; i < 4; i+)if (diri = 2 && chessStatusi = ChessStatus.ALIVE)return ScoreTable.ALIVE_TWO;/ 死2for (int i = 0; i < 4; i+)if (diri = 2 && chessStatusi = Che

38、ssStatus.DIED)return ScoreTable.DEAD_TWO;return 0; /* 横向搜索 * * param r * 该棋子所在的行数 * param c * 该棋子所在的列数 * param chessType * 棋子的类型 * return 得到的个数 */private int getHorCount(int r, int c, ChessType chessType) int count = 1;int t1 = c + 1;int t2 = c - 1;for (int j = c + 1; j < c + 5; j+) if (j >= G

39、ameView.COLS) chessStatus0 = ChessStatus.DIED;break;if (chessMaprj = chessType) count+;if (count >= 5)return count; else chessStatus0 = (chessMaprj = ChessType.NONE) ? ChessStatus.ALIVE: ChessStatus.DIED;t1 = j;break;for (int j = c - 1; j > c - 5; j-) if (j < 0) if (chessStatus0 = ChessStat

40、us.DIED && count < 5) return 0;chessStatus0 = ChessStatus.DIED;break;if (chessMaprj = chessType) count+;if (count >= 5)return count; else if (chessStatus0 = ChessStatus.DIED) if (count < 5 && chessMaprj != ChessType.NONE) return 0; else chessStatus0 = (chessMaprj) = ChessTyp

41、e.NONE ? ChessStatus.ALIVE: ChessStatus.DIED;t2 = j;/ 记录遇到的空格/ 当两端都活的时候,看是否可以延伸if (chessStatus0 = ChessStatus.ALIVE) int tempCount1 = count, tempCount2 = count;boolean isAlive1 = false, isAlive2 = false;for (int i = t1 + 1; i < t1 + 5; i+) if (i >= GameView.ROWS)break;if (chessMapri = chessTyp

42、e) tempCount1+; else isAlive1 = (chessMapri = ChessType.NONE) ? true: false;break;for (int i = t2 - 1; i > t2 - 5; i-) if (i < 0)break;if (chessMapri = chessType) tempCount2+; else isAlive2 = (chessMapri = ChessType.NONE) ? true: false;break;/ 如果两头都是空,直接跳出if (tempCount1 = count && temp

43、Count2 = count)break;if (tempCount1 = tempCount2) count = tempCount1;chessStatus0 = (isAlive1 && isAlive2) ? ChessStatus.HALFALIVE: ChessStatus.DIED; else count = (tempCount1 > tempCount2) ? tempCount1: tempCount2;if (count >= 5)return 0;if (tempCount1 > tempCount2)chessStatus0 = (i

44、sAlive1) ? ChessStatus.HALFALIVE: ChessStatus.DIED;elsechessStatus0 = (isAlive2) ? ChessStatus.HALFALIVE: ChessStatus.DIED;break;return count; /* 纵向搜索 * * param chessType * 要搜索的棋子类型 * param r * 棋子所在的行 * param c * 棋子所在的列 * return */private int getVerCount(int r, int c, ChessType chessType) int t1 = r

45、 + 1;int t2 = r - 1;int count = 1;for (int i = r + 1; i < r + 5; i+) if (i >= GameView.ROWS) chessStatus1 = ChessStatus.DIED;break;if (chessMapic = chessType) count+;if (count >= 5) return count; else chessStatus1 = (chessMapic = ChessType.NONE) ? ChessStatus.ALIVE: ChessStatus.DIED;t1 = i;

46、break;for (int i = r - 1; i > r - 5; i-) if (i < 0) if (chessStatus1 = ChessStatus.DIED && count < 5) return 0;chessStatus1 = ChessStatus.DIED;break;if (chessMapic = chessType) count+;if (count >= 5) return count; else if (chessStatus1 = ChessStatus.DIED) if (chessMapic != ChessT

47、ype.NONE && count < 5) return 0; else chessStatus1 = chessMapic = ChessType.NONE ? ChessStatus.ALIVE: ChessStatus.DIED;t2 = i;/ 如果两头都活,看是否还可以延伸if (chessStatus1 = ChessStatus.ALIVE) int tempCount1 = count, tempCount2 = count;boolean isAlive1 = false, isAlive2 = false;for (int j = t1 + 1; j < t1 + 5; j+) if (j >= GameView.ROWS) / chessStatus1 = ChessStatus.D

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论