android studio五子棋游戏
时间: 2023-11-18 19:04:06 浏览: 204
以下是基于Android Studio开发的五子棋游戏的实现方法:
1. 首先,我们需要定义一个ChessView类,并在其中定义一些成员变量,以便于后续使用。在需要显示五子棋的布局文件中引入该自定义View。
2. 接下来,我们需要编写构造函数,并在其中进行一些初始化设置。例如,初始化画笔以及将棋子图片转换为bitmap等。
3. 在onMeasure()方法中获取屏幕宽高,在onSizeChanged()方法中获取棋盘的宽度,并计算出棋盘的行高。然后,根据行高缩放棋子大小,使其显示大小合适。
4. 每次调用invalidate()方法时,都会调用onDraw()方法进行界面绘制。在该方法中,我们需要先绘制棋盘,然后绘制用户所下的棋子。
5. 对于人机对战模式,我们需要实现一些简单的算法,以实现人机交互。对于人人对战模式,我们只需要进行输赢方的判定即可。
下面是一个简单的五子棋游戏的实现代码:
```java
public class ChessView extends View {
private int mPanelWidth;
private float mLineHeight;
private Bitmap mWhitePiece;
private Bitmap mBlackPiece;
private float ratioPieceOfLineHeight = 3 * 1.0f / 4;
private boolean mIsWhite = true;
private ArrayList<Point> mWhiteArray = new ArrayList<>();
private ArrayList<Point> mBlackArray = new ArrayList<>();
private boolean mIsGameOver;
private boolean mIsWhiteWinner;
private int MAX_LINE = 10;
private Paint mPaint = new Paint();
public ChessView(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(0x44ff0000);
init();
}
private void init() {
mPaint.setColor(0x88000000);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.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 heightSize = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
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) {
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;
}
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 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);
}
}
private void drawPieces(Canvas canvas) {
for (int i = 0, n = mWhiteArray.size(); i < n; i++) {
Point whitePoint = mWhiteArray.get(i);
canvas.drawBitmap(mWhitePiece,
(whitePoint.x + (1 - ratioPieceOfLineHeight) / 2) * mLineHeight,
(whitePoint.y + (1 - ratioPieceOfLineHeight) / 2) * mLineHeight, null);
}
for (int i = 0, n = mBlackArray.size(); i < n; i++) {
Point blackPoint = mBlackArray.get(i);
canvas.drawBitmap(mBlackPiece,
(blackPoint.x + (1 - ratioPieceOfLineHeight) / 2) * mLineHeight,
(blackPoint.y + (1 - ratioPieceOfLineHeight) / 2) * mLineHeight, null);
}
}
private void checkGameOver() {
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<Point> points) {
for (Point p : points) {
int x = p.x;
int y = p.y;
boolean win = checkHorizontal(x, y, points);
if (win) return true;
win = checkVertical(x, y, points);
if (win) return true;
win = checkLeftDiagonal(x, y, points);
if (win) return true;
win = checkRightDiagonal(x, y, points);
if (win) return true;
}
return false;
}
private boolean checkHorizontal(int x, int y, List<Point> points) {
int count = 1;
for (int i = 1; i < 5; i++) {
if (points.contains(new Point(x - i, y))) {
count++;
} else {
break;
}
}
if (count == 5) return true;
for (int i = 1; i < 5; i++) {
if (points.contains(new Point(x + i, y))) {
count++;
} else {
break;
}
}
if (count == 5) return true;
return false;
}
private boolean checkVertical(int x, int y, List<Point> points) {
int count = 1;
for (int i = 1; i < 5; i++) {
if (points.contains(new Point(x, y - i))) {
count++;
} else {
break;
}
}
if (count == 5) return true;
for (int i = 1; i < 5; i++) {
if (points.contains(new Point(x, y + i))) {
count++;
} else {
break;
}
}
if (count == 5) return true;
return false;
}
private boolean checkLeftDiagonal(int x, int y, List<Point> points) {
int count = 1;
for (int i = 1; i < 5; i++) {
if (points.contains(new Point(x - i, y + i))) {
count++;
} else {
break;
}
}
if (count == 5) return true;
for (int i = 1; i < 5; i++) {
if (points.contains(new Point(x + i, y - i))) {
count++;
} else {
break;
}
}
if (count == 5) return true;
return false;
}
private boolean checkRightDiagonal(int x, int y, List<Point> points) {
int count = 1;
for (int i = 1; i < 5; i++) {
if (points.contains(new Point(x + i, y + i))) {
count++;
} else {
break;
}
}
if (count == 5) return true;
for (int i = 1; i < 5; i++) {
if (points.contains(new Point(x - i, y - i))) {
count++;
} else {
break;
}
}
if (count == 5) return true;
return false;
}
}
```
阅读全文