安卓開發 五子棋遊戲
五子棋
Chess類
package com.example.gobang5.tools;
public class Chess {
public enum Color {BLACK, WHITE, NONE}
public Color color;
public Chess() {
this.color = Color.NONE;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
GameView
package com.example.gobang5.views;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
import com.example.gobang5.tools.Chess;
import java.util.ArrayList;
import java.util.List;
public class GoBangView extends View {
private List<Point> mEveryPlay;
private Chess[][] mChessArray;
private Paint mBgPaint, mLinePaint, mChessPaint;
private boolean mIsBlack, mIsLock;
private OnGameListener mListnere;
private void init() {
mEveryPlay = new ArrayList<>(255);
mBgPaint = new Paint();
mLinePaint = new Paint();
mChessPaint = new Paint();
mBgPaint.setAntiAlias(true);
mLinePaint.setAntiAlias(true);
mChessPaint.setAntiAlias(true);
mBgPaint.setColor(Color.GRAY);
mLinePaint.setColor(Color.BLACK);
mChessPaint.setColor(Color.WHITE);
mIsBlack = true;
mIsLock = false;
mChessArray = new Chess[15][15];
for (int i = 0; i < mChessArray.length; i++) {
for (int j = 0; j < mChessArray.length; j++) {
mChessArray[i][j] = new Chess();
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int min = width < height ? width : height;
setMeasuredDimension(min, min);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height = getMeasuredHeight();
int width = getMeasuredWidth();
int avg = height / 16;
canvas.drawRect(0, 0, width, height, mBgPaint);
for (int i = 1; i < 16; i++) {
for (int j = 1; j < 16; j++) {
canvas.drawLine(avg * i, avg, avg * i, height - avg, mLinePaint);
canvas.drawLine(avg, avg * i, height - avg, avg * i, mLinePaint);
}
}
for (int i = 1; i < 16; i++) {
for (int j = 1; j < 16; j++) {
switch (mChessArray[i - 1][j - 1].getColor()) {
case BLACK:
mChessPaint.setColor(Color.BLACK);
break;
case WHITE:
mChessPaint.setColor(Color.WHITE);
break;
case NONE:
continue;
}
canvas.drawCircle(avg * i, avg * j, avg / 2 - 0.5f, mChessPaint);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mIsLock) {
return true;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float y = event.getY();
float x = event.getX();
Rect rect = createRect(x, y);
Point point = queryPoint(rect);
if (point != null) {
addChess(point);
mEveryPlay.add(point);
if (isOver(point)) {
Log.d("Over", "onTouchEvent: Over");
if (mListnere != null) {
mListnere.gameOver();
}
}
mIsBlack = !mIsBlack;
}
}
return super.onTouchEvent(event);
}
private boolean isOver(Point point) {
int y = point.y;
int x = point.x;
Chess.Color color = mChessArray[x][y].getColor();
return overA(x, y, color) || overB(x, y, color) || overC(x, y, color) || overD(x, y, color);
}
private boolean overD(int x, int y, Chess.Color color) {
int count = 0;
for (int i = x, j = y; i < mChessArray.length && j >= 0; i++, j--) {
if (mChessArray[i][j].getColor() == color) {
count++;
} else {
break;
}
}
for (int i = x, j = y; i >= 0 && j < mChessArray[i].length; i--, j++) {
if (mChessArray[i][j].getColor() == color) {
count++;
} else {
break;
}
}
return count > 5;
}
private boolean overC(int x, int y, Chess.Color color) {
int count = 0;
for (int i = x, j = y; i < mChessArray.length && j < mChessArray[i].length; i++, j++) {
if (mChessArray[i][j].getColor() == color) {
count++;
} else {
break;
}
}
for (int i = x, j = y; i >= 0 && j >= 0; i--, j--) {
if (mChessArray[i][j].getColor() == color) {
count++;
} else {
break;
}
}
return count > 5;
}
private boolean overB(int x, int y, Chess.Color color) {
int count = 0;
for (int i = x; i < mChessArray.length; i++) {
if (mChessArray[i][y].getColor() == color) {
count++;
} else {
break;
}
}
for (int i = x; i >= 0; i--) {
if (mChessArray[i][y].getColor() == color) {
count++;
} else {
break;
}
}
return count > 5;
}
private boolean overA(int x, int y, Chess.Color color) {
int count = 0;
for (int i = y; i < mChessArray.length; i++) {
if (mChessArray[x][i].getColor() == color) {
count++;
} else {
break;
}
}
for (int i = y; i >= 0; i--) {
if (mChessArray[x][i].getColor() == color) {
count++;
} else {
break;
}
}
return count > 5;
}
private void addChess(Point point) {
if (mIsBlack) {
mChessArray[point.x][point.y].setColor(Chess.Color.BLACK);
} else {
mChessArray[point.x][point.y].setColor(Chess.Color.WHITE);
}
invalidate();
}
private Point queryPoint(Rect rect) {
int avg = getMeasuredHeight() / 16;
for (int i = 1; i < 16; i++) {
for (int j = 1; j < 16; j++) {
if (rect.contains(i * avg, j * avg)) {
Point point = new Point(i - 1, j - 1);
if (mChessArray[i - 1][j - 1].getColor() == Chess.Color.NONE) {
return point;
}
}
}
}
return null;
}
private Rect createRect(float x, float y) {
int slide = getMeasuredHeight() / 16;
int left = (int) (x - slide / 2);
int right = (int) (x + slide / 2);
int top = (int) (y - slide / 2);
int bottom = (int) (y + slide / 2);
return new Rect(left, top, right, bottom);
}
public void regret() {
if (mEveryPlay.isEmpty()) {
return;
}
Point point = mEveryPlay.get(mEveryPlay.size() - 1);
mChessArray[point.x][point.y].setColor(Chess.Color.NONE);
mEveryPlay.remove(mEveryPlay.size() - 1);
mIsBlack = !mIsBlack;
invalidate();
}
public void again() {
for (Chess[] chess : mChessArray) {
for (Chess chess1 : chess) {
chess1.setColor(Chess.Color.NONE);
}
}
mEveryPlay.clear();
mIsBlack = true;
mIsLock = false;
invalidate();
}
public interface OnGameListener {
void gameOver();
}
public void setOnGameListener(OnGameListener listener) {
this.mListnere = listener;
}
public boolean ismIsLock() {
return mIsLock;
}
public void setmIsLock(boolean mIsLock) {
this.mIsLock = mIsLock;
}
public GoBangView(Context context) {
this(context, null);
}
public GoBangView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public GoBangView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
}
相關文章
- 基於 Blazor 開發五子棋⚫⚪小遊戲Blazor遊戲
- 我眼中的五子棋遊戲開發遊戲開發
- 安卓開發筆記——數獨遊戲安卓筆記遊戲
- 五子棋遊戲(Java)遊戲Java
- JS之五子棋遊戲JS遊戲
- 安卓開發框架系列開篇安卓框架
- [譯] 安卓應用和遊戲的無障礙開發介紹安卓遊戲
- 安卓開發--AIDL研究安卓AI
- 自學安卓app開發(一)安卓APP
- 安卓開發環境搭建安卓開發環境
- 安卓之旅-時鐘開發安卓
- 安卓開發工具安裝安卓
- 安卓開發日記57安卓
- 安卓開發日記56安卓
- 安卓開發日記55安卓
- 安卓開發日記26安卓
- 安卓開發日記25安卓
- 安卓開發日記24安卓
- 安卓開發日記28安卓
- 安卓開發日記27安卓
- go語言安卓開發Go安卓
- 安卓開發日記4安卓
- 安卓開發日記13安卓
- 安卓開發日記14安卓
- 安卓開發日記12安卓
- 安卓開發日記17安卓
- 安卓開發日記16安卓
- 安卓開發日記15安卓
- 安卓開發日記19安卓
- 安卓開發日記18安卓
- 安卓開發日記46安卓
- 安卓開發日記45安卓
- 安卓開發日記47安卓
- 安卓開發第一步:安卓面試題安卓面試題
- 安卓開發入門(一)開發環境搭建安卓開發環境
- C語言五子棋小遊戲C語言遊戲
- 安卓開發之星座查詢安卓
- 安卓USB開發教程 USB Accessory安卓