看板java
标 题程式修改
发信站KKCITY (Thu Mar 23 02:49:25 2006)
转信站ptt!ctu-reader!Spring!news.nctu!news.ntu!bbs.ee.ntu!news.kkcity.com.tw
我写了一个网路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家唱片公司合法授权 音乐尽情下载
--