这是我的代码,感觉额没有错,但运行不对,逻辑有错误,不到5个子就能获胜
package edu.xjnu.wuzq;
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.Build;
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;
public class WuziqiPanel extends View {
private int mPanelwidth;
private float mLineHeight;
private int MAX_LINE = 10;
private int MAX_WIN = 5;
private Paint mPaint = new Paint();
private Bitmap mWhitepiece;
private Bitmap mBlackpiece;
private float retiopiece = 3*1.0f/4;
private boolean mIsWhite = true;
private ArrayList<Point> mWiteArray = new ArrayList<>();
private ArrayList<Point> mBlackArray = new ArrayList<>();
private boolean mIsgameover;
private boolean mIsWhiteWinner;
public WuziqiPanel(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(0x44ff0000);
init();
}
public void init(){
mPaint.setColor(0x88000000);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mWhitepiece = BitmapFactory.decodeResource(getResources() ,R.drawable.white);
mBlackpiece = BitmapFactory.decodeResource(getResources(), R.drawable.black);
}
@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 *1.0f/MAX_LINE);
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 = getValidPoint(x , y);
if(mWiteArray.contains(p) || mBlackArray.contains(p)){
return false;
}
if(mIsWhite){
mWiteArray.add(p);
}else {
mBlackArray.add(p);
}
invalidate();
mIsWhite = !mIsWhite;
}
return true;
}
private Point getValidPoint(int x, int y) {
return new Point((int) (x / mLineHeight), (int) (y / mLineHeight));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBoard(canvas);
drawPiece(canvas);
checkgameover();
}
private void checkgameover() {
boolean whitewin = checkFiveInLine(mWiteArray);
boolean blackwin = checkFiveInLine(mBlackArray);
if(whitewin || blackwin){
mIsgameover = true;
mIsWhite = whitewin;
String text = mIsWhiteWinner ? "白棋获胜":"黑棋获胜";
Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
}
}
private boolean checkFiveInLine(List<Point> points) {
for(Point p:points){
int x=p.x;
int y=p.y;
boolean win = checkHorizontal(x ,y ,points);
if(win) return true;
win = checkVerticel(x, y, points);
if(win) return true;
win = checkRightl(x, y, points);
if(win) return true;
}
return false;
}
private boolean checkHorizontal(int x, int y, List<Point> points) {
int count = 1;
for(int i = 1 ; i<MAX_WIN; i++){
if(points.contains(new Point(x-i ,y))){
count++;
}else {
break;
}
}
if(count == MAX_WIN) return true;
for(int i = 1 ; i<MAX_WIN; i++){
if(points.contains(new Point(x+i ,y))){
count++;
}else {
break;
}
}
if(count == MAX_WIN) return true;
return false;
}
private boolean checkVerticel(int x, int y, List<Point> points) {
int count = 1;
for(int i = 1 ; i<MAX_WIN; i++){
if(points.contains(new Point(x ,y-i))){
count++;
}else {
break;
}
}
if(count == MAX_WIN) return true;
for(int i = 1 ; i<MAX_WIN; i++){
if(points.contains(new Point(x ,y+1))){
count++;
}else {
break;
}
}
if(count == MAX_WIN) return true;
return false;
}
private boolean checkRightl(int x, int y, List<Point> points) {
int count = 1;
for(int i = 1 ; i<MAX_WIN; i++){
if(points.contains(new Point(x -i,y-i))){
count++;
}else {
break;
}
}
if(count == MAX_WIN) return true;
for(int i = 1 ; i<MAX_WIN; i++){
if(points.contains(new Point(x +1,y+1))){
count++;
}else {
break;
}
}
if(count == MAX_WIN) return true;
return false;
}
private void drawPiece(Canvas canvas){
for(int i = 0, n = mWiteArray.size(); i<n ;i++){
Point whitePoint = mWiteArray.get(i);
canvas.drawBitmap(mWhitepiece, (whitePoint.x +(1-retiopiece)/2)*mLineHeight,(whitePoint.y +(1-retiopiece)/2)*mLineHeight,null);
}
for(int i = 0, n = mBlackArray.size(); i<n ;i++){
Point blackPoint = mBlackArray.get(i);
canvas.drawBitmap(mBlackpiece, (blackPoint.x +(1-retiopiece)/2)*mLineHeight,(blackPoint.y +(1-retiopiece)/2)*mLineHeight,null);
}
}
private void drawBoard(Canvas canvas) {
int w = mPanelwidth;
float LineHeight = mLineHeight;
for (int i = 0; i<MAX_LINE; i++){
int startX = (int) (LineHeight/2);
int endX = (int) (w-LineHeight/2);
int y = (int) ((0.5 + i)*LineHeight);
canvas.drawLine(startX,y,endX,y,mPaint);
canvas.drawLine(y,startX,y,endX,mPaint);
}
}
public void start(){
mWiteArray.clear();
mBlackArray.clear();
mIsWhiteWinner = false;
mIsgameover = false;
invalidate();
}
private static final String INSTANCE = "instance";
private static final String INSTANCE_GAME_OVER = "instance";
private static final String INSTANCE_WHITE_ARRAY = "instance";
private static final String INSTANCE_BLACK_ARRAY = "instance";
@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable(INSTANCE,super.onSaveInstanceState());
bundle.putBoolean(INSTANCE_GAME_OVER, mIsgameover);
bundle.putParcelableArrayList(INSTANCE_WHITE_ARRAY, mWiteArray);
bundle.putParcelableArrayList(INSTANCE_BLACK_ARRAY,mBlackArray);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if(state instanceof Bundle){
Bundle bundle = (Bundle) state;
mIsgameover = bundle.getBoolean(INSTANCE_GAME_OVER);
mWiteArray = bundle.getParcelableArrayList(INSTANCE_WHITE_ARRAY);
mBlackArray = bundle.getParcelableArrayList(INSTANCE_BLACK_ARRAY);
super.onRestoreInstanceState(bundle.getParcelable(INSTANCE));
return;
}
super.onRestoreInstanceState(state);
}
}