上一篇博客我说到五子棋的点击事件,接下来该说如何判断的问题
5、输赢的判断
首先设置一个类来输出当游戏胜利时的提示
private void checkGameOver() {
// TODO Auto-generated method stub
boolean 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 stub
for (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 stub
int 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";
//数据的存储
@Override
protected Parcelable onSaveInstanceState() {
// TODO Auto-generated method stub
Bundle 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;
}
//数据的读取
@Override
protected void onRestoreInstanceState(Parcelable state) {
// TODO Auto-generated method stub
if (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"
/>
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int id = item.getItemId();
if (id == R.id.action_settings) {
wuziqiPanel.start();
return true;
}
return super.onOptionsItemSelected(item);
}
代码下载地址:点击打开链接