最近有人來555974449劉某人的群裡問五子棋原始碼,由於我也是Android菜雞於是心血來潮就去研究了單機版的五子棋綜合了一些大牛的部落格,為了防止忘記就所以記錄下來,有什麼不好處歡迎指導,謝謝!
#佈局頁面<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:wzq="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg"
tools:context="com.gx.wei.gobang.MainActivity">
<!-- 可以在自定義View中使用自定義的屬性 此處設定最大行列格子數為10,連續相鄰5個同棋遊戲結束-->
<com.gx.wei.gobang.WuziqiPanel
android:id="@+id/id_wuziqi"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
wzq:panel_background="@mipmap/panel_bg"
wzq:panel_line_color="#C00"
wzq:white_piece_img="@mipmap/stone_w2"
wzq:black_piece_img="@mipmap/stone_b1"
wzq:max_count_line="11"
wzq:max_win_count_piece="5"/>
</RelativeLayout>
複製程式碼
#自定義view的工具類
package com.gx.wei.gobang;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* Created by caobotao on 16/3/31.
*/
public class WuziqiPanel extends View {
private int mPanelWidth ; //棋盤寬度
private float mLineHeight; //棋盤單行間距
private int MAX_LINE;//棋盤行列數
private Paint mPaint = new Paint();
private int mPanelLineColor; //棋盤線的顏色
private Bitmap mWhitePiece; //白棋的圖片
private Bitmap mBlackPiece; //黑棋的圖片
//棋子佔行距的比例
private final float RATIO_PIECE_OF_LINE_HEIGHT = 3 * 1.0f / 4;
//是否將要下白棋
private boolean mIsWhite = true;
//已下的白棋的列表
private ArrayList<Point> mWhitePieceArray = new ArrayList<>();
//已下的黑棋的列表
private ArrayList<Point> mBlackPieceArray = new ArrayList<>();
//遊戲是否結束
private boolean mIsGameOver;
private final int INIT_WIN = -1; //遊戲開始時的狀態
public static final int WHITE_WIN = 0; //白棋贏
public static final int BLACK_WIN = 1; //黑棋贏
public static final int NO_WIN = 2; //和棋
private int mGameWinResult = INIT_WIN; //初始化遊戲結果
private OnGameStatusChangeListener listener;//遊戲狀態監聽器
private int MAX_COUNT_IN_LINE; //多少顆棋子相鄰時贏棋
//設定遊戲狀態監聽器
public void setOnGameStatusChangeListener(OnGameStatusChangeListener listener) {
this.listener = listener;
}
public WuziqiPanel(Context context) {
this(context,null);
}
public WuziqiPanel(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public WuziqiPanel(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//獲取xml中自定義的屬性值並對相應的屬性賦值
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.WuziqiPanel);
int n = a.getIndexCount();
for (int i = 0;i < n; i++) {
int attrName = a.getIndex(i);
switch (attrName) {
//棋盤背景
case R.styleable.WuziqiPanel_panel_background:
BitmapDrawable panelBackgroundBitmap = (BitmapDrawable) a.getDrawable(attrName);
setBackground(panelBackgroundBitmap);
break;
//棋盤線的顏色
case R.styleable.WuziqiPanel_panel_line_color:
mPanelLineColor = a.getColor(attrName, 0x88000000);
break;
//白棋圖片
case R.styleable.WuziqiPanel_white_piece_img:
BitmapDrawable whitePieceBitmap = (BitmapDrawable) a.getDrawable(attrName);
mWhitePiece = whitePieceBitmap.getBitmap();
break;
//黑棋圖片
case R.styleable.WuziqiPanel_black_piece_img:
BitmapDrawable blackPieceBitmap = (BitmapDrawable) a.getDrawable(attrName);
mBlackPiece = blackPieceBitmap.getBitmap();
break;
case R.styleable.WuziqiPanel_max_count_line:
MAX_LINE = a.getInteger(attrName, 10);
break;
case R.styleable.WuziqiPanel_max_win_count_piece:
MAX_COUNT_IN_LINE = a.getInteger(attrName, 5);
break;
}
}
init();
}
//初始化遊戲資料
private void init() {
mPaint.setColor(mPanelLineColor);
mPaint.setAntiAlias(true);//抗鋸齒
mPaint.setDither(true);//防抖動
mPaint.setStyle(Style.FILL);
if (mWhitePiece == null) {
mWhitePiece = BitmapFactory.decodeResource(getResources(), R.mipmap.stone_w2);
}
if (mBlackPiece == null) {
mBlackPiece = BitmapFactory.decodeResource(getResources(), R.mipmap.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);
//解決巢狀在ScrollView中時等情況出現的問題
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 * RATIO_PIECE_OF_LINE_HEIGHT);
mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece, pieceWidth, pieceWidth, false);
mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece, pieceWidth, pieceWidth, false);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBoard(canvas);
drawPiece(canvas);
checkGameOver();
}
//重新開始遊戲
public void restartGame() {
mWhitePieceArray.clear();
mBlackPieceArray.clear();
mIsGameOver = false;
mGameWinResult = INIT_WIN;
invalidate();
}
//檢查遊戲是否結束
private void checkGameOver() {
boolean whiteWin = checkFiveInLine(mWhitePieceArray);
boolean blackWin = checkFiveInLine(mBlackPieceArray);
boolean noWin = checkNoWin(whiteWin,blackWin);
//如果遊戲結束,獲取遊戲結果mGameWinResult
if (whiteWin) {
mGameWinResult = WHITE_WIN;
} else if (blackWin) {
mGameWinResult = BLACK_WIN;
} else if(noWin){
mGameWinResult = NO_WIN;
}
if (whiteWin || blackWin || noWin) {
mIsGameOver = true;
//回撥遊戲狀態介面
if (listener != null) {
listener.onGameOver(mGameWinResult);
}
}
}
//檢查是否五子連珠
private boolean checkFiveInLine(List<Point> points) {
for (Point point : points) {
int x = point.x;
int y = point.y;
boolean checkHorizontal = checkHorizontalFiveInLine(x,y,points);
boolean checkVertical = checkVerticalFiveInLine(x,y,points);
boolean checkLeftDiagonal = checkLeftDiagonalFiveInLine(x,y,points);
boolean checkRightDiagonal = checkRightDiagonalFiveInLine(x,y,points);
if (checkHorizontal || checkVertical || checkLeftDiagonal || checkRightDiagonal) {
return true;
}
}
return false;
}
//檢查向右斜的線上有沒有相同棋子的五子連珠
private boolean checkRightDiagonalFiveInLine(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 checkLeftDiagonalFiveInLine(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 checkVerticalFiveInLine(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 checkHorizontalFiveInLine(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 checkNoWin(boolean whiteWin, boolean blackWin) {
if (whiteWin || blackWin) {
return false;
}
int maxPieces = MAX_LINE * MAX_LINE;
//如果白棋和黑棋的總數等於棋盤格子數,說明和棋
if (mWhitePieceArray.size() + mBlackPieceArray.size() == maxPieces) {
return true;
}
return false;
}
//繪製棋子
private void drawPiece(Canvas canvas) {
for (int i = 0,n = mWhitePieceArray.size();i < n;i++) {
Point whitePoint = mWhitePieceArray.get(i);
canvas.drawBitmap(mWhitePiece,
(whitePoint.x + (1 -RATIO_PIECE_OF_LINE_HEIGHT) / 2) * mLineHeight,
(whitePoint.y + (1 -RATIO_PIECE_OF_LINE_HEIGHT) / 2) * mLineHeight,null);
}
for (int i = 0,n = mBlackPieceArray.size();i < n;i++) {
Point blackPoint = mBlackPieceArray.get(i);
canvas.drawBitmap(mBlackPiece,
(blackPoint.x + (1 -RATIO_PIECE_OF_LINE_HEIGHT) / 2) * mLineHeight,
(blackPoint.y + (1 -RATIO_PIECE_OF_LINE_HEIGHT) / 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, mPaint);//畫橫線
canvas.drawLine(y, startX, y, endX, mPaint);//畫豎線
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mIsGameOver) {
return false;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
int x = (int) event.getX();
int y = (int) event.getY();
Point p = getValidPoint(x, y);
if (mWhitePieceArray.contains(p) || mBlackPieceArray.contains(p)) {
return false;
}
if (mIsWhite) {
mWhitePieceArray.add(p);
} else {
mBlackPieceArray.add(p);
}
invalidate();
mIsWhite = !mIsWhite;
return true;
}
return true;
}
//根據觸控點獲取最近的格子位置
private Point getValidPoint(int x, int y) {
return new Point((int)(x / mLineHeight),(int)(y / mLineHeight));
}
/**
* 當View被銷燬時需要儲存遊戲資料
*/
private static final String INSTANCE = "instance";
private static final String INSTANCE_GAME_OVER = "instance_game_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_GAME_OVER, mIsGameOver);
bundle.putParcelableArrayList(INSTANCE_WHITE_ARRAY, mWhitePieceArray);
bundle.putParcelableArrayList(INSTANCE_BLACK_ARRAY, mBlackPieceArray);
return bundle;
}
//恢復遊戲資料
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
mIsGameOver = bundle.getBoolean(INSTANCE_GAME_OVER);
mWhitePieceArray = bundle.getParcelableArrayList(INSTANCE_WHITE_ARRAY);
mBlackPieceArray = bundle.getParcelableArrayList(INSTANCE_BLACK_ARRAY);
super.onRestoreInstanceState(bundle.getParcelable(INSTANCE));
return;
}
super.onRestoreInstanceState(state);
}
}
複製程式碼
#遊戲監聽介面
package com.gx.wei.gobang;
public interface OnGameStatusChangeListener {
void onGameOver(int gameWinResult);//遊戲結束
}
複製程式碼
#MainActivity
package com.gx.wei.gobang;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends AppCompatActivity{
private WuziqiPanel mGamePanel;
private AlertDialog.Builder alertBuilder;
private AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//遊戲結束時彈出對話方塊
alertBuilder = new AlertDialog.Builder(MainActivity.this);
alertBuilder.setPositiveButton("再來一局", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mGamePanel.restartGame();
}
});
alertBuilder.setNegativeButton("退出遊戲", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
});
alertBuilder.setCancelable(false);
alertBuilder.setTitle("此局結束");
mGamePanel = (WuziqiPanel) findViewById(R.id.id_wuziqi);
mGamePanel.setOnGameStatusChangeListener(new OnGameStatusChangeListener() {
@Override
public void onGameOver(int gameWinResult) {
switch (gameWinResult) {
case WuziqiPanel.WHITE_WIN:
alertBuilder.setMessage("白棋勝利!");
break;
case WuziqiPanel.BLACK_WIN:
alertBuilder.setMessage("黑棋勝利!");
break;
case WuziqiPanel.NO_WIN:
alertBuilder.setMessage("和棋!");
break;
}
alertDialog = alertBuilder.create();
alertDialog.show();
}
});
}
}
複製程式碼
打一波廣告:原始碼已上傳“通往Android的神奇之旅”歡迎來討論學習Android或非Android問題