1. 程式人生 > >android象棋簡單實現

android象棋簡單實現

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

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; } @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); //畫棋盤 --- 可以優化一下,儲存一張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); //豎線 ----統一向下移動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); //豎線 ---- 統一向右移動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); //豎線 ----統一向右移動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); } }

象棋可移動規則類:

public class Rules {


    /**
     * 棋盤(9,8),當前的位置(x,y),下一步的位置(x,y)
     * return 0為超出棋盤外  1走的是原地  2 下一步走的是自己的棋子上 3 移動不符合規則
     * 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) {    //座標轉陣列值
        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  這個為座標位置 ----注意轉換為棋子對應的陣列要注意 例如:座標位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) { //出了九宮格
                    
            
           

相關推薦

android象棋簡單實現

主要是實現兩人對戰象棋,沒有實現人機對戰,主要不會判斷下一步棋走那個好,或者對每下一步棋進行打分而進行選擇最高分進而走哪一步棋 Activity類: public class ChessActivity extends Activity { @Ove

Android簡單實現DrawerLayout

<android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/re

℃江讓您從精通到入門:Android Studio 簡單實現ViewPager,可做APP操作提示

前期準備,如下圖: 第一步、先書寫佈局檔案:activity_main.xml檔案如下: <?xml version="1.0" encoding="utf-8"?> <Re

Android簡單實現使用WebView元件實現在App內開啟web

關於WebView元件 如何實現在App內嵌web 在新建的android專案裡,如果要實現內嵌Web,一定要在AndroidManifest.xml設定訪問網路許可權: <uses-permission android:name="andr

Android簡單實現listview上下拉伸回彈動畫效果

超簡單實現listview上下拉伸回彈動畫效果 希望能幫到大家,共同進步 import android.annotation.SuppressLint; import android.content.Context; import android.uti

MRouter-Android路由簡單實現

路由的意義: 1. 模組間解耦,不能在程式碼中寫死Activity類名。 2. 動態配置業務需求,現在都是業務模組化開發了。 1. 註解 我們這次編寫的MRoute主要使用了編譯時註解技術,註解在我們日常使用的框架中都有體現。 執行

Android簡單實現廣告輪播效果ConvenientBanner

以前一直寫廣告輪播,都是自己實現,最近看帖子的時候,看到,有一個很不錯的廣告輪播第三發框架,趕快學習一下。 先上效果圖(不知道為啥效果圖,畫素這麼低) 1.首先新增依賴 ompile 'com.bigkoo:convenientbanner:2

Android簡單實現將手機圖片上傳到server中

sdk etc mov 創建 ast bmi 以及 lena ews 在本例中。將會簡單的實現安卓手機將圖片上傳到server中。本例使用到了 server端:PHP+APACHE 客戶端:JAVA 先簡單實現一下server端的上傳並測試上傳

Unity(Android版)和Android原生APP簡單實現版本更新

directory 代碼 server 頁面 提示框 自動安裝 obj nis 查看 版本檢測接口說明:(1)請求post,無參數(2)調用地址:http://www.baidu.com/rs/ver/info(3)返回結果:{ "verCode": "2",

安卓音、視頻播放功能簡單實現 --Android基礎

ket undle 顯示 perm err efault 繼續 bre mpat 1、音樂播放功能 關鍵代碼: MainActivity.java: package thonlon.example.cn.musicdemowithoutservice;import

Android 進階(五)》應用分享簡單實現-LazyApkShare

LazyApkShare 分享當前應用的簡單實現。 開源地址 LazyApkShare 新增依賴 Gradle 步驟一. 根目錄下build.gradle allprojects { repositories { maven { url

Android app版本升級的一個簡單實現

夢想會被現實磨滅,希望我能堅持的長久! 1升級原理 build.gradle 中 versionCode 1 , versionName “1.0.0” 是升級的關鍵,versionCode是個int,versionName是個String,其中versionCode每次要升級版

Android 商品分類(簡單實現 僅供參考)

  //匯入依賴 implementation 'com.android.support:recyclerview-v7:28.0.0' //首先在activity頁面新增兩個水平的RecyclerView <?xml version="1.0" encoding="

通過aspectj對Android資料統計的簡單實現

功能需求 一個專案實現之後,我們並不知道使用者對某個部分的使用頻率是對少,為了更好的來對專案各個功能的使用統計,我們需要做一些資料埋點的功能,也就是每當使用者點選按鈕的時候,都對這次點選進行儲存處理,然後再之後統一上傳到伺服器,進行資料分析。 實現思路 條件 假如,當前有

Android中MQTT的簡單實現(只是連線到伺服器,未實現傳送、接受資訊)

1.新增mqtt包到gradle.build a.在project的gradle.build中新增地址(P:我下載的參考例子是不用新增的,但是我自己寫的時候不新增就編譯不過去) allprojects { repositories { google()

android簡單 實現 日曆,生日 原生簡單輕鬆搞定

先看下佈局    對應的一個輸入框點選觸發 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

android 輸入框 手機號 ,密碼 郵箱 ,網路,圖片格式等 判斷 工具類 封裝簡單實現

我們android 開發人員都會與到 登入註冊,郵箱驗證等問題 ,我們需要if判斷 ,簡單邏輯 比較好些,隨著產品的需求,格式越來越多,邏輯越來越複雜,下面 給大家推薦 一下自己封裝的一個工具類 裡面對應的正的表示式等多個簡單實用 首先是一個工具類   public

android 判斷 有沒有網路 ,簡單實現

對應一個按鈕 ,點 public class WlActivity extends AppCompatActivity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) {

android 呼叫手機打電話 簡單實現

首先看下佈局xml ,我的事一個button按鈕 點選 打電話 ,手機號是自己定義,這個你可以根據自己邏輯寫 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas

Android高亮引導頁的簡單實現

       前段時間公司APP有需求做一個內部的高亮引導頁,我之所以說內部,因為外部也有引導頁(這不是廢話嘛,-_-||)。具體的樣式想必大家都很清楚,就是那種在某個頁面上進行操作指引的那種效果。本文著重說了內部引導頁,是為了區別於外部引導頁,外部引導頁就是那種啟動app時