android智能手机五子棋游戏设计_第1页
android智能手机五子棋游戏设计_第2页
android智能手机五子棋游戏设计_第3页
android智能手机五子棋游戏设计_第4页
android智能手机五子棋游戏设计_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

1、基于android的五子棋开发游戏一、开发背景: 在手机游戏的早期,大多数开发者都是想要玩转设备、学习技巧和快速攻关的狂热分子。但是,随着更多的开发者加入这个行业,手机游戏已经进化成涉及到专业设计师、构造师、开发者、产品经销商、律师、和艺术家在内的主导产品。这是行业走向成熟的必然趋势。尽管在某人的业余时间内要开发和推销简单的游戏更加困难,但是更多的专业商店将为许多人提供更多的就业机会。   从手机软件的平台的种类,游戏玩家的心理,未来移动通信服务终端的发展,3G手机网络对手机软件游戏的影响,还有从J2ME手机游戏开发平台介绍的手机游戏的趋势和未来这种游戏的运营模式

2、,这几方面的内容来看,手机游戏是未来游戏发展的趋势,其移动性、互连性,必定会带来网络游戏的风暴,这就使我们对这一个新兴的领域有了一定的概念,对指导我们建立一个手机软件开发培训的知识结构有一定的帮助,也是我们的一个发展契机。 五子棋是起源于中国古代的传统黑白棋种之一。现代五子棋日文称之为连珠,英译为Renju,英文称之为Gobang或FIR,亦有连五子、五子连、串珠、五目、五目碰、五格等多种称谓。 五子棋起源于古代中国,发展于日本,风靡于欧洲。对于它与围棋的关系有两种说法,一说早于围棋,早在 “ 尧造围棋 ” 之前,民间就已有五子棋游戏;一说源于

3、围棋,是围棋发展的一个分支。在中国的文化里,倍受人们的青睐。本世纪初五子棋传入欧洲并迅速风靡全欧。通过一系列的变化,使五子棋这一简单的游戏复杂化、规范化,而最终成为今天的职业连珠五子棋,同时也成为一种国际比赛棋。 2、 开发环境: 采用eclipse开发工具,在windows 7下进行,基于安卓4.1.1操作系统。 环境搭建: (1)JDK安装 (2)Eclipse安装 (3)Android SDK安装 (4)ADT安装创建AVD三、概述: 程序的基本功能。1) 通过触屏点击欢迎界面上的“关于”可显出“我的五子棋”。2) 通过触屏点击“新游戏”进入游戏主界面。3)显示一个10*10的

4、五子棋棋盘。4)在棋盘上面对应位置通过触屏点击下子,棋盘将在相应的位置上显示棋子,并且棋子在纵横交点上。5)如果黑方(白方)连成五个棋子,可以自动判断游戏是否结束,判断胜负,是否黑方或者白方已经胜出,并且通过提示信息黑方(白方)胜利来告知玩家。6)当玩家还未分出白方和黑方胜利,想下次接着玩,点击返回,下次若需进入上一轮未完成游戏主界面可触屏点击欢迎界面上的继续,可进行上一轮未完成的游戏。7)触屏点击“退出”则退出游戏。四、设计思路:1)需求分析: 手机屏幕尺寸分为物理尺寸和显示分辩率两个概念。物理尺寸是指屏幕的实际大小。大的屏幕同时,要配备搞得分辨率,也就是在这个尺寸下可以显示多少个香酥,显示

5、的像素越多,可以表现的余地自然就越大。现在手机行业发展迅速,不同大小屏幕的手机迅速发展,鉴于不同手机屏幕尺寸不同,在设计游戏的时候必须满足:软件的界面简洁明了,操作简单容易。游戏是需要快速反应的,手机处理器的频率直接要求需要使用简单使用的智能算法。在设计本游戏时考虑到用户需要的是一个操作简便界面简单的游戏软件。同时提供上一轮未完成的游戏的继续。此外,通过对软件功能的分析,根据玩家的需求,得出还有如下需求信息:(1) 可以重新开始新游戏;(2) 具有提示哪一方胜利的功能;(3) 具有退出游戏的功能。2)程序设计思想: 设计主要是设计一个能够在android手机上运行的五子棋游戏。根据一般玩手机游

