欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)象棋游戲

 更新時間:2022年05月10日 11:00:24   作者:碼碼大人  
這篇文章主要為大家詳細介紹了Android實現(xiàn)象棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)象棋游戲的具體代碼,供大家參考,具體內(nèi)容如下

主要是實現(xiàn)兩人對戰(zhàn)象棋,沒有實現(xiàn)人機對戰(zhàn),主要不會判斷下一步棋走那個好,或者對每下一步棋進行打分而進行選擇最高分進而走哪一步棋

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(需要循環(huán)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 觸摸交互 --> 旗子走動 ? --->可移動規(guī)則
? ? // TODO 判斷輸贏
? ? private int chessWidth = Rules.Config.chessWidth; ? ?//棋子寬度和高度是一樣
? ? private Bitmap boardBitmap; ? ? //棋盤bitmap
? ? private int red = 0; //紅色棋子
? ? private int black = 1; //黑色棋子
? ? private int currentChessMove = red; //當(dāng)前走棋的棋子
? ? private boolean chooseStatus; //狀態(tài) 是否選中棋子
? ? //白方: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);
? ? ? ? //設(shè)置去鋸齒
? ? ? ? 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) { //自己設(shè)置寬和高相等
? ? ? ? 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); ? //想要移動的棋子不可確認(rèn) --- down和up坐標(biāo)大
? ? ? ? ? ? ? ? } 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 { ?//已經(jīng)選中棋子 --- 移動
? ? ? ? ? ? ? ? ? ? ? ? ? ? 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(), "不符合規(guī)則", 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 { ?//已經(jīng)選中棋子
? ? ? ? ? ? ? ? ? ? ? ? ? ? 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(), "不符合規(guī)則", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;

? ? ? ? }
? ? ? ? return true;
? ? }

? ? TextView tv;

? ? public void setText(TextView tv) { ?//用來提示信息 ?-- 顯示誰贏等
? ? ? ? this.tv = tv;
? ? }

? ? @Override
? ? protected void onSizeChanged(int w, int h, int oldw, int oldh) { //這個也可以獲取寬高
? ? ? ? super.onSizeChanged(w, h, oldw, oldh);
? ? ? ? if (chessWidth == 0)
? ? ? ? ? ? chessWidth = getWidth() / 10;
? ? ? ? Log.d("tag", "onSizeChanged:width=" + chessWidth);
? ? }

? ? @Override
? ? public void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);
? ? ? ? long time = System.currentTimeMillis();
? ? ? ? canvas.drawColor(Color.parseColor("#5500ff00")); ?//畫出白色背景
? ? ? ? canvasBoard(canvas); ? ?//畫棋盤 ? --- 可以優(yōu)化一下,保存一張bitmap,直接drawBitmap
? ? ? ? if (chooseStatus) {
? ? ? ? ? ? paint.setColor(Color.BLUE);
? ? ? ? ? ? canvas.drawCircle(chessWidth * (currentPosition[0] + 1), chessWidth * (currentPosition[1] + 1), chessWidth / 2 + 5, paint);
? ? ? ? }
? ? ? ? canvasChess(canvas); ? ?//畫棋子
? ? ? ? int win = Rules.win();
? ? ? ? if (win != Rules.non_win) {
? ? ? ? ? ? if (tv == null) {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? if (win == Rules.up_win) {
? ? ? ? ? ? ? ? tv.setText("紅方贏");
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? tv.setText("黑方贏");
? ? ? ? ? ? }
? ? ? ? ? ? getParent().requestDisallowInterceptTouchEvent(false); ?//
? ? ? ? }
? ? ? ? Log.d("tag", "用時:" + (System.currentTimeMillis() - time) ); //一般不超過20ms
? ? }

? ? int widthCenter = 1; ? ?//楚河漢界中間是空白的,需要調(diào)整一些不能讓棋盤不好看
? ? int space = chessWidth / 10; ? ?//一開始為0,獲取不到chessWidth
? ? // ? ?int line = 40;
? ? int line = chessWidth / 3;

