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

📄 chesswzq.java

📁 基于java具备人工智能的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: e-top</p>
 * @author cylix
 * @version 1.0
 */

public class ChessWZQ extends JFrame {
    JPanel contentPane;
    BorderLayout borderLayout1 = new BorderLayout();

    protected Image white = null;
    protected Image black = null;
    protected static int pColor; //用户棋子的颜色
    protected static int cColor; // 计算机棋子的颜色

    static boolean ptocFlag=false;
    boolean pFirst = false;
    private int bestX=0; // 计算机选择的最优落子的x坐标
    private int bestY=0; // 计算机选择的最优落子的y坐标
    private int RectX1=0; // 根据x和y坐标设置的矩形位置边界
    private int RectY1=0; // 长度为 9
    private int RectX2=0; // 大小为 9*9
    private int RectY2=0; 
    private int weightBoard[][];

    static Socket socket;
    private static int PORT;
    static ObjectInputStream in;
    static ObjectOutputStream out;
    String name=null ;
    String serverAddress=null;
    static char send[];
    public static Message message = new Message();
    public static boolean beginFlag = false;

    BoardPanel bpanel = new BoardPanel();
    JPanel jpanel3 = new JPanel();
    JLabel label1 = new JLabel("Player1");
    JLabel label2 = new JLabel(" VS ");
    JLabel label3 = new JLabel("Player2");
    JLabel label4 = new JLabel("Player List ");
    JLabel label5 = new JLabel("Message list... ");
    static JLabel label6 = new JLabel("welcome");
    JLabel label7 = new JLabel("Host");
    JLabel label8 = new JLabel("Player");
    JRadioButton jrbBlack = new JRadioButton("Black");
    JRadioButton jrbWhite = new JRadioButton("White");
    DefaultListModel lItems = new DefaultListModel();
    JList list = new JList(lItems);
    JMenuBar mb = new JMenuBar();
    JMenu create = new JMenu("Create");
    JMenu setting = new JMenu("Setting");
    JMenu quit = new JMenu("Quit");
    JMenu about = new JMenu("About");
    JMenuItem cPtoP = new JMenuItem("Play With people");
    JMenuItem cPtoC = new JMenuItem("Play With Computer");
    JMenuItem load = new JMenuItem("Load game...");
    JMenuItem save = new JMenuItem("Save ...");

    public ChessWZQ() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        getContentPane().setLayout(null);
        weightBoard = new int [15][15]; // 保存棋盘每一个点的权重
        PORT = Server.PORT;//设置socket端口
        send = new char[60];