6、戏的流程,玩家一般是先进入一个欢迎界面,进行选择操作后在进入游戏的主界面进行游戏。本游戏软件也将遵循该流程,玩家打开游戏软件后讲先进入欢迎界面,选择开始“新游戏”,然后进入游戏的主界面开始游戏。进入游戏的主界面后,玩家可进行游戏,游戏过程中,若黑方(白方)胜利,棋盘上会显现对话框“黑方(白方)胜利”,玩家点击“确认”,进行下一轮新游戏。若玩家想接上一轮未完成游戏,可在欢迎界面点击“继续”,进入上一轮未分胜负的的游戏界面。3) 程序总体设计: 根据程序设计思想,得出程序设计模型为: 欢迎界面 退出 关于 继续新游戏主界面4) 程序功能模块及流程: 根据需求分析和程序设计思想,可以将此手机五子棋游

7、戏分为以下几个功能模块:a)游戏界面初始化模块:装载界面图片,画出棋盘,清空棋盘,清空游戏落子功能。b)主循环控制模块:负责下棋顺序,当轮到某方下子是,负责将程序流程转到相应的米快中,主要担当一个调度者的角色。c)玩家落子模块:即用户在指定落子区域单击后,程序会计算该子的坐标,并且在相应位置画出相应的棋子。d)胜负判断模块:根据预先设定的规则,判断游戏胜负。e)继续模块:在上一轮未结束游戏的基础上,继续游戏。5) 程序主要流程我的五子棋关于欢迎界面退出显示开始游戏 进行游戏继续黑白方未分胜负黑(白)方胜利 返回退至欢迎界面 继续进行上一轮游戏五、系统实现: 在程序设计过程后,能够成功的运行出游

8、戏,但是在设计继续程序时,遇到了写问题,最后通过查阅资料与向他人咨询得以解决程序相关问题。但是有时游戏会崩溃仍未能够解决,在之后要努力解决这一问题。6、 软件测试:序号输入及操作说明期望结果评价标准备注1点击程序列表进入程序进入欢迎界面测试结果与期望结果一样2进入欢迎界面点击新游戏进入游戏主界面测试结果与期望结果一样3进入游戏主界面进行触屏下棋可以游戏测试结果与期望结果一样4进入欢迎界面,点击继续可以继续上一轮未完成游戏测试结果与期望结果一样软件和手机运行截图: 欢迎界面 游戏主界面 “关于”界面 “继续”界面7、 总结: 在本游戏的编写过程中,我遇到了许多的问题。首先要自己先熟悉一下Andr

9、oid的游戏编程,在这方面我遇到了很大的难题。还有就是对五子棋游戏的算法不是很清楚。在编码实现的时候,会出现一些开始完全意料不到的问题,也许这些问题当中,有的是老师讲过的,不过我想大部分还是老师没有讲过,这个时候就是体现出自己解决问题的能力的时候了。在遇到问题时要学会如何去分析错误,再是学会如何去解决掉这个问题。在遇到问题是,不要有害怕困难的情绪,解决问题也是一个不断学习进步的过程,遇到的问题越多,学到的东西也就越多。因此在遇到困难之时,不要轻言放弃,要努力向前解决问题。八、附录:主要代码:Control.javapackage com.example.fivechess;import and

10、roid.R.integer;public class Control private static Control instance = null;private enum DIRECTION SOUTH, NORTH, EAST, WEST, SOUTH_EAST, SOUTH_WEST, NORTH_EAST, NORTH_WESTprivate Control() public static Control getInstance() if (instance = null)instance = new Control();return instance;public boolean

