五子棋-人人对战(二)
来源:互联网 发布:域名注册查询 百度 编辑:程序博客网 时间:2024/05/17 00:55
上一篇博客我说到五子棋的点击事件,接下来该说如何判断的问题
5、输赢的判断
首先设置一个类来输出当游戏胜利时的提示
private void checkGameOver() {// TODO Auto-generated method stubboolean whiteWin = checkFiveInLine(mWhiteArray);boolean blackWin = checkFiveInLine(mBlackArray);if (blackWin || whiteWin) {mIsGameOver = true;mIsWhiteWinner = whiteWin;String text = mIsWhiteWinner ? "白棋胜利" : "黑棋胜利";Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();}}并且在onTouchEvent类中要设置当胜利时,不能再点击棋子
if (mIsGameOver) {// 如果胜利,则不能下子return false;}然后创建类checkFiveInLine来判断是否五子连珠
private boolean checkFiveInLine(List<Point> points) {// TODO Auto-generated method stubfor (Point point : points) {int x = point.x;// x,y坐标int y = point.y;boolean win = checkHorizontal(x, y, points);if (win) {return true;}win = checkVertical(x, y, points);if (win) {return true;}win = checkLeftDiagonal(x, y, points);if (win) {return true;}win = checkRightDiagonal(x, y, points);if (win) {return true;}}return false;}因为胜利共有四种方法,即在四个方向上连成五个棋子,所以我们要设置四个类来判断(这里我写其中一个方向的,其他方向都差不多):
private int MAX_COUNT_IN_LINE = 5;
// 横向五子连珠时private boolean checkHorizontal(int x, int y, List<Point> points) {// TODO Auto-generated method stubint count = 1;// 左边for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {if (points.contains(new Point(x - i, y))) {count++;} else {break;}}if (count == MAX_COUNT_IN_LINE)return true;// 右边for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {if (points.contains(new Point(x + i, y))) {count++;} else {break;}}if (count == MAX_COUNT_IN_LINE)return true;return false;}其实,做到这一步就可以了,当我们可以给他加一个数据的储存,防止在屏幕翻转是view的重置将数据丢失
6、数据的存储和读取
private static final String INSTANCE = "instance";private static final String INSTANCE_GAME_OVER = "instance_game_over";private static final String INSTANCE_WHITE_ARRAY = "instance_white_array";private static final String INSTANCE_BLACK_ARRAY = "instance_black_array";//数据的存储@Overrideprotected Parcelable onSaveInstanceState() {// TODO Auto-generated method stubBundle bundle = new Bundle();bundle.putParcelable(INSTANCE, super.onSaveInstanceState());bundle.putBoolean(INSTANCE_GAME_OVER, mIsGameOver);bundle.putParcelableArrayList(INSTANCE_WHITE_ARRAY, mWhiteArray);bundle.putParcelableArrayList(INSTANCE_BLACK_ARRAY, mBlackArray);return bundle;}//数据的读取@Overrideprotected void onRestoreInstanceState(Parcelable state) {// TODO Auto-generated method stubif (state instanceof Bundle) {Bundle bundle = (Bundle) state;mIsGameOver=bundle.getBoolean(INSTANCE_GAME_OVER);mWhiteArray=bundle.getParcelableArrayList(INSTANCE_WHITE_ARRAY);mBlackArray=bundle.getParcelableArrayList(INSTANCE_BLACK_ARRAY);super.onRestoreInstanceState(bundle.getParcelable(INSTANCE));return ;}7、设置再来一局
注意:用eclipse的可能在layout中看不到menu,这是可能是因为用的Android版本低。你把apk放到手机上就有了,不必去修改代码,强制吧menu显示出来。
设置一个start类,在其中把黑棋和白棋的数据清理
public void start(){mWhiteArray.clear();mBlackArray.clear();mIsGameOver=false;mIsWhiteWinner=false;invalidate();}在main_activity中声明
wuziqiPanel=(WuziqiPanel) findViewById(R.id.wuziqi);在onOptionsItemSelected中编写,并且记得layout中的view要有ID,不然start不起作用
<com.example.wuziqi.WuziqiPanel android:id="@+id/wuziqi" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" />
@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// TODO Auto-generated method stubint id = item.getItemId();if (id == R.id.action_settings) {wuziqiPanel.start();return true;}return super.onOptionsItemSelected(item);}
代码下载地址:点击打开链接
阅读全文
1 0
- 五子棋-人人对战(二)
- 五子棋 人人对战
- 简单的五子棋(人人对战)
- 五子棋-人人对战(一)
- 五子棋人人对战实现
- 人人对战版五子棋
- 五子棋_人人对战_小结
- android开发五子棋人人对战
- 五子棋JAVA源码__支持人机、人人对战(转)
- 五子棋_Java_控制台窗口_人人对战
- Android实现五子棋游戏(二) 人机对战实现
- 基于qt的五子棋 人人对战 人机对战 程序与程序对战
- JavaScript编写人机对战五子棋(二)
- NodeJS+html5+css3 带人机对战和人人对战的 五子棋
- 期末课程设计之java实现五子棋的人机和人人对战
- Android小游戏——简单易懂单机人人对战五子棋源码详解
- MFC设计局域网对战五子棋游戏(二)实现GDI+自绘按钮
- 人机对战五子棋
- Android UI卡顿优化的套路
- 【Spring 核心】(1):IOC容器的实现
- IAR中eww、ewp、ewd···等各文件的含义和用途
- A
- 爬取糗事百科数据
- 五子棋-人人对战(二)
- C/C++ const总结
- redis集群
- ubuntu17.04下mysql5.7.18源码安装
- 【DP UESTC 594】我要长高
- 彻底找到 Tomcat 启动速度慢的元凶
- windows下emacs latex编译命令的实现
- pdo批量插入数据
- 变化 变形