广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现象棋游戏
  • 143
分享到

Android实现象棋游戏

2024-04-02 19:04:59 143人浏览 八月长安
摘要

本文实例为大家分享了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 {


    

    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) { //出了九宫格
                    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;
    }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Android实现象棋游戏

本文链接: https://www.lsjlt.com/news/148400.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android实现象棋游戏
    本文实例为大家分享了Android实现象棋游戏的具体代码,供大家参考,具体内容如下 主要是实现两人对战象棋,没有实现人机对战,主要不会判断下一步棋走那个好,或者对每下一步棋进行打分而...
    99+
    2022-11-13
  • python实现象棋游戏
    本文实例为大家分享了python实现象棋游戏的具体代码,供大家参考,具体内容如下 import math from turtle import * speed(0)   #调整画的...
    99+
    2022-11-11
  • Android实现中国象棋游戏(局域网版)
    本文实例为大家分享了Android实现中国象棋游戏的具体代码,供大家参考,具体内容如下 实现环境:  android studio 3.2.1, 手机分辨率为: 1920 ...
    99+
    2022-11-13
  • Java实现中国象棋游戏
    目录一、界面二、按钮三、加棋子四、实现棋子的移动五、判断胜负六、按钮“开始游戏”和“重新开始”的实现七、加规则八、轮次九、悔棋十、背景...
    99+
    2022-11-13
  • js实现中国象棋游戏
    本文实例为大家分享了js实现中国象棋游戏的具体代码,供大家参考,具体内容如下 使用table元素作表格,div元素作象棋。 效果如下: 代码如下: <html> <...
    99+
    2022-11-13
  • Java+Swing实现中国象棋游戏
    目录一、系统介绍1.开发环境2.技术选型3.系统功能二、系统展示三、部分代码一、系统介绍 1.开发环境 开发工具:Eclipse2021 JDK版本:jdk1.8 Mysql版本:8...
    99+
    2022-11-13
  • Android Studio实现五子棋小游戏
    项目目录 一、项目概述二、开发环境三、详细设计1、布局设计2、验证码3、AI人机4、背景音乐 四、运行演示五、项目总结六、源码获取 一、项目概述 五子棋是一种两人对弈的策略型棋类游戏,...
    99+
    2023-10-23
    android studio android ide 安卓app 移动应用开发
  • Python实现人机中国象棋游戏
    目录导语1.游戏规则&基本玩法1.1 基本玩法1.2 行棋规则2.素材文件3.主要代码3.1 Chinachess.py 为主文件3.2 Constants.py 数据常量3...
    99+
    2022-11-12
  • 基于Python实现有趣的象棋游戏
    目录导语一、游戏介绍二、游戏规则三、环境准备四、代码展示五、效果展示导语 一直以来,中国象棋都是中华民族的一种象征,当然也是人们最为喜感的一种娱乐方式。 在若干年前,人们都习惯于约上...
    99+
    2023-03-06
    Python实现象棋游戏 Python象棋游戏 Python象棋 Python游戏
  • 怎么用Java实现中国象棋游戏
    本篇内容介绍了“怎么用Java实现中国象棋游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、界面下棋的棋盘首先要准备好,这就是一个合适大...
    99+
    2023-06-30
  • C#实现围棋游戏
    本文实例为大家分享了C#实现围棋游戏的具体代码,供大家参考,具体内容如下 之所以选择围棋作为大作业一方面是想挑战一下,另一方面是由于从6岁学围棋到11岁放下,再到今天已将近8年了,也...
    99+
    2022-11-13
  • Qt实现棋盘游戏
    本文实例为大家分享了Qt实现棋盘游戏的具体代码,供大家参考,具体内容如下 知识点 1、画背景图、线条 2、qDebug()与QString联合使用 qDebug()<<Q...
    99+
    2022-11-12
  • Android实现五子棋游戏(局域网版)
    本文实例为大家分享了Android实现五子棋游戏的具体代码,供大家参考,具体内容如下 实现环境:  android studio 3.2.1, 手机分辨率为: 1920 *...
    99+
    2022-11-13
  • python实现井字棋游戏
    本文实例介绍了python实现井字棋游戏的方法,分享给大家,具体内容如下 windows7下python3.4.0编译运行通过。由于采用了cmd调用,所以与Linux不兼容,无法在Linux下运行。 游戏就...
    99+
    2022-06-04
    游戏 python 井字棋
  • python实现三子棋游戏
    目录一、基本流程二、基本步骤1、菜单界面2、初始化棋盘、打印棋盘3、玩家落子4、电脑落子5、输赢判定三、整体代码四、结果展示三子棋的python实现代码,供大家参考,具体内容如下 一...
    99+
    2022-11-10
  • Java实现五子棋游戏
    本文实例为大家分享了Java实现五子棋游戏的具体代码,供大家参考,具体内容如下 一、功能分析 五子棋的实现还是较为简单的,通过下期的流程我们可以知道大概要实现一下功能: 1、格界面 ...
    99+
    2022-11-12
  • C#实现飞行棋游戏
    飞行棋主要是讲的方法怎么应用,充分的去理解方法和方法的调用,整体收获还是很大的。 我想的是说一下整体的思路。在编程的时间里,逻辑是最重要的,先干嘛后干嘛,对吧。 直接上个飞行棋的图,...
    99+
    2022-11-12
  • java实现四子棋游戏
    非常简单的四子棋游戏 本人是刚学java的小白,最近在书上看到了有关四子棋游戏的编程题,就试着来写一写,代码也比较简单。 思路 写四子棋的难点是如何判断四个棋子连在一起。 下面给出图...
    99+
    2022-11-11
  • Java实现三子棋游戏
    本文实例为大家分享了Java实现三子棋游戏的具体代码,供大家参考,具体内容如下 一、题目要求 编写程序,实现简单的三子棋游戏。在三子棋中,双方在3×3的棋盘中轮流下棋,一...
    99+
    2022-11-13
  • JavaSE实现三子棋游戏
    目录main函数棋盘模块设计玩家模块设计AI(电脑)模块设计游戏模块设计本文实例为大家分享了JavaSE实现三子棋游戏的具体代码,供大家参考,具体内容如下 用面向对象的思维把实现三子...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作