我寫了一個網路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家唱片公司合法授權 音樂盡情下載
--
java 近期熱門文章
PTT數位生活區 即時熱門文章