java 实现《中国象棋》游戏

  • Post author:
  • Post category:java




简介:

中国象棋是起源于中国的一种棋,属于二人对抗性游戏的一种,在中国有着悠久的历史。由于用具简单,趣味性强,成为流行极为广泛的棋艺活动。

中国象棋是中国棋文化,也是中华民族的文化瑰宝,它源远流长,趣味浓厚,基本规则简明易懂。中国象棋在中国的群众基础远远超过围棋,是普及最广的棋类项目,中国象棋已流传到十几个国家和地区。

中国象棋使用方形格状棋盘,圆形棋子共有32个,红黑二色各有16个棋子,摆放和活动在交叉点上。双方交替行棋,先把对方的将(帅)“将死”的一方获胜。



主要需求

按照中国象棋的规则,实现红黑棋对战,两个玩家自己玩。



实现棋盘

在这里插入图片描述



项目目录结构

在这里插入图片描述



部分实现代码



画图类:
/**
 * @author gjt
 * @version 1.0.0
 * @description 画图类
 */
public class Canvas extends JPanel {

    public static final int ROW_COUNT = 10;//棋盘行数
    public static final int COLUMN_COUNT = 9;//棋盘列数
    private int[][] map = new int[ROW_COUNT][COLUMN_COUNT];
    private Font font;
    public static final int BLACK = 1000;
    public static final int WHITE = 0;

    private boolean isBlack = false; //当前是谁下子
    private int selectColumn = -1;
    private int selectRow = -1;
    private ChessManager manager = new ChessManager();
    private Image mainGif;

    public Canvas(){//棋盘为10行9列,默认就好,不用改
        font = new Font("宋体",Font.BOLD,30);
        initMap();
        addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                //super.mouseClicked(e);
                //System.out.println(e.getButton());
                if(e.getButton()==MouseEvent.BUTTON1){
                    int x = e.getX();
                    int y = e.getY();
                    //System.out.println(e.getX()+","+e.getY()+"["+x+","+y+"]");
                    int r = (y - 25) /58;
                    int c = (x - 25) /58;
                    //System.out.println("r = "+r+", c = "+c);
                    //选子
                    if(selectColumn == -1&&map[r][c]!=0){
                        if(RangeChecker.selectInRange(map,r,c,isBlack)){//isBlack 是 红方
                            selectColumn = c;
                            selectRow = r;
                            repaint();
                        }
                        else{
                            showFail("请选择己方棋子");
                        }
                        return ;// 选子阶段结束
                    }

                    boolean valid  = RangeChecker.inRange(map, selectRow, selectColumn,r, c, isBlack);
                    if(!valid){
                        showFail("下子失败");
                    }

                    Chess chess = manager.getChess(map[selectRow][selectColumn]);
                    valid = chess.check(map, selectRow, selectColumn, r, c, isBlack);
                    if(!valid||(map[selectRow][selectColumn]<1000&&map[r][c]<1000&&map[r][c]!=0)||(map[selectRow][selectColumn]>1000&&map[r][c]>1000&&map[r][c]!=0)){
                        showFail("下子失败,规则不允许");
                        selectColumn = -1;
                        selectRow = -1;
                        repaint();
                        return ;
                    }

                    map[r][c] = map[selectRow][selectColumn];
                    map[selectRow][selectColumn] = 0;
                    selectColumn = -1;
                    selectRow = -1;
                    isBlack = !isBlack;//对方来下
                    repaint();
                }
            }
        });
    }
    protected void showFail(String msg) {
        System.out.println(msg);
    }

    public void initMap(){
        //初始化棋盘
        map=new int [][]{{1000+'C',1000+'H',1000+'E',1000+'S',1000+'G',1000+'S',1000+'E',1000+'H',1000+'C'}, {0,0,0,0,0,0,0,0,0,},
                {0,1000+'P',0,0,0,0,0,1000+'P',0},{1000+'A',0,1000+'A',0,1000+'A',0,1000+'A',0,1000+'A'},
                {0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},
                {'A',0,'A',0,'A',0,'A',0,'A'},{0,'P',0,0,0,0,0,'P',0},
                {0,0,0,0,0,0,0,0,0},{'C','H','E','S','G','S','E','H','C'}};
        mainGif = ImageTools.loadImage("main.gif");
        manager.loadAllImage();
    }

    //内部类
    public static class MyPoint{
        public int r;
        public int c;
        public MyPoint(int r,int c) {
            this.r = r;
            this.c = c;
        }
    }

    @Override
    public void paint(Graphics g){
        super.paint(g);
        g.setFont(font);
        int w = this.getWidth();
        int h = this.getHeight();
        g.drawImage(mainGif, 0, 0,null);

        for(int r = 0;r<map.length;r++){
            for(int c = 0;c<map[r].length;c++){
                safelyDraw(g, manager.getImage(map[r][c]), 25+58*c, r*58+25);
            }
        }
        g.drawRect(25+58* selectColumn, 25+58* selectRow, 58, 58);
        g.drawString(isBlack?"黑方下棋":"红方下棋", 220 , 650);

    }

    public void safelyDraw(Graphics g,Image img,int x,int y){
        try {
            g.drawImage(img, x, y,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isBlack() {
        return isBlack;
    }

    public void setBlack(boolean black) {
        isBlack = black;
    }

    public void setSelectColumn(int selectColumn) {
        this.selectColumn = selectColumn;
    }

    public void setSelectRow(int selectRow) {
        this.selectRow = selectRow;
    }
}



项目启动类
/**
 * @author Mr.Gao
 * @version 1.0.0
 * @description 项目启动类
 */
public class ChessStarter {

    public static void main(String[] args){
        JFrame f =new JFrame("中国象棋(Gao作品)");
        ImageIcon icon = new ImageIcon("image" + File.separator + "red_e.gif");
        f.setIconImage(icon.getImage());

        f.setSize(730,750);
        f.setLocationRelativeTo(null);
        f.setLayout(new BorderLayout());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final Canvas canvas = new Canvas();
        f.add(canvas);

        JButton newStartGame = new JButton("新游戏");
        JButton startMusic = new JButton("播放音乐");
        JButton ExitGame = new JButton("退出游戏");

        //为新游戏按钮  添加事件
        newStartGame.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                canvas.initMap();//新游戏
                canvas.repaint();//新游戏后,出棋先后顺序互换
                canvas.setSelectColumn(-1);
                canvas.setSelectRow(-1);
                canvas.setBlack(!canvas.isBlack());
            }
        });

        //为退出按钮  添加事件
        ExitGame.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                int j=JOptionPane.showConfirmDialog(null, "真的要退出吗?","退出",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
                if(j==JOptionPane.YES_OPTION){
                    System.exit(0);
                }
            }
        });

        //为音乐播放按钮  添加事件
        startMusic.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                String filepath = "music" + File.separator + "music.wav";
                MusicPlayer musicPlayer = new MusicPlayer();
                musicPlayer.playMusic(filepath);//音乐播放
            }

        });
        f.add(newStartGame, BorderLayout.WEST);
        f.add(startMusic,BorderLayout.EAST);
        f.add(ExitGame,BorderLayout.SOUTH);
        f.setVisible(true);
    }
}

项目可用作毕业设计,获取源码咨询QQ:1657656774



版权声明:本文为qq_44860722原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。