? ? private void canvasBoard(Canvas canvas) {
? ? ? ? if (boardBitmap == null) {
? ? ? ? ? ? space = chessWidth / 10; ?//確定線的間隔
? ? ? ? ? ? line = chessWidth / 3; ? ?//線的長度
? ? ? ? ? ? boardBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
? ? ? ? ? ? Log.d("tag", "boardBitmap==null" + getWidth() + "height:" + getHeight());
? ? ? ? ? ? Canvas bb = new Canvas(boardBitmap);
? ? ? ? ? ? paint.setColor(Color.BLACK);
? ? ? ? ? ? for (int i = 0; i < 10; i++) { ?//畫線
? ? ? ? ? ? ? ? bb.drawLine(chessWidth, chessWidth * (i + 1), chessWidth * 9, chessWidth * (i + 1), paint);//畫出橫線 ?-- 9行
? ? ? ? ? ? }
? ? ? ? ? ? for (int i = 0; i < 9; i++) {
? ? ? ? ? ? ? ? bb.drawLine(chessWidth * (i + 1), chessWidth, chessWidth * (i + 1), chessWidth * 10, paint);//畫出豎線 ? ?-- 10列
? ? ? ? ? ? }
? ? ? ? ? ? //畫士的地方斜線
? ? ? ? ? ? bb.drawLine(4 * chessWidth, 1 * chessWidth, 6 * chessWidth, chessWidth * 3, paint);
? ? ? ? ? ? bb.drawLine(4 * chessWidth, 3 * chessWidth, 6 * chessWidth, chessWidth * 1, paint);
? ? ? ? ? ? bb.drawLine(4 * chessWidth, 8 * chessWidth, 6 * chessWidth, chessWidth * 10, paint);
? ? ? ? ? ? bb.drawLine(4 * chessWidth, 10 * chessWidth, 6 * chessWidth, chessWidth * 8, paint);
? ? ? ? ? ? //畫兵線 -- ?炮線 ? ?---- 算的代碼寫的雜,也可以忽略
? ? ? ? ? ? drawDetails(bb);

? ? ? ? ? ? //畫楚河漢界 ? ? ---- 這里只是簡單的畫下
? ? ? ? ? ? paint.setColor(Color.WHITE);
? ? ? ? ? ? bb.drawRect(chessWidth + widthCenter, 5 * chessWidth + widthCenter, 9 * chessWidth - widthCenter, 6 * chessWidth - widthCenter, paint);
? ? ? ? ? ? paint.setColor(Color.parseColor("#D1BD92"));
? ? ? ? ? ? String text = "楚河 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?漢界";
? ? ? ? ? ? Rect rect = new Rect();
? ? ? ? ? ? paint.getTextBounds(text, 0, text.length(), rect);
? ? ? ? ? ? bb.drawText(text, 6 * chessWidth - chessWidth - rect.width() / 2, 6 * chessWidth - chessWidth / 2 + rect.height() / 2 - 5, paint);
? ? ? ? ? ? bb.save(Canvas.ALL_SAVE_FLAG);
? ? ? ? ? ? bb.restore();
? ? ? ? ? ? canvas.drawBitmap(boardBitmap, 0, 0, paint);

? ? ? ? } else {
? ? ? ? ? ? Log.d("tag", "boardBitmap不為空");
? ? ? ? ? ? canvas.drawBitmap(boardBitmap, 0, 0, paint);
? ? ? ? }
? ? }

? ? private void drawDetails(Canvas bb) {
? ? ? ? int x1, x2, y1, y2;
? ? ? ? for (int i = 0; i < 4; i++) {
? ? ? ? ? ? x1 = chessWidth + space;
? ? ? ? ? ? x2 = chessWidth + space;
? ? ? ? ? ? y1 = 4 * chessWidth - line - space;
? ? ? ? ? ? y2 = 4 * chessWidth - space;
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x1, y1, i * 2 * chessWidth + x2, y2, paint); ? //豎線
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x2, y2, i * 2 * chessWidth + x2 + line, y2, paint); ?//橫線
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x2, y2 + 2 * space, i * 2 * chessWidth + x2 + line, y2 + 2 * space, paint); ?//橫線
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x1, y2 + 2 * space, i * 2 * chessWidth + x2, y2 + 2 * space + line, paint); ? //豎線

? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x1, y1 + 3 * chessWidth, i * 2 * chessWidth + x2, y2 + 3 * chessWidth, paint);//上面向下平移3*chessWidth Y軸加平移值
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x2, y2 + 3 * chessWidth, i * 2 * chessWidth + x2 + line, y2 + 3 * chessWidth, paint);
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x2, y2 + 2 * space + 3 * chessWidth, i * 2 * chessWidth + x2 + line, y2 + 2 * space + 3 * chessWidth, paint);
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x1, y2 + 2 * space + 3 * chessWidth, i * 2 * chessWidth + x2, y2 + 2 * space + line + 3 * chessWidth, paint);

