Android Studio 五子棋(不敢和大佬做的比)

实现效果
Android Studio 五子棋(不敢和大佬做的比)_第1张图片
代码段
IDE:AS 语言:JAVA
package com.example.myapplication;

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 androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class WuziqiPanel extends View
{
private int mPanelWidth;//格子宽度
private float mLineHeight;//格子高度
private int MAX_LINE = 12;//棋盘格数
private float ratioPieceOfLineHight=3*1.0f/4;

private Paint mPaint=new Paint();
private Bitmap mWhitePiece;
private Bitmap mBlackPiece;
//白棋先手或者当前轮到白棋
private boolean mIsWhite= true;
private ArrayList mWhiteArray= new ArrayList<>();
private ArrayListmBlackArray= new ArrayList<>();
private boolean mIsGameOver;  //游戏是不是结束
private boolean mIsWhiteWinner; //true白棋胜利,false黑棋胜利
private int MAX_COUNT_IN_LINE=5; //五子棋

public WuziqiPanel(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    //setBackgroundColor(0x44ff0000);
    init();
}

private void init() {
    mPaint.setColor(0x8B008B00);
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setStyle(Paint.Style.STROKE);
    mBlackPiece = BitmapFactory.decodeResource(getResources(),R.drawable.stone_b1);
    mWhitePiece = BitmapFactory.decodeResource(getResources(),R.drawable.stone_w2);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //测量尺寸的onMeasure
    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*ratioPieceOfLineHight);
    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(mWhiteArray.contains(p) || mBlackArray.contains(p)){
            return false;
        }
        if(mIsWhite){
            mWhiteArray.add(p);
        }else{
            mBlackArray.add(p);
        }
        invalidate();
        mIsWhite=!mIsWhite;

    }
    return true; //view对这个touch事件感兴趣,交给本view处理
}
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() {
    //判断是否在line上满足五个棋子
    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();
    }
}

private boolean checkFiveInLine(List 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 checkRightDia(int x, int y, List points) {
    int count=1;
    for(int i=1;i points) {
    int count=1;
    for(int i=1;i points) {
    int count=1;
    for(int i=1;i points) {
    int count=1;
    for(int i=1;i

}

你可能感兴趣的:(Android Studio 五子棋(不敢和大佬做的比))