增加了黑白棋顺序的判断
package com.example.lhd.flyfive;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private WuziqiPanel wuziqiPanel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mymain);
wuziqiPanel = (WuziqiPanel) findViewById(R.id.id_wuziqi);
findViewById(R.id.restart).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wuziqiPanel.restart();
}
});
}
}
package com.example.lhd.flyfive;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by LHD on 2016/5/2.
*/
public class WuziqiPanel extends View {
private int mPanelWidth;
private float mLineHeight;
private int MAX_LINE = 10;
private Paint mPaint = new Paint();
private Bitmap mWhitePiece;
private Bitmap mBlackPiece;
private float ratioPieceOfLineHeight = 3 * 1.0f / 4;
//白棋先手,或者当前轮到白棋
private boolean mIsWhite = true;
private ArrayList mWhiteArray = new ArrayList<>();
private ArrayList mBlackArray = new ArrayList<>();
private boolean mIsGameOver;
private boolean mIsWhiteWinner;
private int MAX_COUNT_IN_LINE = 5;
public WuziqiPanel(Context context, AttributeSet attrs) {
super(context, attrs);
// setBackgroundColor(0x44ff0000);
init();
}
private void init() {
mPaint.setColor(0x88000000);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mWhitePiece = BitmapFactory.decodeResource(getResources(),
R.drawable.stone_w2);
mBlackPiece = BitmapFactory.decodeResource(getResources(),
R.drawable.stone_b1);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = Math.min(widthSize, heightSize);
if (widthMode == MeasureSpec.UNSPECIFIED) {
width = heightSize;
} else if (heightMode == MeasureSpec.UNSPECIFIED) {
width = widthSize;
}
setMeasuredDimension(width, width);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mPanelWidth = w;
mLineHeight = mPanelWidth * 1.0f / MAX_LINE;
int pieceWidth = (int) (mLineHeight * ratioPieceOfLineHeight);
mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece, pieceWidth, pieceWidth, false);
mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece, pieceWidth, pieceWidth, false);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mIsGameOver) return false;
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
int x = (int) event.getX();
int y = (int) event.getY();
//用户点击的是一个范围而不是一个坐标点
Point p = getPoint(x, y);
if (mWhiteArray.contains(p) || mBlackArray.contains(p)) {
return false;
}
if (mIsWhite) {
mWhiteArray.add(p);
} else {
mBlackArray.add(p);
}
invalidate();
mIsWhite = !mIsWhite;
return true;
}
return true;
}
private Point getPoint(int x, int y) {
return new Point((int) (x / mLineHeight), (int) (y / mLineHeight));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBoard(canvas);
drawPieces(canvas);
checkGameOver();
}
private void checkGameOver() {
boolean whiteWin = checkFiveInLine(mWhiteArray);
boolean blackWin = checkFiveInLine(mBlackArray);
if (whiteWin || blackWin) {
mIsGameOver = true;
mIsWhiteWinner = whiteWin;
String text = mIsWhiteWinner ? "白棋胜利" : "黑棋胜利";
Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
}
if(checkfallPanel()){
mIsGameOver = true;
Toast.makeText(getContext(),"和棋",Toast.LENGTH_SHORT).show();
}
}
private boolean checkFiveInLine(List points) {
for (Point p : points) {
int x = p.x;
int y = p.y;
boolean win = checkHorizontal(x, y, points);
if (win) return true;
win = checkVetical(x, y, points);
if (win) return true;
win = checkLeft(x, y, points);
if (win) return true;
win = checkRight(x, y, points);
if (win) return true;
}
return false;
}
private boolean checkVetical(int x, int y, List points) {
int count = 1;
//检测左边
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x, y - i))) {
count++;
} else {
break;
}
}
//检测右边
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x, y + i))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE) return true;
return false;
}
/**
* 判断x,y位置的棋子是否横向有5个一致
*
* @param x
* @param y
* @param points
* @return
*/
private boolean checkHorizontal(int x, int y, List points) {
int count = 1;
//检测左边
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x - i, y))) {
count++;
} else {
break;
}
}
//检测右边
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;
}
private boolean checkfallPanel() {
if(mBlackArray.size()+mWhiteArray.size()==100){
return true;
}
return false;
}
//检测左斜线
private boolean checkLeft(int x, int y, List points) {
int count = 1;
//检测左上斜线
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x - i, y + i))) {
count++;
} else {
break;
}
}
////检测左下斜线
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x + i, y - i))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE) return true;
return false;
}
private boolean checkRight(int x, int y, List points) {
int count = 1;
//检测左边
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x - i, y - i))) {
count++;
} else {
break;
}
}
//检测右边
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x + i, y + i))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE) return true;
return false;
}
private void drawPieces(Canvas canvas) {
for (int i = 0, n = mBlackArray.size(); i < n; i++) {
Point blackPoint = mBlackArray.get(i);
canvas.drawBitmap(mBlackPiece,
(blackPoint.x + (1 - ratioPieceOfLineHeight) / 2) * mLineHeight,
(blackPoint.y + (1 - ratioPieceOfLineHeight) / 2) * mLineHeight, null);
}
for (int i = 0, n = mWhiteArray.size(); i < n; i++) {
Point whitePoint = mWhiteArray.get(i);
canvas.drawBitmap(mWhitePiece,
(whitePoint.x + (1 - ratioPieceOfLineHeight) / 2) * mLineHeight,
(whitePoint.y + (1 - ratioPieceOfLineHeight) / 2) * mLineHeight, null);
}
}
private void drawBoard(Canvas canvas) {
int w = mPanelWidth;
float lineHeight = mLineHeight;
for (int i = 0; i < MAX_LINE; i++) {
int starx = (int) (lineHeight / 2);
int endx = (int) (w - lineHeight / 2);
int y = (int) ((0.5 + i) * lineHeight);
canvas.drawLine(starx, y, endx, y, mPaint);
canvas.drawLine(y, starx, y, endx, mPaint);
}
}
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";
private static final String INSTANCE_WHO_FIRST = "instance_who_first";
public void restart(){
mWhiteArray.clear();
mBlackArray.clear();
mIsGameOver = false;
mIsWhiteWinner = false;
mIsWhite = true;
invalidate();
}
@Override
protected Parcelable onSaveInstanceState() {
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);
//判断当前的总的棋子的数两,如果是奇数则说明下一步该黑子了。反之则是白子。
if((mWhiteArray.size()+mBlackArray.size())%2==0){
//下一步该下白子了
mIsWhite = true;
}else{
//下一步该下黑子了
mIsWhite = false;
}
//存储下棋顺序
bundle.putBoolean(INSTANCE_WHO_FIRST,mIsWhite);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
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);
mIsWhite = bundle.getBoolean(INSTANCE_WHO_FIRST);
super.onRestoreInstanceState(bundle.getParcelable(INSTANCE));
return;
}
super.onRestoreInstanceState(state);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/aa"
android:orientation="vertical">
<com.example.lhd.flyfive.WuziqiPanel
android:id="@+id/id_wuziqi"
android:layout_width="match_parent"
android:layout_height="600dp"
android:layout_centerInParent="true"
/>
<Button
android:id="@+id/restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="再来一局" />
RelativeLayout>