? ? ? ? ? ? x1 = 3 * chessWidth - space;
? ? ? ? ? ? x2 = 3 * chessWidth - space;
? ? ? ? ? ? y1 = 4 * chessWidth - line - space;
? ? ? ? ? ? y2 = 4 * chessWidth - space;
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x1, y1, i * 2 * chessWidth + x2, y2, paint); ? //豎線
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x2, y2, i * 2 * chessWidth + x2 - line, y2, paint); ?//橫線
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x2, y2 + 2 * space, i * 2 * chessWidth + x2 - line, y2 + 2 * space, paint); ?//橫線
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x1, y2 + 2 * space, i * 2 * chessWidth + x2, y2 + 2 * space + line, paint); ? //豎線

? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x1, y1 + 3 * chessWidth, i * 2 * chessWidth + x2, y2 + 3 * chessWidth, paint); ? //豎線
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x2, y2 + 3 * chessWidth, i * 2 * chessWidth + x2 - line, y2 + 3 * chessWidth, paint); ?//橫線
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x2, y2 + 2 * space + 3 * chessWidth, i * 2 * chessWidth + x2 - line, y2 + 2 * space + 3 * chessWidth, paint); ?//橫線
? ? ? ? ? ? bb.drawLine(i * 2 * chessWidth + x1, y2 + 2 * space + 3 * chessWidth, i * 2 * chessWidth + x2, y2 + 2 * space + line + 3 * chessWidth, paint); ? //豎線

? ? ? ? }
? ? ? ? //畫炮線
? ? ? ? x1 = 2 * chessWidth + space;
? ? ? ? x2 = 2 * chessWidth + space;
? ? ? ? y1 = 3 * chessWidth - line - space;
? ? ? ? y2 = 3 * chessWidth - space;
? ? ? ? bb.drawLine(x1, y1, x2, y2, paint); ? //豎線
? ? ? ? bb.drawLine(x2, y2, x2 + line, y2, paint); ?//橫線
? ? ? ? bb.drawLine(x2, y2 + 2 * space, x2 + line, y2 + 2 * space, paint); ?//橫線
? ? ? ? bb.drawLine(x1, y2 + 2 * space, x2, y2 + 2 * space + line, paint); ? //豎線
? ? ? ? bb.drawLine(x1 - 2 * space, y1, x2 - 2 * space, y2, paint);//豎線
? ? ? ? bb.drawLine(x2 - 2 * space - line, y2, x2 + line - 2 * space - line, y2, paint); ?//橫線
? ? ? ? bb.drawLine(x2 - 2 * space - line, y2 + 2 * space, x2 + line - 2 * space - line, y2 + 2 * space, paint); ?//橫線
? ? ? ? bb.drawLine(x1 - 2 * space, y2 + 2 * space, x2 - 2 * space, y2 + 2 * space + line, paint); ? //豎線

? ? ? ? bb.drawLine(x1, y1 + 5 * chessWidth, x2, y2 + 5 * chessWidth, paint); ? //豎線 ? ? ?----統(tǒng)一向下移動5*chessWidth
? ? ? ? bb.drawLine(x2, y2 + 5 * chessWidth, x2 + line, y2 + 5 * chessWidth, paint); ?//橫線
? ? ? ? bb.drawLine(x2, y2 + 2 * space + 5 * chessWidth, x2 + line, y2 + 2 * space + 5 * chessWidth, paint); ?//橫線
? ? ? ? bb.drawLine(x1, y2 + 2 * space + 5 * chessWidth, x2, y2 + 2 * space + line + 5 * chessWidth, paint); ? //豎線
? ? ? ? bb.drawLine(x1 - 2 * space, y1 + 5 * chessWidth, x2 - 2 * space, y2 + 5 * chessWidth, paint);//豎線
? ? ? ? bb.drawLine(x2 - 2 * space - line, y2 + 5 * chessWidth, x2 + line - 2 * space - line, y2 + 5 * chessWidth, paint); ?//橫線
? ? ? ? bb.drawLine(x2 - 2 * space - line, y2 + 2 * space + 5 * chessWidth, x2 + line - 2 * space - line, y2 + 2 * space + 5 * chessWidth, paint); ?//橫線
? ? ? ? bb.drawLine(x1 - 2 * space, y2 + 2 * space + 5 * chessWidth, x2 - 2 * space, y2 + 2 * space + line + 5 * chessWidth, paint); ? //豎線

