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

📄 gameframe.java

📁 这是一个经典的五子棋程序能够人机对战
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import javax.swing.JLabel;
import javax.swing.*;
import javax.swing.border.TitledBorder;

/**
 * <p>Title: 五子棋</p>
 *
 * <p>Description: 五子棋游戏</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: David Company</p>
 *
 * @author David
 * @version 1.0
 */
public class GameFrame extends JFrame {
    JPanel contentPane;
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenuFile = new JMenu();
    JMenuItem jMenuFileExit = new JMenuItem();
    JMenu jMenuHelp = new JMenu();
    JMenuItem jMenuHelpAbout = new JMenuItem();
    JPanel jPanel1 = new JPanel();
    JPanel jPanel3 = new JPanel();
    JPanel jPanel4 = new JPanel();
    XYLayout xYLayout2 = new XYLayout();
    JLabel jPlayer1 = new JLabel();
    JLabel jPlayer2 = new JLabel();
    XYLayout xYLayout3 = new XYLayout();
    JLabel lBoard = new JLabel();
    XYLayout xYLayout4 = new XYLayout();
    TitledBorder titledBorder1 = new TitledBorder("");
    JPanel jPanel2 = new JPanel();
    JPanel jPanel5 = new JPanel();
    JLabel jLabel4 = new JLabel();
    XYLayout xYLayout5 = new XYLayout();
    XYLayout xYLayout1 = new XYLayout();
    XYLayout xYLayout6 = new XYLayout();
    JLabel jWaitPlayer1 = new JLabel();
    JLabel jWaitPlayer2 = new JLabel();
    JMenu jMenu1 = new JMenu();
    int selectPlayer=0;  //0 - 没有开始;  1 - Player vs Computer;  2 - Player VS player; 3 - web
    ImageIcon image1;            // 棋盘
    ImageIcon image_bj;          // bj
    ImageIcon image_title;          // title
    ImageIcon image_playing;       // playing
    ImageIcon image_waiting;         // waiting
    ImageIcon image_end;       // end
    ImageIcon jChessImageBlack; // 黑棋子
    ImageIcon jChessImageWhite;// 白棋子

    ImageIcon jImageChess;       // 棋子照片
    JPanel jPanelChess = new JPanel();
    JLabel[] jChess=new JLabel[256];        //  旗子标签
    int m_step=0;                           //  记录步数
    private int m_turn=0;                  //  该谁了=0表示黑方,=1白方
    JButton cmd_undo = new JButton();
    int theStep[][]=new int[256][3];
    boolean isFinish=false;                //是否已经成

    NetDialog netDialog;
    Func func=new Func();
    NetGame netgame;
    //下面是一些图象常量数据
        int linespace=28;
        int xn=15;
        int yn=15;
     //下面数据是调试算法x,y
        int resultX;
        int resultY;
    JMenuItem jMenuItem2 = new JMenuItem();
    JCheckBoxMenuItem jCheckBoxMenuItem1 = new JCheckBoxMenuItem();
    JCheckBoxMenuItem jCheckBoxMenuItem2 = new JCheckBoxMenuItem();
    JMenuItem jMenuItem1 = new JMenuItem();
    int role=-1;
    boolean isWebStart=false;
    int m_undo;
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    XYLayout xYLayout7 = new XYLayout();
    JLabel jLabel3 = new JLabel();
    JLabel jPlayerA = new JLabel();
    JLabel jPlayerB = new JLabel();
    JLabel jLabel5 = new JLabel();
    JButton cmd_New = new JButton();
    public GameFrame() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        //jChessImageBlack = new ImageIcon(game.GameFrame.class.getResource("black.png"));
        //jChessImageWhite = new ImageIcon(game.GameFrame.class.getResource("white.png"));
        image1 = new ImageIcon(game.GameFrame.class.getResource("board.png"));
        image_bj =  new ImageIcon(game.GameFrame.class.getResource("bj.png"));          // bj
        image_title=  new ImageIcon(game.GameFrame.class.getResource("Title.png"));          // title
        image_playing=  new ImageIcon(game.GameFrame.class.getResource("Playing.png"));       // playing
        image_waiting=  new ImageIcon(game.GameFrame.class.getResource("Waiting.png"));         // waiting
        image_end=  new ImageIcon(game.GameFrame.class.getResource("End.png"));         // end
        jChessImageBlack= new ImageIcon(game.GameFrame.class.getResource("black.png")); // 黑棋子
        jChessImageWhite= new ImageIcon(game.GameFrame.class.getResource("white.png"));// 白棋子


