public?class?wuziqiPanel?extends?View
{
????private?int?mPanelWidth;
????private?float?mlineHeight?;
????private??int?MAX_LINE?=?10?;
????private?int?MAX_COUNT_IN_LINE?=?5;
????private?Paint?mPanit?=?new?Paint();
????private?Bitmap?mWhitePiece?;
????private?Bitmap?mBlackPiece?;
????private?float?ratioPieceOfLineHeight?=?3?*?1.0f?/?4;
????//白棋先手,當(dāng)前輪到白棋
????private?boolean?mIsWhite?=?true;
????private?List<Point>?mWhiteArray?=?new?ArrayList<>();
????private?List<Point>?mBlackArray?=?new?ArrayList<>();
????private?boolean?mIsGameOver?;
????private?boolean?mIsWhiteWinner?;
????public?wuziqiPanel(Context?context,?@Nullable?AttributeSet?attrs)?{
????????super(context,?attrs);
????????setBackgroundColor(0x44ff000);
????????init();
????}
????private?void?init()
????{
????????mPanit.setColor(0x88000000);
????????mPanit.setAntiAlias(true);
????????mPanit.setDither(true);
????????mPanit.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)
????{
????????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?(mWhiteArray.contains(p)?||?mBlackArray.contains(p))
????????????{
????????????????return?false;
????????????}
????????????if(mIsWhite)
????????????{
????????????????mWhiteArray.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);
????????drawPieces(canvas);
????????checkGameOver();
????}
????private?void?checkGameOver()
????{
???????boolean?whitewin?=??checkFiveInline(mWhiteArray);
???????boolean?blackewin?=??checkFiveInline(mBlackArray);
???????if(whitewin?||?blackewin)
???????{
???????????mIsGameOver?=?true;
???????????mIsWhiteWinner?=?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.x?;
????????????boolean?win?=?checkHorizontal(x,?y,?points);
????????????if?(win)return?true;
????????????win?=?checkVertical(x,?y,?points);
????????????if?(win)return?true;
????????????win?=?checkLeftDiagona(x,?y,?points);
????????????if?(win)return?true;
????????????win?=?checkRightDiagonal(x,?y,?points);
????????????if?(win)return?true;
????????}
????????return?false;
????}
????/**
?????*?判斷x,y位置的棋子是否橫向有相鄰的五個(gè)一致。
?????*?@param?x
?????*?@param?y
?????*?@param?points
?????*?@return
?????*/
????private?boolean?checkHorizontal(int?x,?int?y,?List<Point>?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;
????????????}
????????}
????????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;
????}
????private?boolean?checkVertical(int?x,?int?y,?List<Point>?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;
????????????}
????????}
????????if?(count?==?MAX_COUNT_IN_LINE)?return?true;
????????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;
????}
????private?boolean?checkRightDiagonal(int?x,?int?y,?List<Point>?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;
????????????}
????????}
????????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?+?i)))
????????????{
????????????????count++;
????????????}else
????????????{
????????????????break;
????????????}
????????}
????????if?(count?==?MAX_COUNT_IN_LINE)?return?true;
????????return?false;
????}
????private?boolean?checkLeftDiagona(int?x,?int?y,?List<Point>?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;
????????????}
????????}
????????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-i)))
????????????{
????????????????count++;
????????????}else
????????????{
????????????????break;
????????????}
????????}
????????if?(count?==?MAX_COUNT_IN_LINE)?return?true;
????????return?false;
????}
????private?void?drawPieces(Canvas?canvas)
????{
????????for(int?i?=?0?,?n?=?mWhiteArray.size();?i?<?n?;?i++)
????????{
????????????Point?whitePonit?=?mWhiteArray.get(i);
????????????canvas.drawBitmap(mWhitePiece,
????????????????????(whitePonit.x?+?(1?-?ratioPieceOfLineHeight)?/?2)?*?mlineHeight,
????????????????????(whitePonit.y?+?(1?-?ratioPieceOfLineHeight)?/?2)?*?mlineHeight,null);
????????}
????????for(int?i?=?0?,?n?=?mBlackArray.size();?i?<?n?;?i++)
????????{
????????????Point?blackPonit?=?mBlackArray.get(i);
????????????canvas.drawBitmap(mBlackPiece,
????????????????????(blackPonit.x?+?(1?-?ratioPieceOfLineHeight)?/?2)?*?mlineHeight,
????????????????????(blackPonit.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?startX?=?(int)?(lineHeight?/?2);
????????????int?endX?=?(int)?(w?-?lineHeight?/2);
????????????int?y?=?(int)?((0.5?+?i)?*?lineHeight);
????????????canvas.drawLine(startX?,y?,?endX?,?y?,?mPanit);
????????????canvas.drawLine(y?,?startX?,?y?,?endX?,?mPanit);
????????}
????}
2018-10-16
我忘記了定義 ?
當(dāng)游戲結(jié)束時(shí)就能在進(jìn)行了