一、Git使用

廖雪峰

 

二、源码重构

黑子走棋操作:第395行

 

1、添加ai功能

使用中国云库api

    添加ai按钮:

JButton aicnl;//ai sxj

 

    添加ai按钮:

    //注册按钮
    JButton aicnl;

    /** sxj
     ** 控制 ai
     ** ai=1 开启ai
     ** ai=0 关闭ai  默认关闭
     */
    int ai = 0;

    //鼠标悬浮信息展示
    aicnl = new JButton(" A I ");
    aicnl.setToolTipText("开启/关闭 AI,AI执黑棋");

    //添加到工具栏
    jmain.add(aicnl);

    //注册按钮监听
    aicnl.addActionListener(this);

 

ai走棋函数:

    /**
     ** 获取ai走棋
     */
    public String aiMove ()
    {
        char[][] mapp = new char[15][15];
        for (int i=1;i<=10;i++)
            for (int j=1;j<=9;j++)
                mapp[j][i] = 'o';

        //将棋盘翻译为FEN格式代码
        for (int i=0;i<32;i++)
            if (play[i].getText() == "1")
            {
                //System.out.println(play[i].getName());
                String ches = play[i].getName().toString();
                char a = ches.charAt(0),b = ches.charAt(1);
                int x = (play[i].getX()-24)/57+1;
                int y = (play[i].getY()-56)/57+1;
                System.out.println(a);
                System.out.println(b);
                if (a == 'R')
                    if (b == '1')
                        mapp[x][y]='r';
                    else
                        mapp[x][y]='R';
                if (a == 'H')
                    if (b == '1')
                        mapp[x][y]='n';
                    else
                        mapp[x][y]='N';
                if (a == 'E')
                    if (b == '1')
                        mapp[x][y]='b';
                    else
                        mapp[x][y]='B';
                if (a == 'B')
                    if (b == '1')
                        mapp[x][y]='a';
                    else
                        mapp[x][y]='A';
                if (a == 'P')
                    if (b == '1')
                        mapp[x][y]='p';
                    else
                        mapp[x][y]='P';
                if (a == 'C')
                    if (b == '1')
                        mapp[x][y]='c';
                    else
                        mapp[x][y]='C';
                if (a == 'K')
                    if (b == '1')
                        mapp[x][y]='k';
                    else
                        mapp[x][y]='K';
            }

        String fen = "";
        for (int i=1;i<=10;i++)
        {
            int emp = 0;
            for (int j=1;j<=9;j++) if (mapp[j][i] == 'o') emp++; else { if (emp > 0)
                        fen += emp + "";
                    emp = 0;
                    fen += mapp[j][i];
                }
            if (emp > 0)
                fen += emp + "";
            if (i < 10) fen += '/'; } System.out.println(fen); //访问中国象棋云库api String requestUrl = "http://api.chessdb.cn:81/chessdb.php?action=queryall&learn=1&showall=1&egtbmetric=dtm&board="+fen+"%20b"; output = ""; //建立URL,把请求地址给补全,其中urlencode()方法用于把params里的参数给取出来 try { URL url = new URL(requestUrl); //打开http连接 HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection(); httpUrlConn.setDoInput(true); httpUrlConn.setRequestMethod("GET"); httpUrlConn.connect(); //获得输入 InputStream inputStream = httpUrlConn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); output = bufferedReader.readLine(); System.out.println(output); char[] bm = output.toCharArray(); //判断是否死棋 if (output == "checkmate " || output == "stalemate ") { JOptionPane.showConfirmDialog( this,"红棋胜利","玩家二胜利", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE); chessPlayClick=3; text.setText(" 红棋胜利"); } //翻译返回命令 if (output.length() >= 9)
            {
                int aX = ((int)(bm[5] - 'a')) * 57 + 24;
                int aY = (9 - (int)(bm[6] - '0')) * 57 + 56;
                int bX = ((int)(bm[7] - 'a')) * 57 + 24;
                int bY = (9 - (int)(bm[8] - '0')) * 57 + 56;
                System.out.println("666:"+bm[5]+bm[6]+bm[7]+bm[8]+"   "+aX+" "+aY+"   "+bX+" "+bY);

                for (int i=0;i<32;i++)
                    if (play[i].getX() == aX && play[i].getY() == aY && play[i].getText() == "1")
                    {
                        //判断目标位置是否有棋子,有便将其设死
                        for (int j=0;j<32;j++)
                            if (play[j].getX() == bX && play[j].getY() == bY && play[j].getText() == "1")
                            {
                                play[j].setText("0");
                                play[j].setVisible(false);
                                addToVar(play[j],j);
                                break;
                            }

                        //当前记录添加到集合(用于悔棋)
                        addToVar(play[i],i);

                        //移动棋子
                        play[i].setBounds(bX,bY,55,55);
                        
                        text.setText("               红棋走棋");

                        break;
                    }
            }
            else
                text.setText("  当前局面尚未录入");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return output;
    }

 

红方走完棋后添加判断是否ai走棋:

                //ai 是否开启 by_sxj
                if (ai == 1 && chessPlayClick == 1)
                {
                    aiMove();
                    chessPlayClick=2;
                }

 

2、添加更换主题功能

EZ


CSUer