JAVA实现分布式三子棋(文档化注释,可联网)

  • B5_569111
    了解作者
  • 9.7KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-15 05:28
    上传日期
程序说明: 当第一次开启一盘新棋局时,由玩家1先下,此盘结束后,下一盘开始,由输的一方先下,若平局,则下一盘开始由对方先下.当双方不再继续对战,即其中有一方退出或者双方退出后,重新开启的棋局依然由玩家1先下.
src.rar
  • tserver
  • ServerFrame.java
    11.6KB
  • Config.java
    419B
  • tclient
  • ClientFrame.java
    13.5KB
  • GameClient.java
    821B
  • Pawn.java
    2.8KB
  • LastPawn.java
    2.6KB
内容介绍
package tclient; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ConnectException; import java.net.Socket; import java.net.SocketException; /** * 客户端程序 * @author Administrator * */ public class ClientFrame extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private int size = 3; private Pawn[][] pawn = null; private JPanel panel; private MsgPanel msgPanel,toppanel; private JLabel msg,info,wins; private Font font = new Font("宋体",Font.PLAIN,12); private Socket s = null; private DataOutputStream dos = null; private boolean white ; /** * 是否可以走棋 */ private boolean move = false; private String pos_w=",",pos_b=",",opw = "",opb="",odw=""; /** * 查看最后画面 */ public JButton btn ; private Receive receive; private String host = "127.0.0.1"; private int port = 9999; private FlowLayout out ; private int skin ; /** * 创建客户端框架 * @param host * @param port * @param skin */ public ClientFrame(String host,int port,int skin) { this.setTitle("三子棋游戏"); this.host = host; this.port = port; this.size = 3; this.skin = skin; int left = (int)(this.getToolkit().getScreenSize().getWidth()-size*100)/2; int top = (int)(this.getToolkit().getScreenSize().getHeight()-size*100-60)/2 ; this.setBounds(left,top,size*100,size*100+90); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); toppanel = new MsgPanel(skin); toppanel.setPreferredSize(new Dimension(size*100,30)); wins = new JLabel(); wins.setFont(font); wins.setForeground(Color.blue); wins.setPreferredSize(new Dimension(size*100-80,20)); wins.setText("游戏状态:准备就绪"); toppanel.add(wins,BorderLayout.CENTER); btn = new JButton(" "); btn.setPreferredSize(new Dimension(60,20)); btn.setContentAreaFilled(false); btn.setFocusPainted(false); btn.setForeground(Color.blue); btn.setBorder(null); btn.setFont(font); btn.addActionListener(this); toppanel.add(btn,BorderLayout.EAST); this.add(toppanel); panel = new JPanel(new GridLayout(size,size)); panel.setPreferredSize(new Dimension(size*100,size*100)); panel.setBackground(Color.red); panel.setOpaque(false); pawn = new Pawn[size][size]; int tmp = 1; for(int i=0;i<size;i++) { for(int j=0;j<size;j++) { pawn[i][j] = new Pawn(white,this,tmp,skin); panel.add(pawn[i][j]); tmp++; } } this.add(panel); msgPanel = new MsgPanel(skin); msgPanel.setLayout(new FlowLayout()); msgPanel.setPreferredSize(new Dimension(size*100,30)); msgPanel.setOpaque(true); msg = new JLabel(); msg.setPreferredSize(new Dimension(100,20)); msg.setText("正在连接服务器..."); msg.setForeground(Color.blue); msg.setFont(font); msgPanel.add(msg,BorderLayout.SOUTH); info = new JLabel(); info.setPreferredSize(new Dimension(size*100-120,20)); info.setText(""); info.setForeground(Color.blue); info.setFont(font); msgPanel.add(info,BorderLayout.CENTER); this.add(msgPanel); out = new FlowLayout(); out.setHgap(0); out.setVgap(0); this.setLayout(out); this.setResizable(false); this.validate(); /** * 与服务器连接 */ this.connect(); } /** * 设置提示 * @param str */ public void setInfo(String str) { this.info.setText(str); } /** * 设置状态 * @param str */ public void setGameState(String str) { wins.setText(str); } /** * 返回当前是否可以移动 * @param move */ public void setMove(boolean move) { this.move = move; } /** * 返回当前是否可以移动 * @return */ public boolean getMove() { return this.move; } class WindowMonitor extends WindowAdapter { public void windowClosing(WindowEvent e) { setVisible(false); disconnect(); System.exit(0); } } /** * 与服务器建立连接 */ public void connect() { try { s = new Socket(host, port); dos = new DataOutputStream(s.getOutputStream()); receive = new Receive(this); Thread t = new Thread(receive); t.start(); /** * 显示 */ this.setVisible(true); } catch (ConnectException e) { disconnect(); } catch (IOException e) { e.printStackTrace(); } } /** * 显示连接信息 */ public void notConnect() { this.setVisible(false); JOptionPane.showMessageDialog(this, "与服务器建立连接失败,服务器未开启或已关闭可网络不通,请检查"); System.exit(0); } /** * 与服务区断开连接 */ public void disconnect() { notConnect() ; try { if(dos != null) dos.close(); if(s != null) s.close(); } catch (IOException e) { msg.setText("关闭连接失败"); info.setText(""); } } /** * 向服务器发送信息 * @param str */ public void send(String str) { wins.setText("游戏状态:正在游戏"); try { dos.writeUTF(str); dos.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } } /** * 游戏结果 * @param boo */ public void isEnd(boolean boo) { if(boo) { wins.setText("游戏状态:恭喜您,您赢了"); if(white) odw = "w"; else odw = "b"; } else { wins.setText("游戏状态:游戏结束-平局"); odw = ""; } new LastPawn(size, opw, opb,odw,skin); receive.addPawn(white); } /** * 我的选择 * @param pos */ public void addWChoose(String pos) { this.pos_w += pos + ","; } /** * 返回我的选择 * @return */ public String getWChoose() { return this.pos_w; } /** * 对方的选择 * @param pos */ public void addBChoose(String pos) { this.pos_b += pos + ","; } /** * 返回对方的选择 * @return */ public String getBChoose() { return this.pos_b; } /** * 比赛结果:0平,1玩家1赢,-1玩家2赢 * @return */ public int Win() { /** * 默认值 */ int ret = -1; int tpos = size * size ; String[] posw = pos_w.split(","); String[] posb = pos_b.split(","); int nowchoose = posw.length + posb.length-2; String win[] = null; win = new String("1-2-3,4-5-6,7-8-9,1-4-7,2-5-8,3-6-9,1-5-9,3-5-7").split(","); for(int i=0;i<win.length;i++) { String[] str = win[i].split("-"); int mc = 0; for(int j=0;j<str.length;j++) { if(pos_w.indexOf(","+str[j]+",") != -1) { mc++; } } if(mc == str.length) { ret = 1; setOldPwan(); break; } } if(tpos == nowchoose && ret == -1) { ret = 0; setOldPwan(); } return ret; } /** * 设置最后画面 */ public void setOldPwan() { if(white) { opw = pos_w; opb = pos_b; } else { opw = pos_b; opb = pos_w; } btn.setText(" 最后画面 "); } /** * 接收信息,传送当前窗体 * @author Administrator * */ class Receive implements Runnable { private DataInputStream dis = null; private boolean startReceive = false; private ClientFrame frame; Receive(ClientFrame frame) { this.frame = frame; try { dis = new DataInputStream(s
评论
    相关推荐