? ? ? ? bb.drawLine(6 * chessWidth + x1, y1, 6 * chessWidth + x2, y2, paint); ? //豎線 ? ? ?---- 統(tǒng)一向右移動6*chessWidth+
? ? ? ? bb.drawLine(6 * chessWidth + x2, y2, 6 * chessWidth + x2 + line, y2, paint); ?//橫線
? ? ? ? bb.drawLine(6 * chessWidth + x2, y2 + 2 * space, 6 * chessWidth + x2 + line, y2 + 2 * space, paint); ?//橫線
? ? ? ? bb.drawLine(6 * chessWidth + x1, y2 + 2 * space, 6 * chessWidth + x2, y2 + 2 * space + line, paint); ? //豎線
? ? ? ? bb.drawLine(6 * chessWidth + x1 - 2 * space, y1, 6 * chessWidth + x2 - 2 * space, y2, paint);//豎線
? ? ? ? bb.drawLine(6 * chessWidth + x2 - 2 * space - line, y2, 6 * chessWidth + x2 + line - 2 * space - line, y2, paint); ?//橫線
? ? ? ? bb.drawLine(6 * chessWidth + x2 - 2 * space - line, y2 + 2 * space, 6 * chessWidth + x2 + line - 2 * space - line, y2 + 2 * space, paint); ?//橫線
? ? ? ? bb.drawLine(6 * chessWidth + x1 - 2 * space, y2 + 2 * space, 6 * chessWidth + x2 - 2 * space, y2 + 2 * space + line, paint); ? //豎線

? ? ? ? bb.drawLine(6 * chessWidth + x1, y1 + 5 * chessWidth, 6 * chessWidth + x2, y2 + 5 * chessWidth, paint); ? //豎線 ? ? ?----統(tǒng)一向右移動6*chessWidth
? ? ? ? bb.drawLine(6 * chessWidth + x2, y2 + 5 * chessWidth, 6 * chessWidth + x2 + line, y2 + 5 * chessWidth, paint); ?//橫線
? ? ? ? bb.drawLine(6 * chessWidth + x2, y2 + 2 * space + 5 * chessWidth, 6 * chessWidth + x2 + line, y2 + 2 * space + 5 * chessWidth, paint); ?//橫線
? ? ? ? bb.drawLine(6 * chessWidth + x1, y2 + 2 * space + 5 * chessWidth, 6 * chessWidth + x2, y2 + 2 * space + line + 5 * chessWidth, paint); ? //豎線
? ? ? ? bb.drawLine(6 * chessWidth + x1 - 2 * space, y1 + 5 * chessWidth, 6 * chessWidth + x2 - 2 * space, y2 + 5 * chessWidth, paint);//豎線
? ? ? ? bb.drawLine(6 * chessWidth + x2 - 2 * space - line, y2 + 5 * chessWidth, 6 * chessWidth + x2 + line - 2 * space - line, y2 + 5 * chessWidth, paint); ?//橫線
? ? ? ? bb.drawLine(6 * chessWidth + x2 - 2 * space - line, y2 + 2 * space + 5 * chessWidth, 6 * chessWidth + x2 + line - 2 * space - line, y2 + 2 * space + 5 * chessWidth, paint); ?//橫線
? ? ? ? bb.drawLine(6 * chessWidth + x1 - 2 * space, y2 + 2 * space + 5 * chessWidth, 6 * chessWidth + x2 - 2 * space, y2 + 2 * space + line + 5 * chessWidth, paint); ? //豎線
? ? }

