老师,能不能发下源代码
老师,能不能发下代码
老师,能不能发下代码
2018-12-23
public class wuziqipanel extends View { private int mPanelWidth;//格子宽度 private float mLineHeight;//格子高度 private int maxline = 12;//棋盘格数 private Paint mPaint = new Paint(); private Bitmap mWhite;//白棋 private Bitmap mBlack;//黑棋 private float qiziHeight = 4*1.0f/5;//棋子高度 private boolean isWhite = true;//白棋回合 private ArrayList<Point> mWhiteArray = new ArrayList<>();//白棋坐标 private ArrayList<Point> mBlackArray = new ArrayList<>();//黑棋坐标 //确定游戏结束和哪方胜利 private boolean Over; private boolean WhiteWin; public wuziqipanel(Context context, AttributeSet attrs){ super(context,attrs); setBackgroundColor(0x50C0C0C0); init(); } private void init() { mPaint.setColor(0x88000000); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setStyle(Paint.Style.STROKE); mWhite = BitmapFactory.decodeResource(getResources(),R.drawable.white); mBlack = BitmapFactory.decodeResource(getResources(),R.drawable.black); } //测量尺寸的onMeasure类 protected void onMeasure(int widthMeasure,int heightMeasure){ int widthSize = MeasureSpec.getSize(widthMeasure); int widthMode = MeasureSpec.getMode(widthMeasure); int heightSize = MeasureSpec.getSize(heightMeasure); int heightMode = MeasureSpec.getMode(heightMeasure); 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 / maxline; int qiziWidth = (int) (mLineHeight*qiziHeight); mWhite = Bitmap.createScaledBitmap(mWhite,qiziWidth,qiziWidth,false); mBlack = Bitmap.createScaledBitmap(mBlack,qiziWidth,qiziWidth,false); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); drawBoard(canvas); drawqizi(canvas); checkOver(); } //画棋盘线 private void drawBoard(Canvas canvas) { int w = mPanelWidth; float lineHeight = mLineHeight; for(int i =0;i<maxline;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); } for(int i =0;i<maxline;i++){ int startY = (int) (lineHeight/2); int endY = (int) (w-lineHeight/2); int x = (int) ((0.5+i)*lineHeight); canvas.drawLine(x,startY,x,endY,mPaint); } } //画棋子 private void drawqizi(Canvas canvas) { int n = mWhiteArray.size(); int m = mBlackArray.size(); for(int i=0;i<n;i++){ Point WhitePoint = mWhiteArray.get(i); canvas.drawBitmap(mWhite, (WhitePoint.x+(1-qiziHeight)/2)*mLineHeight, (WhitePoint.y+(1-qiziHeight)/2)*mLineHeight,null); } for(int i=0;i<m;i++){ Point BlackPoint = mBlackArray.get(i); canvas.drawBitmap(mBlack, (BlackPoint.x+(1-qiziHeight)/2)*mLineHeight, (BlackPoint.y+(1-qiziHeight)/2)*mLineHeight,null); } } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if(Over==true) return false; 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(isWhite){ mWhiteArray.add(p); }else { mBlackArray.add(p); } invalidate(); isWhite = !isWhite; return true; } return true; } private Point getPoint(int x,int y) { //返回下的棋子的所在位置 return new Point((int)(x/mLineHeight),(int)(y/mLineHeight)); } //检测是否满足五个的顺利条件 private void checkOver() { //判断是否在line上满足五个棋子 boolean whitewin = checkLine(mWhiteArray); boolean blackwin = checkLine(mBlackArray); if(whitewin || blackwin){ Over = true; WhiteWin = whitewin; String text = WhiteWin?"白棋胜!":"黑棋胜!"; Toast.makeText(getContext(),text,Toast.LENGTH_SHORT).show(); } } private boolean checkLine(List<Point> points) { for(Point p:points){ int x = p.x; int y = p.y; boolean win = checkHor(x,y,points); if(win) return true; win = checkVer(x,y,points); if(win) return true; win = checkLeftDia(x,y,points); if(win) return true; win = checkRightDia(x,y,points); if(win) return true; } return false; } //判断在横向的棋子是否有连续五个 private boolean checkHor(int x, int y, List<Point> points) { int count = 1;//数棋子,从一个棋子开始数,1 //向左数 for(int i=1;i<5;i++){ if(points.contains(new Point(x-i,y))){ count++; }else{ break; } } if(count == 5){ return true; } //向右数 for(int i=1;i<5;i++){ if(points.contains(new Point(x+i,y))){ count++; }else{ break; } } //是否够五个棋子 if(count == 5){ return true; } return false; } private boolean checkVer(int x, int y, List<Point> points) { int count = 1;//数棋子,从一个棋子开始数,1 //向上数 for(int i=1;i<5;i++){ if(points.contains(new Point(x,y-i))){ count++; }else{ break; } } if(count == 5){ return true; } //向下数 for(int i=1;i<5;i++){ if(points.contains(new Point(x,y+i))){ count++; }else{ break; } } //是否够五个棋子 if(count == 5){ return true; } return false; } private boolean checkLeftDia(int x, int y, List<Point> points) { int count = 1;//数棋子,从一个棋子开始数,1 //向右上数 for(int i=1;i<5;i++){ if(points.contains(new Point(x-i,y+i))){ count++; }else{ break; } } if(count == 5){ return true; } //向左下数 for(int i=1;i<5;i++){ if(points.contains(new Point(x+i,y-i))){ count++; }else{ break; } } //是否够五个棋子 if(count == 5){ return true; } return false; } private boolean checkRightDia(int x, int y, List<Point> points) { int count = 1;//数棋子,从一个棋子开始数,1 //向左上数 for(int i=1;i<5;i++){ if(points.contains(new Point(x-i,y-i))){ count++; }else{ break; } } if(count == 5){ return true; } //向右下数 for(int i=1;i<5;i++){ if(points.contains(new Point(x+i,y+i))){ count++; }else{ break; } } //是否够五个棋子 if(count == 5){ return true; } return false; } public void reStart(){ mWhiteArray.clear(); mBlackArray.clear(); Over = false; WhiteWin = false; invalidate(); } //储存和恢复棋盘状态 private static final String INSTANCE = "instance"; private static final String INSTANCE_OVER = "instance_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(){ Bundle bundle = new Bundle(); bundle.putParcelable(INSTANCE,super.onSaveInstanceState()); bundle.putBoolean(INSTANCE_OVER,Over); bundle.putParcelableArrayList(INSTANCE_WHITE_ARRAY,mWhiteArray); bundle.putParcelableArrayList(INSTANCE_BLACK_ARRAY,mBlackArray); return bundle; } @Override protected void onRestoreInstanceState(Parcelable state) { if(state instanceof Bundle){ Bundle bundle = (Bundle) state; Over = bundle.getBoolean(INSTANCE_OVER); mWhiteArray = bundle.getParcelableArrayList(INSTANCE_WHITE_ARRAY); mBlackArray = bundle.getParcelableArrayList(INSTANCE_BLACK_ARRAY); super.onRestoreInstanceState(bundle.getParcelable(INSTANCE)); return; } super.onRestoreInstanceState(state); } }
举报