android自定义View之五子棋小游戏

简单人人对战版本,博主很懒,屏幕适配没有弄。。
试过写人机对战版本,无奈放弃,逻辑判断太多啦啊啊啊(1000多行逻辑判断,AI智商还是小学生水平)
凑合看看吧。。

package com.example.yuanxingtouxiang;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class WuZiQi extends View {
    // 白棋先手,当前轮到白棋
    private boolean mIsWhite = true;
    private ArrayList mWhiteArray = new ArrayList<>();
    private ArrayList mBlackArray = new ArrayList<>();
    private int mLineHeight = 75;
    private int mPanelWidth = 1100;
    private boolean gameOver = false;
    private boolean next = false;

    public WuZiQi(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBoard(canvas);
        drawPieces(canvas);
        checkGameOver();

    }

    private void checkGameOver() {
        boolean whiteWin = checkFiveLine(mWhiteArray);
        boolean blackWin = checkFiveLine(mBlackArray);
        String text = whiteWin ? "白棋胜利" : "黑棋胜利";
        if (whiteWin || blackWin) {
            Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
            gameOver = true;
            checkXiaYiJu();
        }
    }

    private void checkXiaYiJu() {
        nextGame();
    }

    private void nextGame() {

        next = true;
        mWhiteArray.clear();
        mBlackArray.clear();
        gameOver = false;

    }

    private boolean checkFiveLine(ArrayList points) {
        boolean win = false;
        for (Point p : points) {
            int x = p.x;
            int y = p.y;
            win = checkHengXiangWin(x, y, points);
            if (win)
                break;
            win = checkZongXiangWin(x, y, points);
            if (win)
                break;
            win = checkXie_1Win(x, y, points);
            if (win)
                break;
            win = checkXie_2Win(x, y, points);

        }
        return win;
    }

    private boolean checkXie_2Win(int x, int y, ArrayList points) {
        int sum = 1;
        boolean flag;
        for (int i = 1; i < 5; i++) {
            flag = false;
            if (points.contains(new Point(x - i * mLineHeight, y + i * mLineHeight))) {
                sum++;
                flag = true;
            }
            if (!flag)
                break;
        }
        if (sum == 5)
            return true;
        for (int i = 1; i < 5; i++) {
            flag = false;
            if (points.contains(new Point(x + i * mLineHeight, y - i * mLineHeight))) {
                sum++;
                flag = true;
            }
            if (!flag)
                break;
            if (sum == 5)
                return true;
        }
        return false;
    }

    private boolean checkXie_1Win(int x, int y, ArrayList points) {

        int sum = 1;
        boolean flag;
        for (int i = 1; i < 5; i++) {
            flag = false;
            if (points.contains(new Point(x - i * mLineHeight, y - i * mLineHeight))) {
                sum++;
                flag = true;
            }
            if (!flag)
                break;
        }
        if (sum == 5)
            return true;

        for (int i = 1; i < 5; i++) {
            flag = false;
            if (points.contains(new Point(x + i * mLineHeight, y + i * mLineHeight))) {
                sum++;
                flag = true;
            }
            if (!flag)
                break;
            if (sum == 5)
                return true;

        }
        return false;
    }

    private boolean checkZongXiangWin(int x, int y, ArrayList points) {
        int sum = 1;
        boolean flag;
        for (int i = 1; i < 5; i++) {
            flag = false;
            if (points.contains(new Point(x, y - i * mLineHeight))) {
                sum++;
                flag = true;
            }
            if (!flag)
                break;
        }
        if (sum == 5)
            return true;
        for (int i = 1; i < 5; i++) {
            flag = false;
            if (points.contains(new Point(x, y + i * mLineHeight))) {
                sum++;
                flag = true;
            }
            if (!flag)
                break;
            if (sum == 5)
                return true;
        }
        return false;
    }

    private boolean checkHengXiangWin(int x, int y, List points) {
        int sum = 1;
        boolean flag;
        for (int i = 1; i < 5; i++) {
            flag = false;
            if (points.contains(new Point(x - i * mLineHeight, y))) {
                sum++;
                flag = true;
            }
            if (!flag)
                break;
        }
        if (sum == 5)
            return true;
        for (int i = 1; i < 5; i++) {
            flag = false;
            if (points.contains(new Point(x + i * mLineHeight, y))) {
                sum++;
                flag = true;
            }
            if (!flag)
                break;
            if (sum == 5)
                return true;
        }
        return false;
    }

    private void drawBoard(Canvas canvas) {
        int MAX_LINE = 22;
        float lineHeight = mLineHeight;
        Paint mPaint = new Paint();
        for (int i = 0; i < MAX_LINE; i++) {
            int startX1 = (int) (lineHeight / 2);
            int startY1 = (int) ((0.5 + i) * lineHeight);

            int endX1 = (int) (mPanelWidth - lineHeight / 2) - 50;
            int endY1 = (int) ((0.5 + i) * lineHeight);

            int startX2 = (int) ((0.5 + i) * lineHeight);
            int startY2 = (int) (lineHeight / 2);

            int endX2 = (int) ((0.5 + i) * lineHeight);
            int endY2 = (int) (mPanelWidth - lineHeight / 2) + 550;

            canvas.drawLine(startX1, startY1, endX1, endY1, mPaint);
            canvas.drawLine(startX2, startY2, endX2, endY2, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gameOver) {
            return false;
        }

        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP) {
            if (next) {
                next = false;
                String text = "下一局";
                Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
                invalidate();
                return true;
            }
            int x = (int) event.getX();
            int y = (int) event.getY();
            Point p = getValidPoint(x, y);
            if (mWhiteArray.contains(p) || mBlackArray.contains(p) || x > 1050 || y > 1650) {
                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(((x / mLineHeight)) * mLineHeight + mLineHeight / 2,
                ((y / mLineHeight)) * mLineHeight + mLineHeight / 2);
    }

    private void drawPieces(Canvas canvas) {
        Paint p1 = new Paint();
        Paint p2 = new Paint();
        p1.setColor(Color.rgb(245, 245, 245));
        p2.setColor(Color.BLACK);
        for (int i = 0, n = mWhiteArray.size(); i < n; i++) {
            Point whitePoint = mWhiteArray.get(i);
            canvas.drawCircle(whitePoint.x, whitePoint.y, 25, p1);
        }

        for (int i = 0, n = mBlackArray.size(); i < n; i++) {
            Point blackPoint = mBlackArray.get(i);
            canvas.drawCircle(blackPoint.x, blackPoint.y, 25, p2);
        }
    }

}

—————————————————————————————————————————–Frozen 2016-12-18

你可能感兴趣的:(android自定义View之五子棋小游戏)