? ? private void canvasChess(Canvas canvas) {
? ? ? ? for (int y = 0; y < 10; y++) {
? ? ? ? ? ? for (int x = 0; x < 9; x++) {
? ? ? ? ? ? ? ? if (Rules.chessValue(x, y) <= 0 || Rules.chessValue(x, y) > 14) {
? ? ? ? ? ? ? ? ? ? if (Rules.chessValue(x, y) < 0)
? ? ? ? ? ? ? ? ? ? ? ? Log.e("tag", "hava a bug");
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? drawChess(canvas, x, y);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? int textWidth;
? ? int textHeight;

? ? private void drawChess(Canvas canvas, int x, int y) { ? //這里是畫棋子的地方 ? --- 自己可以改動,例如:相 換-->象
? ? ? ? //紅方:1車 ?2馬 ?3相 ?4士 ?5帥 ?6炮 7兵
? ? ? ? //黑方:14車 13馬 12相 11士 10帥 9炮 8兵
? ? ? ? String text = "車";
? ? ? ? Paint chessPaint = null;
? ? ? ? if (Rules.chessValue(x, y) < 8) {//紅方
? ? ? ? ? ? switch (Rules.chessValue(x, y)) {
? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? text = "車";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? text = "馬";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? text = "相";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? text = "士";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? ? ? text = "帥";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 6:
? ? ? ? ? ? ? ? ? ? text = "炮";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 7:
? ? ? ? ? ? ? ? ? ? text = "兵";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? chessPaint = redPaint;
? ? ? ? } else {
? ? ? ? ? ? switch (15 - Rules.chessValue(x, y)) { ? ?//黑方
? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? text = "車";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? text = "馬";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? text = "相";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? text = "士";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? ? ? text = "帥";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 6:
? ? ? ? ? ? ? ? ? ? text = "炮";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 7:
? ? ? ? ? ? ? ? ? ? text = "兵";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? chessPaint = blackPaint;
? ? ? ? }
? ? ? ? if (textHeight == 0 || textWidth == 0) {
? ? ? ? ? ? Rect rect = new Rect();
? ? ? ? ? ? redPaint.getTextBounds(text, 0, text.length(), rect);
? ? ? ? ? ? textWidth = rect.width();//文字寬
? ? ? ? ? ? textHeight = rect.height() - 10;//文字高 --- 高度相對來說有一定的對不上,需要進行小調(diào)整
? ? ? ? }

? ? ? ? x += 1;
? ? ? ? y += 1;
? ? ? ? paint.setColor(Color.parseColor("#D1BD92"));
? ? ? ? canvas.drawCircle(chessWidth * x, chessWidth * y, chessWidth / 2, paint);
// ? ? ? ?canvas.drawRect(chessWidth*j-textWidth/2,chessWidth*i-textHeight/2,chessWidth*j+textWidth/2,chessWidth*i+textHeight/2,paint);
? ? ? ? canvas.drawText(text, chessWidth * x - textWidth / 2, chessWidth * y + textHeight / 2, chessPaint);
? ? }

}

象棋可移動規(guī)則類:

public class Rules {


? ? /**
? ? ?* 棋盤(9,8),當(dāng)前的位置(x,y),下一步的位置(x,y)
? ? ?* return 0為超出棋盤外 ?1走的是原地 ?2 下一步走的是自己的棋子上 3 移動不符合規(guī)則
? ? ?* 4 可移動到下一步
? ? ?*/

? ? public static class Config {
? ? ? ? public static int chessWidth = 0; ? //棋子寬度設(shè)置
? ? }

? ? public static int[][] chessBoard = ?//棋盤棋子的分布 {9}[8] ? ?10橫9列
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? {1, 2, 3, 4, 5, 4, 3, 2, 1},
? ? ? ? ? ? ? ? ? ? {0, 0, 0, 0, 0, 0, 0, 0, 0},
? ? ? ? ? ? ? ? ? ? {0, 6, 0, 0, 0, 0, 0, 6, 0},
? ? ? ? ? ? ? ? ? ? {7, 0, 7, 0, 7, 0, 7, 0, 7},
? ? ? ? ? ? ? ? ? ? {0, 0, 0, 0, 0, 0, 0, 0, 0}, ?//紅方棋子分布 ? ?//不該有紅黑之分,如果有換棋功能,可能雙方會換顏色,但是這個棋盤值不該變,不然會有系列問題
? ? ? ? ? ? ? ? ? ? {0, 0, 0, 0, 0, 0, 0, 0, 0}, ?//黑方棋子分布
? ? ? ? ? ? ? ? ? ? {8, 0, 8, 0, 8, 0, 8, 0, 8},
? ? ? ? ? ? ? ? ? ? {0, 9, 0, 0, 0, 0, 0, 9, 0},
? ? ? ? ? ? ? ? ? ? {0, 0, 0, 0, 0, 0, 0, 0, 0},
? ? ? ? ? ? ? ? ? ? {14, 13, 12, 11, 10, 11, 12, 13, 14},
? ? ? ? ? ? };
? ? public static int non_win = 0;
? ? public static int up_win = 1; ? ? ? //不應(yīng)該有紅,黑之分 ?應(yīng)該只有上和下之分要好點
? ? public static int down_win = 2;

? ? public static int win() { ? //判斷輸贏
? ? ? ? int x = 0, y = 0;
? ? ? ? for (int i = 0; i < 10; i++) {
? ? ? ? ? ? for (int j = 0; j < 9; j++) {
? ? ? ? ? ? ? ? if (chessBoard[i][j] == 5) {
? ? ? ? ? ? ? ? ? ? x = 1;
? ? ? ? ? ? ? ? } else if (chessBoard[i][j] == 10) {
? ? ? ? ? ? ? ? ? ? y = 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (x == y) {
? ? ? ? ? ? return non_win;
? ? ? ? } else if (x == 1 && y == 0) {
? ? ? ? ? ? return up_win;
? ? ? ? } else {
? ? ? ? ? ? return down_win;
? ? ? ? }
? ? }

? ? // ? 序列
// ? ?{1, ? 2, ?3, ?4, ?5, ?4, ?3, ?2, ?1},
// ? ?{0, ? 0, ?0, ?0, ?0, ?0, ?0, ?0, ?0},
// ? ?{0, ? 6, ?0, ?0, ?0, ?0, ?0, ?6, ?0},
// ? ?{7, ? 0, ?7, ?0, ?7, ?0, ?7, ?0, ?7},
// ? ?{0, ? 0, ?0, ?0, ?0, ?0, ?0, ?0, ?0},
// ? ?{0, ? 0, ?0, ?0, ?0, ?0, ?0, ?0, ?0},
// ? ?{8, ? 0, ?8, ?0, ?8, ?0, ?8, ?0, ?8},
// ? ?{0, ? 9, ?0, ?0, ?0, ?0, ?0, ?9, ?0},
// ? ?{0, ? 0, ?0, ?0, ?0, ?0, ?0, ?0, ?0},
// ? ?{14, 13, 12, 11, 10, 11, 12, 13, 14},
? ? public static int chessValue(int positionX, int positionY) { ? ?//坐標(biāo)轉(zhuǎn)數(shù)組值
? ? ? ? return chessBoard[positionY][positionX];
? ? }
? ? public static void moveChess(int fromX, int fromY, int toX, int toY) {
? ? ? ? chessBoard[toY][toX] = chessValue(fromX,fromY);
? ? ? ? chessBoard[fromY][fromX] = 0;
? ? }
? ? public static boolean canMove(int fromX, int fromY, int toX, int toY) {//0,3 0,4
//TODO 起始位置到目的位置fromX,fromY, toX, toY ?這個為坐標(biāo)位置 ----注意轉(zhuǎn)換為棋子對應(yīng)的數(shù)組要注意 例如:坐標(biāo)位x,y --> 棋子=chessBoard[y][x]
? ? ? ? if (toX > 8 || toY > 9 || toX < 0 || toY < 0) { ? ?//超出棋盤范圍
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? if (fromX == toX && fromY == toY) { ? ? //走的是原地
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? if (chessBoard[fromY][fromX] > 7) { ? ?//一方棋子走的位置是自己棋子的位置
? ? ? ? ? ? if (chessBoard[toY][toX] > 7) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? } else if (chessBoard[fromY][fromX] > 0) {
? ? ? ? ? ? if (chessBoard[toY][toX] < 8 &&chessValue(toX,toY)!=0) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? switch (chessBoard[fromY][fromX]) {
? ? ? ? ? ? case 1: //車
? ? ? ? ? ? case 14:
? ? ? ? ? ? ? ? if (Math.abs(toY - fromY) > 0 && Math.abs(toX - fromX) == 0) {//走的豎線
? ? ? ? ? ? ? ? ? ? if (toY > fromY) {
? ? ? ? ? ? ? ? ? ? ? ? for (int i = fromY + 1; i < toY; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[i][fromX] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? for (int i = toY + 1; i < fromY; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[i][fromX] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? } else if (Math.abs(toX - fromX) > 0 && Math.abs(toY - fromY) == 0) { //走的橫線
? ? ? ? ? ? ? ? ? ? if (toX > fromX) {
? ? ? ? ? ? ? ? ? ? ? ? for (int i = fromX + 1; i < toX; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[fromY][i] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? for (int i = toX + 1; i < fromX; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[fromY][i] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 2: //馬
? ? ? ? ? ? case 13:
? ? ? ? ? ? ? ? if (Math.abs(toY - fromY) == 2 && Math.abs(toX - fromX) == 1) {
? ? ? ? ? ? ? ? ? ? int centerY = (toY + fromY) / 2;
? ? ? ? ? ? ? ? ? ? if (chessBoard[centerY][fromX] != 0) {//馬蹄處有棋子
? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? } else if (Math.abs(toY - fromY) == 1 && Math.abs(toX - fromX) == 2) {
? ? ? ? ? ? ? ? ? ? int centerX = (toX + fromX) / 2;
? ? ? ? ? ? ? ? ? ? if (chessBoard[fromY][centerX] != 0) {//馬蹄處有棋子
? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 3: //相
? ? ? ? ? ? ? ? if (toY > 4) {//過河了
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? } else if (Math.abs(toY - fromY) == 2 && Math.abs(toX - fromX) == 2) { ?//走"田"字
? ? ? ? ? ? ? ? ? ? int centerY = (toY + fromY) / 2;
? ? ? ? ? ? ? ? ? ? int centerX = (toX + fromX) / 2;
? ? ? ? ? ? ? ? ? ? if (chessBoard[centerY][centerX] != 0) {// 象眼處有棋子
? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 12:
? ? ? ? ? ? ? ? if (toY < 5) {//過河了
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? } else if (Math.abs(toY - fromY) == 2 && Math.abs(toX - fromX) == 2) { ?//走"田"字
? ? ? ? ? ? ? ? ? ? int centerY = (toY + fromY) / 2;
? ? ? ? ? ? ? ? ? ? int centerX = (toX + fromX) / 2;
? ? ? ? ? ? ? ? ? ? if (chessBoard[centerY][centerX] != 0) {// 象眼處有棋子
? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 4: //士
? ? ? ? ? ? ? ? if (toY > 2 || toX < 3 || toX > 5) { //出了九宮格
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? } else if (Math.abs(toX - fromX) == 1 && Math.abs(toY - fromY) == 1) {//走斜線,直走一格
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 11:
? ? ? ? ? ? ? ? if (toY < 7 || toX < 3 || toX > 5) { //出了九宮格
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? } else if (Math.abs(toX - fromX) == 1 && Math.abs(toY - fromY) == 1) {//走斜線,直走一格
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? //帥
? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? if (toY > 2 || toX < 3 || toX > 5) {//出了九宮格
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? } else if ((Math.abs(toX - fromX) + Math.abs(toY - fromY)) == 1) {//只能走一格
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 10:
? ? ? ? ? ? ? ? if (toY < 7 || toX < 3 || toX > 5) {//出了九宮格
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? } else if ((Math.abs(toX - fromX) + Math.abs(toY - fromY)) == 1) {//只能走一格
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 6: //炮
? ? ? ? ? ? case 9:
? ? ? ? ? ? ? ? int count = 0;
? ? ? ? ? ? ? ? if (chessBoard[toY][toX] == 0) { ? ?//到的位置是空位置
? ? ? ? ? ? ? ? ? ? if (Math.abs(toY - fromY) > 0 && (toX - fromX) == 0) {//走的豎線
? ? ? ? ? ? ? ? ? ? ? ? if (toY > fromY) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = fromY + 1; i < toY; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[i][fromX] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = toY + 1; i < fromY; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[i][fromX] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? } else if (Math.abs(toX - fromX) > 0 && Math.abs(toY - fromY) == 0) {//走的橫線
? ? ? ? ? ? ? ? ? ? ? ? if (toX > fromX) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = fromX + 1; i < toX; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[fromY][i] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = toX + 1; i < fromX; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[fromY][i] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }else { //到的位置是有子的
? ? ? ? ? ? ? ? ? ? if (Math.abs(toY - fromY) > 0 && (toX - fromX) == 0) {//走的豎線
? ? ? ? ? ? ? ? ? ? ? ? if (toY > fromY) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = fromY + 1; i < toY; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[i][fromX] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = toY + 1; i < fromY; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[i][fromX] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? } else if (Math.abs(toX - fromX) > 0 && Math.abs(toY - fromY) == 0) {//走的橫線
? ? ? ? ? ? ? ? ? ? ? ? if (toX > fromX) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = fromX + 1; i < toX; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[fromY][i] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = toX - 1; i > fromX; i--) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chessBoard[fromY][i] != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (count == 1) { ?//如果中間只有一個棋子間隔就可以
? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 7: //兵
? ? ? ? ? ? ? ? if ((toY - fromY) < 0) {//后退
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? } else if (fromY > 4) {//過了河
? ? ? ? ? ? ? ? ? ? if ((Math.abs(toX - fromX) + Math.abs(toY - fromY)) == 1) { //只能走一格
? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } else {//沒過河
? ? ? ? ? ? ? ? ? ? if (Math.abs(toY - fromY) == 1 && fromX == toX) { ? ?//只能往前走
? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 8:
? ? ? ? ? ? ? ? if ((toY - fromY) > 0) {//后退
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? } else if (fromY <= 4) {//過了河
? ? ? ? ? ? ? ? ? ? if ((Math.abs(toX - fromX) + Math.abs(toY - fromY)) == 1) { //只能走一格
? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } else {//沒過河
? ? ? ? ? ? ? ? ? ? if (Math.abs(toY - fromY) == 1 && fromX == toX) { ? ?//只能往前走
? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default: ? ?//如果為其他值是不允許移動
? ? ? ? ? ? ? ? break;
? ? ? ? }

? ? ? ? return false;
? ? }

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android使用Opengl錄像時添加水印

    Android使用Opengl錄像時添加水印

    這篇文章主要為大家詳細介紹了Android使用Opengl錄像時添加水印,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • android實現(xiàn)簡單圓弧效果

    android實現(xiàn)簡單圓弧效果

    這篇文章主要為大家詳細介紹了android實現(xiàn)簡單圓弧效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • Android內(nèi)部存儲與外部存儲的示例講解

    Android內(nèi)部存儲與外部存儲的示例講解

    內(nèi)部存儲和外部存儲的概念隨著Android版本的更新也在發(fā)生不斷的變化。最早的內(nèi)部存儲指的是系統(tǒng)自帶的ROM存儲,外部存儲指的是外置的Sdcard或者通過OTG掛在的USB存儲
    2023-03-03
  • Android自定義彈框Dialog效果

    Android自定義彈框Dialog效果

    這篇文章主要為大家詳細介紹了Android自定義彈框Dialog效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 利用adt-bundle輕松搭建Android開發(fā)環(huán)境與Hello world(Windows)

    利用adt-bundle輕松搭建Android開發(fā)環(huán)境與Hello world(Windows)

    這篇文章主要介紹了利用adt-bundle在Windows下輕松搭建Android開發(fā)環(huán)境與Hello world,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 淺談Android中AsyncTask的工作原理

    淺談Android中AsyncTask的工作原理

    AsyncTask是Android本身提供的一種輕量級的異步任務(wù)類。它可以在線程池中執(zhí)行后臺任務(wù),然后把執(zhí)行的進度和最終的結(jié)果傳遞給主線程更新UI。本文將介紹Android中AsyncTask的工作原理。
    2021-06-06
  • Android利用DownloadManager實現(xiàn)文件下載

    Android利用DownloadManager實現(xiàn)文件下載

    這篇文章主要為大家詳細介紹了Android利用DownloadManager實現(xiàn)文件下載,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • android實現(xiàn)banner輪播圖無限輪播效果

    android實現(xiàn)banner輪播圖無限輪播效果

    這篇文章主要為大家詳細介紹了android實現(xiàn)banner輪播圖無限輪播效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 利用百度地圖Android sdk高仿微信發(fā)送位置功能及遇到的問題

    利用百度地圖Android sdk高仿微信發(fā)送位置功能及遇到的問題

    這篇文章給大家介紹了利用百度地圖Android sdk高仿微信發(fā)送位置功能,在實現(xiàn)此功能的時候遇到點小問題,下面小編給大家列出來,需要的朋友參考下吧
    2017-12-12
  • Android繪制炫酷的引導(dǎo)界面

    Android繪制炫酷的引導(dǎo)界面

    這篇文章主要為大家詳細介紹了Android繪制炫酷引導(dǎo)界面的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02

最新評論