課程
/移動(dòng)開發(fā)
/Android
/Android-五子連珠
老師,能不能發(fā)下代碼
2018-12-23
源自:Android-五子連珠 2-1
正在回答
public?class?wuziqipanel?extends?View?{ ????private?int?mPanelWidth;//格子寬度 ????private?float?mLineHeight;//格子高度 ????private?int?maxline?=?12;//棋盤格數(shù) ????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<>();//白棋坐標(biāo) ????private?ArrayList<Point>?mBlackArray?=?new?ArrayList<>();//黑棋坐標(biāo) ????//確定游戲結(jié)束和哪方勝利 ????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); ????} ????//測(cè)量尺寸的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)); ????} //檢測(cè)是否滿足五個(gè)的順利條件 ????private?void?checkOver()?{ ????????//判斷是否在line上滿足五個(gè)棋子 ????????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; ????} ????//判斷在橫向的棋子是否有連續(xù)五個(gè) ????private?boolean?checkHor(int?x,?int?y,?List<Point>?points)?{ ????????int?count?=?1;//數(shù)棋子,從一個(gè)棋子開始數(shù),1 ????????//向左數(shù) ????????for(int?i=1;i<5;i++){ ????????????if(points.contains(new?Point(x-i,y))){ ????????????????count++; ????????????}else{ ????????????????break; ????????????} ????????} ????????if(count?==?5){ ????????????return?true; ????????} ????????//向右數(shù) ????????for(int?i=1;i<5;i++){ ????????????if(points.contains(new?Point(x+i,y))){ ????????????????count++; ????????????}else{ ????????????????break; ????????????} ????????} ????????//是否夠五個(gè)棋子 ????????if(count?==?5){ ????????????return?true; ????????} ????????return?false; ????} ????private?boolean?checkVer(int?x,?int?y,?List<Point>?points)?{ ????????int?count?=?1;//數(shù)棋子,從一個(gè)棋子開始數(shù),1 ????????//向上數(shù) ????????for(int?i=1;i<5;i++){ ????????????if(points.contains(new?Point(x,y-i))){ ????????????????count++; ????????????}else{ ????????????????break; ????????????} ????????} ????????if(count?==?5){ ????????????return?true; ????????} ????????//向下數(shù) ????????for(int?i=1;i<5;i++){ ????????????if(points.contains(new?Point(x,y+i))){ ????????????????count++; ????????????}else{ ????????????????break; ????????????} ????????} ????????//是否夠五個(gè)棋子 ????????if(count?==?5){ ????????????return?true; ????????} ????????return?false; ????} ????private?boolean?checkLeftDia(int?x,?int?y,?List<Point>?points)?{ ????????int?count?=?1;//數(shù)棋子,從一個(gè)棋子開始數(shù),1 ????????//向右上數(shù) ????????for(int?i=1;i<5;i++){ ????????????if(points.contains(new?Point(x-i,y+i))){ ????????????????count++; ????????????}else{ ????????????????break; ????????????} ????????} ????????if(count?==?5){ ????????????return?true; ????????} ????????//向左下數(shù) ????????for(int?i=1;i<5;i++){ ????????????if(points.contains(new?Point(x+i,y-i))){ ????????????????count++; ????????????}else{ ????????????????break; ????????????} ????????} ????????//是否夠五個(gè)棋子 ????????if(count?==?5){ ????????????return?true; ????????} ????????return?false; ????} ????private?boolean?checkRightDia(int?x,?int?y,?List<Point>?points)?{ ????????int?count?=?1;//數(shù)棋子,從一個(gè)棋子開始數(shù),1 ????????//向左上數(shù) ????????for(int?i=1;i<5;i++){ ????????????if(points.contains(new?Point(x-i,y-i))){ ????????????????count++; ????????????}else{ ????????????????break; ????????????} ????????} ????????if(count?==?5){ ????????????return?true; ????????} ????????//向右下數(shù) ????????for(int?i=1;i<5;i++){ ????????????if(points.contains(new?Point(x+i,y+i))){ ????????????????count++; ????????????}else{ ????????????????break; ????????????} ????????} ????????//是否夠五個(gè)棋子 ????????if(count?==?5){ ????????????return?true; ????????} ????????return?false; ????} ????public?void?reStart(){ ????????mWhiteArray.clear(); ????????mBlackArray.clear(); ????????Over?=?false; ????????WhiteWin?=?false; ????????invalidate(); ????} ????//儲(chǔ)存和恢復(fù)棋盤狀態(tài) ????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); ????} }
舉報(bào)
Android游戲開發(fā)-五子連珠,本教程通過UI與邏輯實(shí)現(xiàn)雙人對(duì)戰(zhàn)
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2019-06-27