        try {
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        try {
            getContentPane().setLayout(null);

            jrbBlack.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    bpanel.setColor(1);
                    cColor=2;
                    jrbBlack.setEnabled(false);
                    jrbWhite.setEnabled(false);
                    drawChess(1);
                    jrbWhite.setSelected(true);
                    if(ptocFlag==true){
                        return;
                    }
                    Message ms = new Message();
                    ms.color=1;
                    ms.type=13;
                    try{
                        out.writeObject(ms);
                    }catch(IOException error){
                        error.printStackTrace();
                    }
                }
            });
            jrbWhite.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    bpanel.setColor(2);
                    cColor=1;
                    jrbWhite.setEnabled(false);
                    jrbBlack.setEnabled(false);
                    drawChess(2);
                    jrbBlack.setSelected(false);
                    if(ptocFlag==true){
                        return;
                    }
                    Message ms = new Message();
                    ms.color=2;
                    ms.type=13;
                    try{
                        out.writeObject(ms);
                    }catch(IOException error){
                        error.printStackTrace();
                    }
                }
            });
            about.addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e){
                    JOptionPane.showMessageDialog(null,
                                                  "author: cylix \nCopyright (c) 2003 e-top\nmail:cylix_xtcc@sina.com",
                                                  "五子棋1.0(beta)",JOptionPane.INFORMATION_MESSAGE);
                }
            });
            quit.addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e){
                    int flag=JOptionPane.showConfirmDialog(null,
                                                  "Quit the Program ?",
                                                  "Are you sure to quit ?",
                                                  JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                    if(flag==0){       
                        // 断开与服务器的连接
                        sendDisconnect();
                        System.exit(0);
                    }
                }
            });
            setting.addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e){

                    Object selection[] = {"Forbiden","Allow any"};
                    Object set = JOptionPane.showInputDialog(null,
                        "Setting...","would you allow any method?",
                        JOptionPane.QUESTION_MESSAGE,null,selection,selection[0]);
                    if(ptocFlag==true){
                        return;
                    }
                    Message ms = new Message();
                    if(set==null){
                        return;
                    }
                    if(set.toString().equals(selection[0])){
                        ms.setting=false;
                    }
                    else{
                        ms.setting=true;
                    }
                    ms.type=12;
                    try{
                        out.writeObject(ms);
                    }catch(IOException error){
                        error.printStackTrace();
                    }
                }
            });
            cPtoP.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    ptocFlag=false;
                    JOptionPane.showMessageDialog(null,
                        "You can choose a player from the listBox on the right","Welcome...",
                        JOptionPane.INFORMATION_MESSAGE);
                }
            });
            cPtoC.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    if(ptocFlag==true){
                        int flag=JOptionPane.showConfirmDialog(null,
                            "You give up... ?","Message",
                            JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                        if(flag==0){
                            newGame();
                            return;
                        }
                    }
                    label3.setText("Computer");
                    Object selection[] = {"Computer First","You First"};
                    Object set = JOptionPane.showInputDialog(null,
                        "choose who first...","setting...",
                        JOptionPane.QUESTION_MESSAGE,null,
                        selection,selection[0]);
                        ptocFlag = true;
                    if(set.toString().equals(selection[1])){
                        pFirst = true;
                    }else{
                        pFirst = false;
                    }
                    pColor=bpanel.getColor();
                    if(pColor==1){
                        jrbBlack.setSelected(true);
                        jrbWhite.setSelected(false);
                        cColor=2;
                    }
                    else{
                        jrbWhite.setSelected(true);
                        jrbBlack.setSelected(false);
                        cColor=1;
                    }
                    jrbBlack.setEnabled(false);
                    jrbWhite.setEnabled(false);
                    ptoComputer();
                }
            });

            create.setBounds(5,5,40,20);
            setting.setBounds(45,5,40,20);
            quit.setBounds(85,5,40,20);
            about.setBounds(125,5,40,20);
            create.add(cPtoP);
            create.add(cPtoC);
            create.add(load);
            create.add(save);
            mb.add(create);
            mb.add(setting);
            mb.add(quit);
            mb.add(about);
            this.setJMenuBar(mb);

            bpanel.setBounds(0,0,470,460);
            bpanel.setBorder(BorderFactory.createMatteBorder(0,0,1,1,Color.ORANGE));

            jpanel3.setLayout(null);
            jpanel3.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.DARK_GRAY));
            jpanel3.setBounds(470,0,180,460);
            label7.setBounds(10,10,30,20);
            label7.setBackground(Color.blue);
            label7.setForeground(Color.yellow);
            label8.setBounds(90,10,40,30);
            label8.setBackground(Color.blue);
            label8.setForeground(Color.yellow);
            label1.setBounds(10,40,50,20);
            label2.setForeground(Color.RED);
            label2.setBounds(60,40,50,20);
            label3.setBounds(90,40,70,20);
            label4.setForeground(Color.BLUE);
            label4.setBounds(10,78,70,20);
            label5.setBackground(Color.magenta);
            label5.setForeground(Color.RED);
            label5.setBounds(15,395,160,20);
            label6.setBounds(15,415,180,20);
            jrbBlack.setSelected(true);
            jrbBlack.setBounds(10,380,80,15);
            jrbWhite.setBounds(90,380,80,15);

            list.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));
            list.setBounds(10,100,150,265);

            list.addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e){
                    //双击鼠标选择一个用户 
                    if(e.getClickCount()==2){
                        String str = list.getSelectedValue().toString();
                        if(str.equals(name)==true){
                            label6.setText("You can't choose yourself");
                            return;
                        }
                        label3.setText(str);
                        list.setEnabled(false);

                        Message ms = new Message();
                        ms.type=3;  //向另外的用户发出连接请求
                        strToCharArray(str,ms.msg);
                        ms.color = bpanel.getColor();
                        try{
                            out.writeObject(ms);
                        }catch(IOException er){
                            er.printStackTrace();
                        }
                }
            }
        });
            jpanel3.add(label1);
            jpanel3.add(label2);
            jpanel3.add(label3);
            jpanel3.add(label4);
            jpanel3.add(label5);
            jpanel3.add(label6);
            jpanel3.add(label7);
            jpanel3.add(jrbBlack);
            jpanel3.add(jrbWhite);
            jpanel3.add(list);
            getContentPane().add(bpanel);
            getContentPane().add(jpanel3);

        }
        catch(Exception e) {
            e.printStackTrace();
        }

    }

    //界面控件初始化
    private void jbInit() throws Exception  {
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(borderLayout1);
        this.setSize(new Dimension(660, 530));
        this.setTitle("五子棋客户端(beta)");
    }
    /**
     * 向服务器发送断开命令
     * type = 7
     */
    public void sendDisconnect(){
        Message ms = new Message();
        ms.type=7;
        try{
            out.writeObject(ms);
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public void drawChess(int col){
        Graphics g = jpanel3.getGraphics();
        if(col==1) g.setColor(Color.black);
        else g.setColor(Color.white);
        g.fillOval(50,10,20,20);
    }

    /**
     * 主应用程序
     */
    public static void main(String [] args){
        ChessWZQ wzq = new ChessWZQ();
        wzq.setResizable(false);
        wzq.setVisible(true);
        wzq.drawChess(1);   //绘制棋盘,默认的颜色为黑色

        Message ms = new Message();
        if(args[0]!=null)
        	wzq.serverAddress = new String(args[0]);
        else
        	wzq.serverAddress = new String("localhost");
        if(args[1]!=null)
        	wzq.name = new String(args[1]);
        else
        	wzq.name = new String("cylix");
        wzq.strToCharArray(wzq.name,ms.msg);
        try{
            // 通过命令行得到服务器名称
            InetAddress addr = InetAddress.getByName(wzq.serverAddress);
            //根据服务器名称和端口号建立Socket对象
            socket = new Socket(addr, PORT);
			//获取网络输出流
            out = new ObjectOutputStream(
                socket.getOutputStream());
            //获取网络输入流
			in = new ObjectInputStream(
                socket.getInputStream());
            ms.type=1;
            try{
                out.writeObject(ms);
            }catch(IOException e){
                e.printStackTrace();
            }
            while(true){
                try {
                    ms = (Message) in.readObject();
                    wzq.doMessage(ms);
                }
                catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }catch(IOException e){
            e.printStackTrace();
        }

    }
    /**
     * 处理从网络获取的Message对象
     */
    protected int doMessage(Message msg){
        String str=arrayToString(msg.msg);
        switch(msg.type){
            case 2:{//对落子做出响应
                putChessman(msg);
                break;
            }
            case 3:{//请求另一个玩家
                requestAnother(msg);
                break;
            }
            case 4:{ // B拒绝同A进行游戏
                getDeny(msg);
                break;}
            case 5:{ // B同意 A的游戏请求
                acceptToPlay(msg);
                break;}
            case 6:{//获取胜利
                getVictory(msg);
                break;}
            case 7:{//断开连接
                getDisconnection(msg);
                break;}
            case 9:{//同意添加新的游戏玩家
                lItems.add(0,str);
                break;
            }
            case 10:{ //成功连接后显示响应信息
                label1.setText(str);
                label6.setText("welcome "+str);
                lItems.add(0,str);
                break;
            }
            case 14:{ 
                break;
            }
            case 15:{
                lItems.clear();
                break;
            }
            case 17:{
                getFailed(msg);

⌨️ 快捷键说明

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