最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

主要是實現(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; //當前走棋的棋子
? ? 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);
? ? ? ? //設置去鋸齒
? ? ? ? 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 { ?//已經(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; ? ?//楚河漢界中間是空白的,需要調整一些不能讓棋盤不好看
? ? 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;//文字高 --- 高度相對來說有一定的對不上,需要進行小調整
? ? ? ? }

? ? ? ? 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),當前的位置(x,y),下一步的位置(x,y)
? ? ?* return 0為超出棋盤外 ?1走的是原地 ?2 下一步走的是自己的棋子上 3 移動不符合規(guī)則
? ? ?* 4 可移動到下一步
? ? ?*/

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

? ? 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; ? ? ? //不應該有紅,黑之分 ?應該只有上和下之分要好點
? ? 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) { ? ?//坐標轉數(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 ?這個為坐標位置 ----注意轉換為棋子對應的數(shù)組要注意 例如:坐標位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;
? ? }

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android編程實現(xiàn)監(jiān)聽EditText變化的方法

    Android編程實現(xiàn)監(jiān)聽EditText變化的方法

    這篇文章主要介紹了Android編程實現(xiàn)監(jiān)聽EditText變化的方法,涉及Android針對EditText的相關操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Flutter實現(xiàn)底部導航

    Flutter實現(xiàn)底部導航

    這篇文章主要為大家詳細介紹了Flutter實現(xiàn)底部導航的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • Android 橫豎屏處理的知識小結

    Android 橫豎屏處理的知識小結

    這篇文章主要介紹了Android 橫豎屏處理的知識小結,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Android智能指針輕量級Light Pointer初識

    Android智能指針輕量級Light Pointer初識

    這篇文章主要為大家介紹了Android智能指針輕量級Light Pointer初識詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android實現(xiàn)淘寶購物車

    Android實現(xiàn)淘寶購物車

    這篇文章主要為大家詳細介紹了Android實現(xiàn)淘寶購物車,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android開發(fā)筆記之:Dialog的使用詳解

    Android開發(fā)筆記之:Dialog的使用詳解

    本篇文章是對Android中Dialog的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Android?MPAndroidChart繪制原理

    Android?MPAndroidChart繪制原理

    這篇文章主要介紹了Android?MPAndroidChart繪制原理,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • Android開發(fā)使用Activity嵌套多個Fragment實現(xiàn)橫豎屏切換功能的方法

    Android開發(fā)使用Activity嵌套多個Fragment實現(xiàn)橫豎屏切換功能的方法

    這篇文章主要介紹了Android開發(fā)使用Activity嵌套多個Fragment實現(xiàn)橫豎屏切換功能的方法,結合實例形式分析了Android使用Activity嵌套多個Fragment進行橫豎屏切換的原理與具體操作技巧,需要的朋友可以參考下
    2017-11-11
  • Android處理視圖圓角和色彩的工具類

    Android處理視圖圓角和色彩的工具類

    這篇文章主要為大家詳細介紹了Android處理視圖圓角和色彩的工具類,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • android用PopWindow做下拉框實例代碼

    android用PopWindow做下拉框實例代碼

    本篇文章主要介紹了android用PopWindow做下拉框實例代碼,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01

最新評論

河北区| 山丹县| 安庆市| 唐山市| 疏勒县| 丰都县| 隆尧县| 达州市| 田阳县| 万全县| 天台县| 溧阳市| 阳新县| 株洲市| 宿松县| 三河市| 搜索| 舒城县| 东明县| 海安县| 三亚市| 渭南市| 巫溪县| 奇台县| 丰都县| 鹿邑县| 淮安市| 沙雅县| 辽源市| 潮州市| 连山| 浏阳市| 永吉县| 怀化市| 南平市| 福泉市| 尖扎县| 湖北省| 上虞市| 茶陵县| 南投市|