主要是实现两人对战象棋,没有实现人机对战,主要不会判断下一步棋走那个好,或者对每下一步棋进行打分而进行选择最高分进而走哪一步棋
Activity类:
public class ChessActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //LinearLayout里包裹了一个TextView
TextView text = (TextView) findViewById(R.id.text);
text.setText("象棋");
LinearLayout linear = (LinearLayout) findViewById(R.id.linear);
ChessView ccView = new ChessView(ChessActivity.this); //自定义View --- 也可使用SurfaceView(需要循环draw) https://blog.csdn.net/android_cmos/article/details/68955134
ccView.setText(text);
linear.addView(ccView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
}
自定义的View类:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class ChessView extends View {
// TODO 画棋盘
// TODO 画棋子
// TODO 触摸交互 --> 旗子走动 --->可移动规则
// TODO 判断输赢
private int chessWidth = Rules.Config.chessWidth; //棋子宽度和高度是一样
private Bitmap boardBitmap; //棋盘bitmap
private int red = 0; //红色棋子
private int black = 1; //黑色棋子
private int currentChessMove = red; //当前走棋的棋子
private boolean chooseStatus; //状态 是否选中棋子
//白方:1车 2马 3相 4士 5帅 6炮 7兵
//黑方:14车 13马 12相 11士 10帅 9炮 8兵
private int[] currentPosition = new int[2]; //用来记录上一步棋子的x,y
Paint paint = new Paint();
Paint redPaint = new Paint();
Paint blackPaint = new Paint();
public ChessView(Context context) {
this(context, null);
}
public ChessView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ChessView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
//初始化一些东西
private void init() {
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
//设置去锯齿
paint.setAntiAlias(true);
paint.setTextSize(45);
paint.setStrokeWidth(3);
redPaint.setColor(Color.RED);
redPaint.setAntiAlias(true);
redPaint.setTextSize(60);
redPaint.setStyle(Paint.Style.FILL);
blackPaint.setStyle(Paint.Style.FILL);
blackPaint.setColor(Color.BLACK);
blackPaint.setAntiAlias(true);
blackPaint.setTextSize(60);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //自己设置宽和高相等
int width = MeasureSpec.getSize(widthMeasureSpec);
if (chessWidth == 0)
chessWidth = width / 10;
Log.d("tag", "onMeasure:width=" + chessWidth);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(width + chessWidth, MeasureSpec.EXACTLY);
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
float touchX, touchY; //用于记录触摸的点
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("tag", "onTouchEvent:x=" + event.getX() + " y=" + event.getY() + " 10 * chessWidth=" + 10 * chessWidth);
if (event.getY() > 10.5 * chessWidth||Rules.win()!=Rules.non_win) { //超出棋盘范围的点不需要 -- 因为有10行,棋子也占半行
return false; //有人赢了也不允许移动
}
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
touchX = event.getX();
touchY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
Log.d("tag", "移动棋子:x=" + event.getX() + " y=" + event.getY() + " chessWidth=" + chessWidth + " touchX=" + touchX + " touchY=" + touchY);
if (Math.round(event.getX() - touchX) > chessWidth || Math.round(event.getY() - touchY) > chessWidth) {
Log.d("tag", "移动棋子不可跨度太大");
return super.onTouchEvent(event); //想要移动的棋子不可确认 --- down和up坐标大
} else {
int x = (int) (event.getX() / chessWidth - 0.5f);
int y = (int) (event.getY() / chessWidth - 0.5f);
Log.d("tag", "移动棋子:x=" + x + " y=" + y);
if (y > 9 || x > 8) {
return super.onTouchEvent(event);
}
if (currentChessMove == red) { //红棋走
if (chooseStatus == false) { //没选中棋子 -- 开始选
if (Rules.chessValue(x, y) > 0 && Rules.chessValue(x, y) < 8) {
chooseStatus = true;
currentPosition[0] = x;
currentPosition[1] = y;
invalidate(); //重新draw
}
} else { //已经选中棋子 --- 移动
if (Rules.canMove(currentPosition[0], currentPosition[1], x, y)) {
//可以移动棋子
chooseStatus = false;
Rules.moveChess(currentPosition[0], currentPosition[1], x, y);
currentChessMove = black;
invalidate(); //重新draw
} else if (Rules.chessValue(x, y) > 0 && Rules.chessValue(x, y) < 8) {
currentPosition[0] = x; //选中别的棋子
currentPosition[1] = y;
invalidate();
} else {
Toast.makeText(getContext(), "不符合规则", Toast.LENGTH_SHORT).show();
}
}
} else { //黑棋走
if (chooseStatus == false) { //没选中棋子
if (Rules.chessValue(x, y) > 7 && Rules.chessValue(x, y) < 15) {
chooseStatus = true;
currentPosition[0] = x;
currentPosition[1] = y;
invalidate(); //重新draw
}
} else { //已经选中棋子
if (Rules.canMove(currentPosition[0], currentPosition[1], x, y)) {
//可以移动棋子
chooseStatus = false;
Rules.moveChess(currentPosition[0], currentPosition[1], x, y);
currentChessMove = red;
invalidate();
} else if (Rules.chessValue(x, y) > 7 && Rules.chessValue(x, y) < 15) {
currentPosition[0] = x; //选中别的棋子
currentPosition[1] = y;
invalidate();
} else {
Toast.makeText(getContext(), "不符合规则", Toast.LENGTH_SHORT).show();
}
}
}
}
break;
}
return true;
}
TextView tv;
public void setText(TextView tv) { //用来提示信息 -- 显示谁赢等
this.tv = tv;
}