[IT科技] 有一个五子棋程序,多人,支持多房间-【Applet客户端】

发表于 @ 2009-6-8 18:00:26

有一个五子棋程序,多人,支持多房间-【Applet客户端】
package test.wuzi;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.StringTokenizer;
import javax.swing.JApplet;

public class OmokApplet extends JApplet implements Runnable, ActionListener {
  private TextArea msgView = new TextArea("", 1, 1, 1);

  private TextField sendBox = new TextField("");

  private TextField nameBox = new TextField();

  private TextField roomBox = new TextField("0");

  private Label pInfo = new Label("待机事:  名");

  private java.awt.List pList = new java.awt.List();

  private Button startButton = new Button("开始对决");

  private Button stopButton = new Button("弃权");

  private Button enterButton = new Button("入场");

  private Button exitButton = new Button("去待机事");

  private Label infoView = new Label(" < Thing java >", 1);

  private OmokBoard board = new OmokBoard(15, 30);

  private BufferedReader reader;

  private PrintWriter writer;

  private Socket socket;

  private int roomNumber = -1;

  private String userName = null;

  public OmokApplet() {
  }

  @Override
  public void init() {
    setLayout(null);
    msgView.setEditable(false);
    infoView.setBounds(10, 30, 480, 30);
    infoView.setBackground(new Color(200, 200, 255));
    board.setLocation(10, 70);
    add(infoView);
    add(board);
    Panel p = new Panel();
    p.setBackground(new Color(200, 255, 255));
    p.setLayout(new GridLayout(3, 3));
    p.add(new Label("名字:", 2));
    p.add(nameBox);
    p.add(new Label("房间号:", 2));
    p.add(roomBox);
    p.add(enterButton);
    p.add(exitButton);
    enterButton.setEnabled(false);
    p.setBounds(500, 30, 250, 70);
    Panel p2 = new Panel();
    p2.setBackground(new Color(255, 255, 100));
    p2.setLayout(new BorderLayout());
    Panel p2_1 = new Panel();
    p2_1.add(startButton);
    p2_1.add(stopButton);
    p2.add(pInfo, "North");
    p2.add(pList, "Center");
    p2.add(p2_1, "South");
    startButton.setEnabled(false);
    stopButton.setEnabled(false);
    p2.setBounds(500, 110, 250, 180);
    Panel p3 = new Panel();
    p3.setLayout(new BorderLayout());
    p3.add(msgView, "Center");
    p3.add(sendBox, "South");
    p3.setBounds(500, 300, 250, 250);
    add(p);
    add(p2);
    add(p3);
    sendBox.addActionListener(this);
    enterButton.addActionListener(this);
    exitButton.addActionListener(this);
    startButton.addActionListener(this);
    stopButton.addActionListener(this);
    setSize(760, 560);
    setVisible(true);
    connect();
    // addWindowListener(new WindowAdapter() {
    // public void windowClosing(WindowEvent we) {
    // System.exit(0);
    // }
    // });
  }