11、isValid(int row, int col) Model model = Model.getInstance();if (model.getChessAt(row, col) = 0)return true;elsereturn false;public void putChess(int row, int col) Model model = Model.getInstance();MyView view = MyView.getInstance();if (isValid(row, col) model.setChessAt(row, col, model.currentPlayer

12、);model.win_flage = isWin(row, col);if (model.win_flage) view.showWin(model.currentPlayer); else model.switchPlayer();view.invalidate();public boolean isWin(int row, int col) int e, w, s, n, ne, nw, se, sw;Model model = Model.getInstance();int color = model.getChessAt(row, col);e = getCountDirection

13、(row, col, color, DIRECTION.EAST);w = getCountDirection(row, col, color, DIRECTION.WEST);n = getCountDirection(row, col, color, DIRECTION.NORTH);s = getCountDirection(row, col, color, DIRECTION.SOUTH);ne = getCountDirection(row, col, color, DIRECTION.NORTH_EAST);se = getCountDirection(row, col, colo

14、r, DIRECTION.SOUTH_EAST);nw = getCountDirection(row, col, color, DIRECTION.NORTH_WEST);sw = getCountDirection(row, col, color, DIRECTION.SOUTH_WEST);if (e + w + 1 >= 5)return true;else if (s + n + 1 >= 5)return true;else if (ne + sw + 1 >= 5)return true;else if (nw + se + 1 >= 5)return t

15、rue;elsereturn false;public int getCountDirection(int row, int col, int color,DIRECTION direction) int nCount = 0;Model model = Model.getInstance();for (int n = 1; n <= 4; n+) int currentRow = getNextRow(row, direction, n);int currentCol = getNextCol(col, direction, n);if (currentRow < 0 | cur

16、rentCol < 0)break;if (model.getChessAt(currentRow, currentCol) = color)nCount+;elsebreak;return nCount;private int getNextRow(int row, DIRECTION direction, int step) int nextRow;switch (direction) case SOUTH:case SOUTH_EAST:case SOUTH_WEST:nextRow = row + step;break;case NORTH:case NORTH_EAST:cas

17、e NORTH_WEST:nextRow = row - step;break;default:nextRow = row;break;if (nextRow < 0 | nextRow >= Model.N)nextRow = -1;return nextRow;private int getNextCol(int col, DIRECTION direction, int step) int nextCol;switch (direction) case EAST:case SOUTH_EAST:case NORTH_EAST:nextCol = col + step;brea

18、k;case WEST:case SOUTH_WEST:case NORTH_WEST:nextCol = col - step;break;default:nextCol = col;break;if (nextCol < 0 | nextCol >= Model.N)nextCol = -1;return nextCol;GameActivity.javapackage com.example.fivechess;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.

19、FileOutputStream;import java.io.IOException;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class GameActivity extends Activity Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.activity_game);Ov

20、erridepublic boolean onCreateOptionsMenu(Menu menu) / Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.game, menu);return true;Overrideprotected void onDestroy() / TODO Auto-generated method stubSystem.out.println("-GameActivity: onDestroy-&q

21、uot;);try FileOutputStream fileOutputStream = this.openFileOutput("chess.data", MODE_WORLD_WRITEABLE);Model model = Model.getInstance();byte outBuffer = new bytemodel.N * model.N;for(int i = 0; i < model.N; i+)for(int j = 0; j < model.N; j+)if(model.win_flage)outBufferi*model.N + j =