        lBoard.setIcon(image1);
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(xYLayout1);
        this.setResizable(false);
        setSize(new Dimension(606, 485));
        setTitle("五子棋游戏");
        jMenuFile.setText("File");
        jMenuFileExit.setText("Exit");
        jMenuFileExit.addActionListener(new
                                        GameFrame_jMenuFileExit_ActionAdapter(this));
        jMenuHelp.setText("Help");
        jMenuHelpAbout.setText("About");
        jMenuHelpAbout.addActionListener(new
                                         GameFrame_jMenuHelpAbout_ActionAdapter(this));
        contentPane.setMinimumSize(new Dimension(606, 485));
        contentPane.setPreferredSize(new Dimension(606, 485));
        jPanel1.setBackground(Color.pink);
        jPanel1.setLayout(xYLayout4);
        jPanel3.setBackground(Color.white);
        jPanel3.setLayout(xYLayout2);
        jPanel4.setLayout(xYLayout3);
        jPlayer1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 18));
        jPlayer1.setIcon(jChessImageBlack);
        jPlayer1.setText("Player 1");
        jPlayer2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 18));
        jPlayer2.setIcon(jChessImageWhite);
        jPlayer2.setText("Player 2");
        lBoard.setAlignmentY((float) 0.0);
        lBoard.setBorder(titledBorder1);
        lBoard.setDisabledIcon(null);
        lBoard.setDisplayedMnemonic('0');
        lBoard.setIcon(image1);
        lBoard.setText("");
        lBoard.addMouseListener(new GameFrame_lBoard_mouseAdapter(this));
        jPanel2.setBackground(Color.white);
        jPanel2.setLayout(xYLayout7);
        jPanel5.setLayout(xYLayout5);
        jLabel4.setText("Please Select a Player First");
        jPanelChess.setBackground(Color.cyan);
        jPanelChess.setEnabled(false);
        jPanelChess.setBorder(null);
        jPanelChess.setDoubleBuffered(false);
        jPanelChess.setOpaque(false);
        jPanelChess.setInputVerifier(null);
        jPanelChess.setLayout(xYLayout6);
        cmd_undo.setEnabled(false);
        cmd_undo.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
        cmd_undo.setToolTipText("");
        cmd_undo.setText("悔棋");
        cmd_undo.addActionListener(new GameFrame_cmd_undo_actionAdapter(this));
        jWaitPlayer1.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
        jWaitPlayer1.setToolTipText("");
        jWaitPlayer1.setHorizontalAlignment(SwingConstants.CENTER);
        jWaitPlayer1.setText("Stop");
        jWaitPlayer2.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
        jWaitPlayer2.setToolTipText("");
        jWaitPlayer2.setHorizontalAlignment(SwingConstants.CENTER);
        jWaitPlayer2.setText("Stop");
        jMenu1.setText("Select Player");
        jMenuItem2.setText("New Game");
        jMenuItem2.addActionListener(new GameFrame_jMenuItem2_actionAdapter(this));
        jCheckBoxMenuItem1.setText("Player vs Computer");
        jCheckBoxMenuItem1.addActionListener(new
                GameFrame_jCheckBoxMenuItem1_actionAdapter(this));
        jCheckBoxMenuItem2.setText("Player vs Player");
        jCheckBoxMenuItem2.addActionListener(new
                GameFrame_jCheckBoxMenuItem2_actionAdapter(this));
        jMenuItem1.setText("Network");
        jMenuItem1.addActionListener(new GameFrame_jMenuItem1_actionAdapter(this));
        jLabel1.setIcon(image_bj);
        jLabel1.setText("jLabel1");
        jLabel2.setIcon(image_bj);
        jLabel2.setText("jLabel2");
        jLabel3.setForeground(Color.black);
        jLabel3.setToolTipText("");
        jLabel3.setIcon(image_title);
        jLabel3.setText("jLabel3");
        jPanel5.setBackground(SystemColor.window);
        jPlayerA.setIcon(image_waiting);
        jPlayerA.setText("");
        jPlayerB.setIcon(image_waiting);
        jLabel5.setIcon(image_end);
        jLabel5.setText("");
        cmd_New.addActionListener(new GameFrame_cmd_New_actionAdapter(this));
        cmd_New.setText("New");
        jMenuBar1.add(jMenuFile);
        jMenuFile.add(jMenuItem2);
        jMenuFile.add(jMenu1);
        jMenuFile.addSeparator();
        jMenuFile.add(jMenuFileExit);
        jMenuBar1.add(jMenuHelp);
        jMenuHelp.add(jMenuHelpAbout);
        jPanel1.add(jPanelChess, new XYConstraints(0, 0, -1, -1));  //把棋子层加到jPanel1中
        jPanel1.add(lBoard, new XYConstraints(0, 0, -1, -1));
        jMenu1.add(jCheckBoxMenuItem1);
        jMenu1.add(jCheckBoxMenuItem2);
        jMenu1.add(jMenuItem1);
        jPanel3.add(jPlayer1, new XYConstraints(30, 116, 122, 26));
        jPanel4.add(jPlayer2, new XYConstraints(30, 115, 117, 28));
        contentPane.add(jPanel1, new XYConstraints(0, 0, -1, 431));
        jPanel3.add(jPlayerA, new XYConstraints(49, 18, 70, 67));
        jPanel3.add(jWaitPlayer1, new XYConstraints(52, 83, 58, 17));
        jPanel4.add(jPlayerB, new XYConstraints(49, 18, 70, 67));
        jPanel4.add(jWaitPlayer2, new XYConstraints(52, 83, 58, 17));
        jPanel4.add(jLabel2, new XYConstraints(0, 0, -1, -1));
        contentPane.add(jPanel2, new XYConstraints(435, 0, 166, 77));
        contentPane.add(jPanel3, new XYConstraints(435, 78, 166, 155));
        contentPane.add(jPanel4, new XYConstraints(435, 235, 166, 155));
        contentPane.add(jPanel5, new XYConstraints(435, 392, 166, 39));
        jPanel5.add(jLabel4, new XYConstraints(5, 11, 149, -1));
        jPanel5.add(jLabel5, new XYConstraints(0, 0, -1, -1));
        jPanel2.add(cmd_undo, new XYConstraints(90, 35, 61, 24));
        jPanel2.add(cmd_New, new XYConstraints(18, 35, 63, 24));
        jPanel3.add(jLabel1, new XYConstraints(0, 0, -1, -1));
        jPanel2.add(jLabel3, new XYConstraints(0, 0, -1, -1));
        setJMenuBar(jMenuBar1);
        m_undo=0;
        /*     测试棋子位置
        jImageChess=new ImageIcon(game.GameFrame.class.getResource("black.png"));
        jChess[m_step]=new JLabel();
        jChess[m_step].setIcon(jImageChess);
        jChess[m_step].setAlignmentY((float) 0.0);
        jChess[m_step].setDisabledIcon(null);
        jChess[m_step].setDisplayedMnemonic('0');
        jChess[m_step].setText("");
        jPanelChess.add(jChess[m_step], new XYConstraints(8, 8, 22, 22));
        jChess[m_step]=new JLabel();
     */
    }

    /**
     * File | Exit action performed.
     *
     * @param actionEvent ActionEvent
     */
    void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
        System.exit(0);
    }

    /**
     * Help | About action performed.
     *
     * @param actionEvent ActionEvent
     */
    void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
        GameFrame_AboutBox dlg = new GameFrame_AboutBox(this);
        Dimension dlgSize = dlg.getPreferredSize();
        Dimension frmSize = getSize();
        Point loc = getLocation();
        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                        (frmSize.height - dlgSize.height) / 2 + loc.y);
        dlg.setModal(true);
        dlg.pack();
        dlg.show();
    }

     //showAllStep is the funcation to show history of theStep[][]
    public void showAllStep(){
        for (int i=1;i<=m_step;i++){
            System.out.print("Step " + i);
            if (theStep[i][2]==0) System.out.print("黑: x=");
            if (theStep[i][2]==1) System.out.print("白: x=");
            System.out.println(theStep[i][0] + " , y=" + theStep[i][1]);
        }
    }

    // writeStep is the funcation to write every step in theStep[][],it can't work,beause 二维数组的操作不对
    private void writeStep(int x,int y,int turn){
        theStep[m_step] = new int[3];
        theStep[m_step][0]=x;
        theStep[m_step][1]=y;
        theStep[m_step][2]=turn;
    }

    private void changePlayer(){
        m_turn=( m_turn + 1 ) % 2;
        if (m_turn == 0 ) {
            jPlayerA.setIcon(image_playing);
            jWaitPlayer1.setText("Playing...");
            jPlayerB.setIcon(image_waiting);
            jWaitPlayer2.setText("Waiting...");
        }
        if (m_turn == 1) {
            jPlayerA.setIcon(image_waiting);
            jWaitPlayer1.setText("Waiting...");
            jPlayerB.setIcon(image_playing);
            jWaitPlayer2.setText("Playing...");
        }
    }

    public synchronized void draw(int i,int j,int turn)  {
        if (turn==0) jImageChess=jChessImageBlack;
        if (turn==1) jImageChess=jChessImageWhite;
        jChess[m_step]=new JLabel();
        jChess[m_step].setIcon(jImageChess);
        jChess[m_step].setAlignmentY((float) 0.0);
        jChess[m_step].setDisabledIcon(null);
        jChess[m_step].setDisplayedMnemonic('0');
        jChess[m_step].setText("");
        jPanelChess.add(jChess[m_step], new XYConstraints(28*j+8, 28*i+8, 22, 22)); //行列 与 XY相反
        jPanelChess.updateUI();
        jPanelChess.revalidate();
    }

    private void showMsg(String msg,String title){
        JOptionPane.showMessageDialog(this,msg,title,JOptionPane.INFORMATION_MESSAGE);
    }

    //解str 到 int[]
    public int[] analys(String str){
             String temp[]=new String[20];
             temp=str.split(":");
             int t[]=new int[20];
             for (int i=0;i<=2;i++)
                 t[i]=Integer.parseInt(temp[i]);
             return t;
  }

    public  void desktop(int x,int y){
        cmd_undo.setEnabled(true);
        if (func.isDrawNow(x, y)) {
            func.setXY(x, y, m_turn);
            m_step++;
            this.writeStep(x, y, m_turn);
            this.showAllStep();
            draw(x, y, m_turn);
            jLabel4.setText("Step: " + m_step + " : " + x + " " + y);
            System.out.println("Player:" + m_turn + " X=" + x + ",Y=" + y);
            changePlayer();
            if (func.isOK()) {
               //public static void showMessageDialog(Component parentComponent,Object message,String title,int messageType,Icon icon)
               String stemp = new String("");
               stemp="恭喜你赢了!";
               if (m_undo!=0) {
                   stemp += "您悔了" + m_undo + "步棋!我不服气!再来一次!";
               }

⌨️ 快捷键说明

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