  public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == sendBox) {
      String msg = sendBox.getText();
      if (msg.length() == 0)
        return;
      if (msg.length() >= 30)
        msg = msg.substring(0, 30);
      try {
        writer.println("[MSG]" + msg);
        sendBox.setText("");
      } catch (Exception ie) {}
    } else if (ae.getSource() == enterButton) {
      try {
        if (Integer.parseInt(roomBox.getText()) < 1) {
          infoView.setText("房间号错误. 必须大于0");
          return;
        }
        writer.println("[ROOM]" + Integer.parseInt(roomBox.getText()));
        msgView.setText("");
      } catch (Exception ie) {
        infoView.setText("输入的事项发生错误.");
      }
    } else if (ae.getSource() == exitButton) {
      try {
        goToWaitRoom();
        startButton.setEnabled(false);
        stopButton.setEnabled(false);
      } catch (Exception e) {}
    } else if (ae.getSource() == startButton) {
      try {
        writer.println("[START]");
        infoView.setText("等待对方决定.");
        startButton.setEnabled(false);
      } catch (Exception e) {}
    } else if (ae.getSource() == stopButton) {
      try {
        writer.println("[DROPGAME]");
        endGame("已弃权.");
      } catch (Exception e) {}
    }
  }

  void goToWaitRoom() {
    if (userName == null) {
      String name = nameBox.getText().trim();
      if (name.length() <= 1 || name.length() > 9) {
        infoView.setText("名字错误. 2~10个字");
        nameBox.requestFocus();
        return;
      }
      userName = name;
      writer.println("[NAME]" + userName);
      nameBox.setText(userName);
      nameBox.setEditable(false);
    }
    msgView.setText("");
    writer.println("[ROOM]0");
    infoView.setText("已进入待机室.");
    roomBox.setText("0");
    enterButton.setEnabled(true);
    exitButton.setEnabled(false);
  }

  public void run() {
    String msg;
    try {
      while ((msg = reader.readLine()) != null) {
        if (msg.startsWith("[STONE]")) {
          String temp = msg.substring(7);
          int x = Integer.parseInt(temp.substring(0, temp.indexOf(" ")));
          int y = Integer.parseInt(temp.substring(temp.indexOf(" ") + 1));
          board.putOpponent(x, y);
          board.setEnable(true);
        } else if (msg.startsWith("[ROOM]")) {
          if (!msg.equals("[ROOM]0")) {
            enterButton.setEnabled(false);
            exitButton.setEnabled(true);
            infoView.setText(msg.substring(6) + "号房间已被进入.");
          } else
            infoView.setText("已进入待机室.");
          roomNumber = Integer.parseInt(msg.substring(6));
          if (board.isRunning()) {
            board.stopGame();
          }
        } else if (msg.startsWith("[FULL]")) {
          infoView.setText("房间已满,禁止入内.");
        } else if (msg.startsWith("[PLAYERS]")) {
          nameList(msg.substring(9));
        } else if (msg.startsWith("[ENTER]")) {
          pList.add(msg.substring(7));
          playersInfo();
          msgView.append("[" + msg.substring(7) + "]入场.\n");
        } else if (msg.startsWith("[EXIT]")) {
          pList.remove(msg.substring(6));
          playersInfo();
          msgView.append("[" + msg.substring(6) + "]进入其他房间.\n");
          if (roomNumber != 0)
            endGame("对方离开.");
        } else if (msg.startsWith("[DISCONNECT]")) {
          pList.remove(msg.substring(12));
          playersInfo();
          msgView.append("[" + msg.substring(12) + "]中断连接.\n");
          if (roomNumber != 0)
            endGame("对方离开.");
        } else if (msg.startsWith("[COLOR]")) {
          String color = msg.substring(7);
          board.startGame(color);
          if (color.equals("BLACK"))
            infoView.setText("得到黑子.");
          else
            infoView.setText("得到白子.");
          stopButton.setEnabled(true);
        } else if (msg.startsWith("[DROPGAME]"))
          endGame("对方弃权.");
        else if (msg.startsWith("[WIN]"))
          endGame("获胜.");
        else if (msg.startsWith("[LOSE]"))
          endGame("失败.");
        else
          msgView.append(msg + "\n");
      }
    } catch (IOException ie) {
      msgView.append(ie + "\n");
    }
    msgView.append("连接中断.");
  }

  private void endGame(String msg) {
    infoView.setText(msg);
    startButton.setEnabled(false);
    stopButton.setEnabled(false);
    try {
      Thread.sleep(2000);
    } catch (Exception e) {}
    if (board.isRunning())
      board.stopGame();
    if (pList.getItemCount() == 2)
      startButton.setEnabled(true);
  }

  private void playersInfo() {
    int count = pList.getItemCount();
    if (roomNumber == 0)
      pInfo.setText("待机室: " + count + "名");
    else
      pInfo.setText(roomNumber + " 号房: " + count + "名");
    if (count == 2 && roomNumber != 0)
      startButton.setEnabled(true);
    else
      startButton.setEnabled(false);
  }

  private void nameList(String msg) {
    pList.removeAll();
    StringTokenizer st = new StringTokenizer(msg, "\t");
    while (st.hasMoreElements())
      pList.add(st.nextToken());
    playersInfo();
  }

  private void connect() {
    try {
      msgView.append("请求连接服务器.\n");
      socket = new Socket("127.0.0.1", 7777);
      msgView.append("---连接成功--.\n");
      msgView.append("请输入名字,然后进入待机室.\n");
      reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      writer = new PrintWriter(socket.getOutputStream(), true);
      new Thread(this).start();
      board.setWriter(writer);
    } catch (Exception e) {
      msgView.append(e + "\n\n连接失败..\n");
    }
  }
}



关联内容

有一个五子棋程序,多人,支持多房间-【客户端】2009-6-8 18:00:26
有一个五子棋程序,多人,支持多房间-【Applet客户端】2009-6-8 18:00:26
有一个五子棋程序,多人,支持多房间-【服务器端】2009-6-8 18:00:26
五子棋程序,支持多人,多线程2009-6-8 18:00:26
控制台的五子棋java程序2009-6-8 18:00:26
五子棋服务器端程序随系统启动的问题2009-6-8 18:00:26
有一段多人聊天的服务器,客户端程序2009-6-8 18:00:26
诺基亚5320XM支持那些格式的程序.软件.游戏?2009-7-23 12:12:03
五子棋进入小学课堂(1)(图)2009-6-13 2:38:12
五子棋进入小学课堂(2)(图)2009-6-13 2:38:13
我有一个tc的五子棋源代码,怎么制作出游戏exe 啊?2009-7-18 23:04:04
有那么多人支持中国足球,为什么没人愿意参与讨论?2009-7-21 5:51:31
索爱的w910手机不是智能机但却支持多开程序,我想问问它和智能机有什么差距呢???2009-7-18 18:04:01
桌面上有一个程序删除不了,想删除掉,怎么办2009-7-18 10:40:46
错误信息:您的程序缺少一个名为“ 数据库操作支持库1.4版”的易语言支持库,其文件名为“shell.fnr或shell.fne“,数字签名为46E94341933A462383A4DE26322C,2009-7-23 17:18:35
电脑启东时有一个程序对话窗上面好多一行英文字母2009-7-19 18:17:35
下五子棋有什么方法?2009-7-22 21:09:10
朝阳区五子棋大赛明举行2009-7-16 5:09:05
玩五子棋有什么取胜要诀?2009-7-23 10:45:28
五子棋考段位是考有禁手的吗?2009-7-20 18:14:28
Copyright © 2009 老紫竹
网站地图 | 最新文章 | 未读文章 | 24小时内热点文章 |津ICP备09000085号