22、 0;elseoutBufferi*model.N + j = Integer.valueOf(model.arrayij).byteValue();fileOutputStream.write(outBuffer);fileOutputStream.close(); catch (FileNotFoundException e) / TODO Auto-generated catch blockße.printStackTrace(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(

23、);super.onDestroy();MainActivity.javapackage com.example.fivechess;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button

24、;public class MainActivity extends Activity implements OnClickListener Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btnNewGame = (Button) findViewById(R.id.btnNewGame);Button btnAbout = (Button) findViewBy

25、Id(R.id.btnAbout);Button btnQuit = (Button) findViewById(R.id.btnQuit);Button btnContinue = (Button)findViewById(R.id.btnContinue);btnNewGame.setOnClickListener(this);btnAbout.setOnClickListener(this);btnQuit.setOnClickListener(this);btnContinue.setOnClickListener(this);Overridepublic boolean onCrea

26、teOptionsMenu(Menu menu) / Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;Overridepublic void onClick(View v) switch (v.getId() case R.id.btnNewGame:startGame();break;case R.id.btnAbout:new AlertDialog.Builder(this).setTi

27、tle("关于").setMessage("我的五子棋").setPositiveButton("确定", null).show();break;case R.id.btnContinue:continueGame();break;case R.id.btnQuit:finish();break;private void startGame() Model model = Model.getInstance();model.newGame();Intent intent = new Intent(this, GameActivity.

28、class);startActivity(intent);private void continueGame() Model model = Model.getInstance();model.continueGame();Intent intent = new Intent(this, GameActivity.class);startActivity(intent);Model.javapackage com.example.fivechess;import java.io.FileInputStream;import java.io.FileNotFoundException;impor

29、t java.io.IOException;public class Model public final static int BLACK_CHESS = 1;public final static int WHITE_CHESS = 2;public final static int N = 11;public int array = new intNN;public int currentPlayer = BLACK_CHESS;public static boolean win_flage = false;private static Model instance = null;pri

30、vate Model() public static Model getInstance() if (instance = null)instance = new Model();return instance;public void newGame() for (int i = 0; i < N; i+) for (int j = 0; j < N; j+) arrayij = 0;currentPlayer = BLACK_CHESS;public void continueGame() int chessCount = 0;MyView myView = MyView.get

31、Instance();FileInputStream fileInputStream;try fileInputStream = myView.context.openFileInput("chess.data");byte inBuffer = new byteN * N;fileInputStream.read(inBuffer);for(int i = 0; i < N; i+)for(int j = 0; j < N; j+)arrayij = Byte.valueOf(inBufferi*N + j).intValue();if(arrayij !=

32、0)chessCount+; catch (FileNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();myView.continueChess();if(chessCount % 2 = 1)currentPlayer = WHITE_CHESS;elsecurrentPlayer = BLACK_CHESS;public int getChess

33、At(int row, int col) return arrayrowcol;public void setChessAt(int row, int col, int color) arrayrowcol = color;public void switchPlayer() currentPlayer = currentPlayer = BLACK_CHESS ? WHITE_CHESS: BLACK_CHESS;MyView.javapackage com.example.fivechess;import android.app.Activity;import android.app.Al

34、ertDialog;import android.content.Context;import android.content.DialogInterface;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Style;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;pub

35、lic class MyView extends View int chessRadius;Paint paintBack = null;Paint paintGrid = null;Paint paintWhiteChess = null;Paint paintBlackChess = null;int boardWidth;float boardPadding;float gridWidth;private static MyView instance = null;public Activity context;public MyView(Context context, Attribu

36、teSet attrs) super(context, attrs);this.context = (Activity) context;paintBack = new Paint();paintBack.setColor(Color.rgb(217, 168, 0);paintBlackChess = new Paint();paintBlackChess.setColor(Color.BLACK);paintBlackChess.setStyle(Style.FILL_AND_STROKE);paintWhiteChess = new Paint();paintWhiteChess.set

37、Color(Color.WHITE);paintWhiteChess.setStyle(Style.FILL_AND_STROKE);paintGrid = new Paint();paintGrid.setColor(Color.BLACK);instance = this;public static MyView getInstance() return instance;Overrideprotected void onDraw(Canvas canvas) canvas.drawRect(0, 0, boardWidth, boardWidth, paintBack);drawGrid

38、s(canvas);drawAllChess(canvas);super.onDraw(canvas);protected void drawGrids(Canvas canvas) for (int i = 0; i < Model.N; i+) canvas.drawLine(getX(i), getY(0), getX(i), getY(Model.N - 1),paintGrid);canvas.drawLine(getX(0), getY(i), getX(Model.N - 1), getY(i),paintGrid);protected void drawAllChess(

39、Canvas canvas) Model model = Model.getInstance();for (int i = 0; i < Model.N; i+) for (int j = 0; j < Model.N; j+) drawChess(canvas, i, j, model.arrayij);protected void drawChess(Canvas canvas, int row, int col, int color) Paint p = null;if (color = Model.WHITE_CHESS)p = paintWhiteChess;else i

40、f (color = Model.BLACK_CHESS)p = paintBlackChess;elsereturn;float x = getX(col);float y = getY(row);canvas.drawCircle(x, y, chessRadius, p);protected float getX(int col) return col * gridWidth + boardPadding;protected float getY(int row) return row * gridWidth + boardPadding;protected int getRow(flo

41、at y) return (int) (y - boardPadding + gridWidth / 2) / gridWidth);protected int getCol(float x) return (int) (x - boardPadding + gridWidth / 2) / gridWidth);Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) boardWidth = w > h ? h : w;gridWidth = boardWidth / (Model.N + 1);ch

42、essRadius = (int) (gridWidth * 0.3);boardPadding = gridWidth;super.onSizeChanged(w, h, oldw, oldh);Overridepublic boolean onTouchEvent(MotionEvent event) int row = getRow(event.getY();int col = getCol(event.getX();Control control = Control.getInstance();if (control.isValid(row, col)control.putChess(

43、row, col);return super.onTouchEvent(event);public void continueChess() Model model = Model.getInstance();for(int i = 0; i < model.N; i+)for(int j = 0; j < model.N; j+)if(model.arrayii != 0)model.setChessAt(i, j, model.arrayij);public void showWin(int color) AlertDialog.Builder dlg = new AlertD

44、ialog.Builder(context);dlg.setTitle("胜利");String message = ""if (color = Model.BLACK_CHESS)message = "黑方胜利"else if (color = Model.WHITE_CHESS)message = "白方胜利"dlg.setMessage(message);dlg.setPositiveButton("确定", new DialogInterface.OnClickListener() Overridepublic void onClick(DialogInterface arg0, int arg1) context.finish(););dlg.show();activity_main.xml<LinearLayout xmlns:android=" xmlns:tools=" android:id="+id/LinearLayout1" android:layout_wid

温馨提示

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

评论

0/150

提交评论