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

📄 chesswzq.java~2~

📁 这是一个用java编写的网络对战五子棋程序
💻 JAVA~2~
📖 第 1 页 / 共 3 页
字号:
            jpanel3.add(list);

            // getContentPane().add(jpanel1);
            getContentPane().add(bpanel);
            getContentPane().add(jpanel3);

           // lItems.add(0,"hugh");
           // lItems.add(0,"cylix");

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

    }

    //Component initialization
    private void jbInit() throws Exception  {
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(borderLayout1);
        this.setSize(new Dimension(660, 530));
        this.setTitle("五子棋客户端(beta)");
    }
    /**
     * send disconnection to server
     * 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);
    }

    /**
     * <format> java ChessWZQ < server address> <player name>
     * @param args[0]=server address args[1]=player name
     */
    public static void main(String [] args){
        ChessWZQ wzq = new ChessWZQ();
        wzq.setResizable(false);
        wzq.setVisible(true);
        wzq.drawChess(1);   //default color is black

        Message ms = new Message();
//        wzq.serverAddress = new String(args[0]);
//        wzq.name = new String(args[1]);
//        wzq.strToCharArray(wzq.name,ms.msg);


        try{
            // should get server name by command line
            InetAddress addr = InetAddress.getByName(wzq.serverAddress);
            //System.out.println("address "+ addr.toString()+" port: "+PORT);
            socket = new Socket(addr, PORT);
            //System.out.println("set socket successful...");

            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();
                    //System.out.println("get message from server...type = "+ms.type);
                    wzq.doMessage(ms);
                }
                catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            //System.out.println("ms.coordinateX + , + msg.coordinateY");
            //System.out.println(ms.color);
            //System.out.println("close ...");
            //socket.close();
        }catch(IOException e){
            e.printStackTrace();
        }

    }
    /**
     * deal with message get from server
     * @param msg
     * @return
     */
    protected int doMessage(Message msg){
        String str=arrayToString(msg.msg);
        //System.out.println("msg name = "+str+" msg.type = "+msg.type);
        switch(msg.type){
            case 2:{//respones of put chessman
                putChessman(msg);
                break;
            }
            case 3:{//request from another player
                requestAnother(msg);
                break;
            }
            case 4:{ // B deny to play with A
                getDeny(msg);
                break;}
            case 5:{ // B accept A's requestion
                acceptToPlay(msg);
                break;}
            case 6:{
                getVictory(msg);
                break;}
            case 7:{
                getDisconnection(msg);
                break;}
            case 9:{//reply of adding new player to all client
                ///System.out.println("add new player to list type = 9 name = "+str+"msg[0]= "+msg.msg[0]);
                lItems.add(0,str);
                break;
            }
            case 10:{ //respones of connecting
                //System.out.println("add him self  type = 10 name = "+str+"msg[0]= "+msg.coordinateX+msg.msg[0]);
                label1.setText(str);
                label6.setText("welcome "+str);
                lItems.add(0,str);
                break;
            }
            case 14:{ //B have accept playing with A,then get msg
                      //to set color and settings
                break;
            }
            case 15:{
                lItems.clear();
                break;
            }
            case 17:{
                getFailed(msg);
                break;
            }
            case 20:{
                ptocWin(msg);
                break;
            }
        }
        return 0; //end correctly
    }
    private void ptocWin(Message ms){
        String str=this.arrayToString(ms.msg);
        JOptionPane.showMessageDialog(null,str+" win the game!","V the game",JOptionPane.INFORMATION_MESSAGE);
        newGame();
    }
    /**
     * failed the game and have a new one
     * type == 17
     * @param msg
     */
    public void getFailed(Message msg){
        bpanel.drawChess(msg.coordinateX,msg.coordinateY);
        JOptionPane.showMessageDialog(null,
                                      "Sorry,You've failed the game",
                                      "Try Again",
                                      JOptionPane.INFORMATION_MESSAGE);
        label3.setText("Player2");
        // have a new game to continue to play
        newGame();
    }
    /**
     * another player send disconnection msg,here will resolve it
     * type ==7
     * @param msg
     */
    public void getDisconnection(Message msg){
        getVictory(msg);
    }
    /**
     * win the game
     * type ==6
     * @param msg
     */
    public void getVictory(Message msg){
        JOptionPane.showMessageDialog(null,
                                      "You  Win  The  Game",
                                      "Congratulations",
                                      JOptionPane.INFORMATION_MESSAGE);
        // have a new game to continue to play
        label3.setText("Player2");
        newGame();
    }
    /**
     * when people win a game ,then he can start a new one
     * @param msg
     */
    public void newGame(){
        jrbBlack.setEnabled(true);
        jrbWhite.setEnabled(true);
        jrbBlack.setSelected(true);
        jrbWhite.setSelected(false);
        list.setEnabled(true);
        setting.setEnabled(true);
        bpanel.clearBoard();
        drawChess(1);
        pColor=1;cColor=2;
        if(ptocFlag==false){
            Message msg = new Message();
            msg.type = 19;
            strToCharArray(name, msg.msg);
            try {
                out.writeObject(msg);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        ptocFlag=false;
        beginFlag=false;
    }
    /**
     * precondition:get a msg from competitor of puting a chessman
     * fundition : deal with it
     * @param msg
     */
    public void putChessman(Message msg){
        if(ptocFlag==false){
            bpanel.updateBoard(msg.coordinateX,msg.coordinateY);
            bpanel.drawChess(msg.coordinateX,msg.coordinateY);
            beginFlag = true;
            return;
        }else{
            // update native board
            // search position to put chessman
        }
    }
    /**
     * A get a Ok msg from B and seting according with B
     * type= 5 B accept to play with A
     * @param msg = B's name
     */
    public void acceptToPlay(Message msg){
        String str=arrayToString(msg.msg);
        String ss=null;
        if(msg.color==1){
            ss = new String("white");
            bpanel.setColor(2);
        }
        else{
            ss = new String("black");
            bpanel.setColor(1);
        }
        JOptionPane.showMessageDialog(null,
                                      "OK. "+str+" have accepted your requestion\nYour color is"+ss,
                                      "Game will to begin...",JOptionPane.ERROR_MESSAGE);

        list.setEnabled(false);
        jrbBlack.setEnabled(false);
        jrbWhite.setEnabled(false);
        setting.setEnabled(false);
        beginFlag=true;
    }
    /**
     * A get a deny msg from B
     * type ==4 deny to play
     * @param msg
     */
    public void getDeny(Message msg){
        String str=arrayToString(msg.msg);
        JOptionPane.showMessageDialog(null,
                                      "I'm sorry\n"+str+" denied your requestion",
                                      "Sorry...",JOptionPane.ERROR_MESSAGE);
        list.setEnabled(true);
        label3.setText("Player2");
    }
    /**
     * A request play game with B,what below is B done
     * deal with request to paly
     * @param msg = requester 's name
     */
    public void requestAnother(Message msg){
        String str=arrayToString(msg.msg);
//        System.out.print("client requestAnother begin ...");
        int flag =JOptionPane.showConfirmDialog(null,
            "Player "+str+" want to play with you\nAre you OK?",
            "Play request...",JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE);
        if(flag==0){// press YES
            msg.type=5; //agree with request
            if(msg.color==1){ // msg.color is B 's color ,different with A's
                drawChess(msg.color);
                jrbBlack.setSelected(true);
                jrbWhite.setSelected(false);
                bpanel.setColor(1);
            }
            else{
                drawChess(msg.color);
                jrbWhite.setSelected(true);
                jrbBlack.setSelected(false);
                bpanel.setColor(2);
            }
//                System.out.println("B's color is"+msg.color);
            list.setEnabled(false);

⌨️ 快捷键说明

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