java 板


我写了一个网路client / server 的井字游戏,但是我遇到一个问题@@ 当A选择完位置後 换B选择,此时A 应该是不能有动作的~请问有人可以帮我修改吗? //==============SERVER========// /* 程式范例: ChatServer.java */ import java.net.*; import java.io.*; import java.util.*; // 聊天室使用者执行绪类别 class ChatUserThread extends Thread { private static HashSet<ChatUserThread> userThreadList = new HashSet<ChatUserThread>(); public static int userCount = 0; // 记录聊天室的人数 public static int NUM = 9; // 记录顺序 private Socket socket; private DataInputStream in; private DataOutputStream out; static String [][] game={{"1","2","3"}, {"4","5","6"}, {"7","8","9"}}; // 建构子 public ChatUserThread(Socket socket) throws IOException { this.socket = socket; // 建立串流物件 BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); in = new DataInputStream(bis); BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); out = new DataOutputStream(bos); userCount++; } // 执行绪的执行方法 public void run() { // 取得IP位址 String address = socket.getInetAddress().toString(); String user = ""; // 使用者名称 try { user = in.readUTF(); // 读取字串, 使用UTF-8加码 userThreadList.add(this); sendMsgs("<"+user+"("+address+")>-- 进入聊天室"); while (true) { // 读取使用者张贴的讯息, 使用UTF-8加码 String msg = in.readUTF(); // 张贴讯息给聊天室的所有使用者 sendMsgs("<"+user+"("+address+")>"+msg); OX(msg);//进行游戏判断 for(int a1=0 ;a1<3;a1++) { sendMsgs(game[a1][0] +""+ game[a1][1] +""+game[a1][2] ); } } } catch ( IOException e ) { } finally { // 删除使用者执行绪物件 userThreadList.remove(this); sendMsgs("<"+user+"("+address+")>-- 离开聊天室"); userCount--; System.out.println("目前聊天室的人数: " + userCount); if(userCount==0) { game[0][0]="1"; game[0][1]="2"; game[0][2]="3"; game[1][0]="4"; game[1][1]="5"; game[1][2]="6"; game[2][0]="7"; game[2][1]="8"; game[2][2]="9"; NUM=9; } try { socket.close(); } // 关闭Socket物件 catch ( IOException e ) { } } } public static void OX(String message) { int message3=Integer.valueOf(message); if(NUM!=0) { if(NUM%2==1) { NUM--; switch (message3) { case 1: //message2="1"; game[0][0]="O"; break; case 2: //message2="2"; game[0][1]="O"; break; case 3: //message2="3"; game[0][2]="O"; break; case 4: //message2="4"; game[1][0]="O"; break; case 5: //message2="5"; game[1][1]="O"; break; case 6: //message2="6"; game[1][2]="O"; break; case 7: //message2="7"; game[2][0]="O"; break; case 8: //message2="8"; game[2][1]="O"; break; case 9: // message2="9"; game[2][2]="O"; break; default: sendMsgs("输入错误"); } } else { NUM--; switch (message3) { case 1: //message2="1"; game[0][0]="X"; break; case 2: //message2="2"; game[0][1]="X"; break; case 3: //message2="3"; game[0][2]="X"; break; case 4: //message2="4"; game[1][0]="X"; break; case 5: //message2="5"; game[1][1]="X"; break; case 6: //message2="6"; game[1][2]="X"; break; case 7: //message2="7"; game[2][0]="X"; break; case 8: //message2="8"; game[2][1]="X"; break; case 9: //message2="9"; game[2][2]="X"; break; default: sendMsgs("输入错误"); } } } else { sendMsgs("错误"); } if(TEST(game)==1) { if(NUM%2==0) { sendMsgs("<<选择第一个(O)的人胜利>>"); } else { sendMsgs("<<选择第二个(x)的人胜利>>"); } } if(NUM==0) { sendMsgs("<<平手>>" ); } } public static int TEST(String game[][]) { for(int a2=0;a2<=2;a2++) { if((game[a2][0]==game[a2][1])&&(game[a2][1]==game[a2][2])) { return 1; } if((game[0][a2]==game[1][a2])&&(game[1][a2]==game[2][a2])) { return 1; } } if((game[0][0]==game[1][1])&&(game[1][1]==game[2][2])) { return 1; } if((game[2][0]==game[1][1])&&(game[1][1]==game[0][2])) { return 1; } return 0; } // 广播讯息给所有的使用者 public static void sendMsgs(String message) { synchronized(userThreadList) { // 使用Iterator介面物件来取得HashSet元素 Iterator<ChatUserThread> iterator = userThreadList.iterator(); // 取出所有的使用者执行绪物件 while (iterator.hasNext()) { ChatUserThread currentUser = iterator.next(); try { synchronized(currentUser.out) { // 送出讯息, 使用UTF-8加码 currentUser.out.writeUTF(message); } currentUser.out.flush(); } catch ( IOException e ) { } } } } } // 主类别 public class ChatServer { // 主程式 public static void main(String args[]) { // 取得命令列参数 int num =0; if (args.length != 1) { System.out.println("使用: ChatServer <port>"); return; } int port = Integer.parseInt(args[0]); // 取得埠号 try // 建立ServerSocket物件 { ServerSocket server = new ServerSocket(port); System.out.println(server); System.out.println("Java聊天室已经启动(按Ctrl-C关闭)..."); System.out.println("使用通讯埠: " + port); System.out.println("等待客户端连线中......"); // 等待连线的无穷回圈 while (true) { // 建立客户端Socket物件 System.out.println("加入" ); Socket client = server.accept(); System.out.println("客户端连线的IP位址: " + client.getInetAddress()); // 建立聊天室使用者的执行绪物件 ChatUserThread currentUser = new ChatUserThread(client); currentUser.start(); // 启动执行绪 System.out.println("目前聊天室的人数: " + ChatUserThread.userCount); } } catch ( Exception e ) { e.printStackTrace(); return; } } } //==============================CLIENT=================================// /* 程式范例: ChatClient.java */ import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; // 继承JFrame, 实作Runnable和ActionListener介面 public class ChatClient extends JFrame implements Runnable, ActionListener { private Container c; private DataInputStream in; private DataOutputStream out; private String user; private JTextArea output; private JTextArea output2; private JTextField input; private Thread client; static char [][] game= new char[3][3]; static String msg ; // 建构子 public ChatClient(String title, String user, InputStream in, OutputStream out) { super(title); // 建立串流物件 this.in = new DataInputStream(new BufferedInputStream(in)); this.out = new DataOutputStream(new BufferedOutputStream(out)); this.user = user; // 使用者名称 c = getContentPane(); c.setLayout(new FlowLayout(FlowLayout.CENTER)); // 建立Swing元件的使用介面 JLabel label = new JLabel(user+"讯息: "); input = new JTextField("", 25); input.requestFocus(); JButton button = new JButton("送出"); button.addActionListener(this); output = new JTextArea("欢迎进入Java聊天室...\n", 15, 18); output.setEditable(false); // 不可编辑 JScrollPane area = new JScrollPane(output); output2 = new JTextArea("欢迎进入Java聊天室...\n", 15, 18); output2.setEditable(false); // 不可编辑 JScrollPane area2 = new JScrollPane(output2); c.add(area); c.add(area2); c.add(label); c.add(input); c.add(button); // 建立执行绪物件 client = new Thread(this); client.start(); game[0][0]='1'; game[0][1]='2'; game[0][2]='3'; game[1][0]='4'; game[1][1]='5'; game[1][2]='6'; game[2][0]='7'; game[2][1]='8'; game[2][2]='9'; } // 实作执行绪的run方法 public void run() { try // 送出使用者名称字串, 使用UTF-8加码 { out.writeUTF(user); out.flush(); // 接收聊天讯息 while (true) { // 读取讯息, 使用UTF-8加码 msg = in.readUTF(); output2.append(msg + "\n"); // 显示讯息 } } catch ( IOException e ) { try // 关闭串流 { in.close(); out.close(); } catch ( IOException e2 ) { } System.exit(0); // 结束应用程式 } } // 实作事件处理方法 public void actionPerformed(ActionEvent evt) { try // 送出讯息, 使用UTF-8加码 { out.writeUTF(input.getText()); out.flush(); } catch ( IOException e ) { e.printStackTrace(); } input.setText(""); // 清除文字方块 input.requestFocus(); } // 主程式 public static void main (String args[]) throws Exception { // 取得命令列参数 if (args.length != 3) { System.out.println("使用: ChtClient <Server> <Port> <Username>"); return; } int port = Integer.parseInt(args[1]); // 取得埠号 // 建立Socket物件 Socket socket = new Socket(args[0], port); String title = "Java聊天室: "+args[0]+"/"+args[1]; ChatClient cc = new ChatClient(title, args[2], socket.getInputStream(), socket.getOutputStream()); cc.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); cc.setSize(450,400); cc.setVisible(true); } } -- ┌─────KKCITY─────┐ KKBOX歌名歌手歌词专辑搜寻 bbs.kkcity.com.tw http://www.kkbox.com.tw └──From:125.233.3.52 ──┘ 超过60家唱片公司合法授权 音乐尽情下载 --







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草
伺服器连线错误,造成您的不便还请多多包涵!
「赞助商连结」






like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP