⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 startgame.java

📁 java开发的人机对战五子棋游戏!只有安装jdk环境,就可以运行
💻 JAVA
字号:
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.Icon;
import javax.swing.ImageIcon;


public class StartGame extends MainWin{
    public StartGame() {
    }

    Vector allqi=new Vector();
    int i=0,j=0;
    int x,y;
    int dingwei[][]=new int[18][18];
    Icon iblack = new ImageIcon("black.png");
    Icon iwinner=new ImageIcon("winner.png");
    Icon iloser=new ImageIcon("loser.png");
    Icon ired = new ImageIcon("white.png");
    int count=0;
    int win=0;
    CheckWin cwin=new CheckWin();
    Computer computer=new Computer();
    qizi qi=new qizi();
    qizi q=new qizi();
    qizi cqi=new qizi();


    public void StartGame()
    {
        showmsg("游戏开始.....");
        showfive.addMouseListener(this);
        startgame.removeActionListener(this);
        startgame.setText("重新开始");
        showfive.removeAll();
        showfive.add(fivechess);
        showfive.updateUI();
        allqi.removeAllElements();
        computer.setstep(0);

    }
    public void  mouseClicked(MouseEvent e)
    {
        zouqi(e.getX(),e.getY());
    }
    public void zouqi(int ux,int uy)
    {
        int yx=0,yy=0;
        int winner=0;
        qizi qi=new qizi();
        qizi q=new qizi();
        qizi cqi=new qizi();
        qi = check(ux, uy, 1);
        if(qi!=null)
        {
            showfive.removeMouseListener(this);
            yx = qi.x;
            yy = qi.y;
//            System.out.print((yx+10)/29);
//            System.out.print((yy+10)/29);
            dingwei[(yx + 10) / 29][(yy + 10) / 29] = 1;
            allqi.addElement(qi);
            CheckWin checkwin = new CheckWin();
            winner = checkwin.CheckWin(dingwei, (yx + 10) / 29, (yy + 10) / 29);
            showfive.removeAll();
            if (winner == 1)
            {
                JLabel lwinner = new JLabel(iwinner);
                lwinner.setBounds(50,170,400,100);
                showfive.add(lwinner);
                showmsg("黑方胜");
                startgame.addActionListener(this);
                for(int m=0;m<18;m++)
                    for(int n=0;n<18;n++)
                        dingwei[m][n]=0;

            }

//            count = allqi.size() - 1;
//            showfive.removeAll();
//            while (count >= 0) {
//                qi = (qizi) allqi.elementAt(count);
//                x = qi.x;
//                y = qi.y;
//                if (qi.sider == 1) {
//                    JLabel cess = new JLabel(iblack);
//                    cess.setBounds(x, y, 50, 50);
//                    showfive.add(cess);
//                } else {
//                    JLabel cess = new JLabel(ired);
//                    cess.setBounds(x, y, 50, 50);
//                    showfive.add(cess);
//                }
//
//                count--;
//            }
//            showfive.add(fivechess);
//            showfive.updateUI();
//            win=cwin.CheckWin(dingwei,(yx+10)/29,(yy+10)/29);
//            if (win != 0) {
//                showmsg("winner:" + win);
//
//            }

            //计算机走棋
//        computer=new Computer(dingwei,(yx+10)/29,(yy+10)/29);
            if (winner == 0)
            {
                q = computer.zouqi(dingwei, (yx + 10) / 29, (yy + 10) / 29);
                dingwei[q.x][q.y] = 2;
                winner = checkwin.CheckWin(dingwei, q.x, q.y);
                cqi.x = q.x * 29 - 10;
                cqi.y = q.y * 29 - 10;
                allqi.addElement(cqi);
            }
                count = allqi.size() - 1;
                if (winner==2)
                {
                    JLabel lloser = new JLabel(iloser);
                     lloser.setBounds(50,170,400,100);
                     showfive.add(lloser);
                    showmsg("红方胜");
                    startgame.addActionListener(this);
                    for(int m=0;m<18;m++)
                    for(int n=0;n<18;n++)
                        dingwei[m][n]=0;

                }


                while (count >= 0) {
                    qi = (qizi) allqi.elementAt(count);
                    x = qi.x;
                    y = qi.y;
                    if (qi.sider == 1) {
                        JLabel cess = new JLabel(iblack);
                        cess.setBounds(x, y, 50, 50);
                        showfive.add(cess);
                    } else {
                        JLabel cess = new JLabel(ired);
                        cess.setBounds(x, y, 50, 50);
                        showfive.add(cess);
                    }
                    count--;
                }
                showfive.add(fivechess);


                showfive.updateUI();
                if(winner==0)
                  showfive.addMouseListener(this); //人走棋

        }
    }

    public qizi check(int x_, int y_,int sider) {
         qizi qi = new qizi();
         x = x_;
         y = y_;
         int minx=10,miny=10;

         if (x < 10 || y < 10 || x > 515 || y > 515) {
             return null;
         } else {
             if ((x_ - minx) % 29 > 14.5) {
                 i = (int) ((x_ - minx) / 29) + 1;
             } else {
                 i = (int) (x_ - minx) / 29;
             }
             if ((y_ - miny) % 29 > 14.5) {
                 j = (int) ((y_ - miny) / 29) + 1;
             } else {
                 j = (int) (y_ - miny) / 29;
             }
             x = i * 29 ;
             y = j * 29 ;
             if (dingwei[i][j] == 0) {
                 if (sider == 1) {
                     dingwei[i][j] = 1;
                 }
                 if (sider == 2) {
                     dingwei[i][j] = 2;
                 }
                 qi.x = x-10;
                 qi.y = y-10;
                 qi.sider = sider;
                 return qi;
             }
             return null; //当那个点已经有棋时,点击无效
         }
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -