五子棋的棋盘Android版
子棋的棋盘Android版实现,具体上下文可以参考上一篇的五子棋AI实现中的代码[java]
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
//棋盘
public class Chessboard extends View implements IChessboard{
//游戏状态常量:
//已准备好,可开局
private static final int READY = 1;
//已开局
private static final int RUNNING = 2;
//已结束
private static final int PLAYER_TWO_LOST = 3;
private static final int PLAYER_ONE_LOST = 4;
//当前状态,默认为可开局状态
private int currentMode = READY;
//画笔对象
private final Paint paint = new Paint();
//代表绿色
private static final int GREEN = 0;
private static final int NEW_GREEN = 1;
//红色
private static final int RED = 2;
//黄色
private static final int NEW_RED = 3;
//点大小
private static int pointSize = 20;
//用于提示输赢的文本控件
private TextView textView = null;
//不同颜色的Bigmap数组
private Bitmap[] pointArray = new Bitmap[4];
//屏幕右下角的坐标值,即最大坐标值
private static int maxX;
private static int maxY;
//第一点偏离左上角从像数,为了棋盘居中
private static int yOffset;
private static int xOffset;
//两个玩家
//第一个玩家默认为人类玩家
private IPlayer player1 = new HumanPlayer();
//第二个则根据选择人机战还是对战模式来初始化
private IPlayer player2;
//预先初始两个第二玩家
//电脑玩家
private static IPlayer computer = AiFactory.getInstance(2);
//人类玩家
private static final IPlayer human = new HumanPlayer();
// 所有未下的空白点
private final List<Point> allFreePoints = new ArrayList<Point>();
public Chessboard(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
//把三个颜色的点准备好,并放入数组
Resources r = this.getContext().getResources();
fillPointArrays(GREEN,r.getDrawable(R.drawable.green_point));
fillPointArrays(NEW_GREEN,r.getDrawable(R.drawable.new_green_point));
fillPointArrays(RED,r.getDrawable(R.drawable.red_point));
fillPointArrays(NEW_RED,r.getDrawable(R.drawable.new_red_point));
//设置画线时用的颜色
paint.setColor(Color.LTGRAY);
}
//初始横线和竖线的数目
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
maxX = (int) Math.floor(w / pointSize);
maxY = (int) Math.floor(h / pointSize);
//设置X、Y座标微调值,目的整个框居中
xOffset = ((w - (pointSize * maxX)) / 2);
yOffset = ((h - (pointSize * maxY)) / 2);
//创建棋盘上的线条
createLines();
//初始化棋盘上所有空白点
createPoints();
}
//产生棋盘上所有的线
private void createLines(){
for (int i = 0; i <= maxX; i++) {//竖线
lines.add(new Line(xOffset+i*pointSize-pointSize/2, yOffset, xOffset+i*pointSize-pointSize/2, yOffset+maxY*pointSize));
}
for (int i = 0; i <= maxY; i++) {//横线
lines.add(new Line(xOffset, yOffset+i*pointSize-pointSize/2, xOffset+maxX*pointSize, yOffset+i*pointSize-pointSize/2));
}
}
//画棋盘
private List<Line> lines = new ArrayList<Line>();
private void drawChssboardLines(Canvas canvas){
for (Line line : lines) {
canvas.drawLine(line.xStart, line.yStart, line.xStop, line.yStop, paint);
}
}
//线类
class Line{
float xStart,yStart,xStop,yStop;
public Line(float
补充:移动开发 , Android ,