import java.awt.*;
import java.io.*;
import java.applet.*;
import java.applet.Applet;
import java.net.URL;
import java.net.Socket;
import java.net.InetAddress;
import java.net.MalformedURLException;
//import gameframe;public class Netgame extends Applet implements Runnable{ ControlPanel Control;
GridBagLayout gridbag = new GridBagLayout(); String name="";
int otherId; public static int travel[];
int final_die[]=new int[21]; public final int r = 34; // 半径
int turn=1; // 1: blue, 2:red
int myturn=1; // 1 or 2;
int mode=0;
static int play=1;
static int calling=-2;
static int idle=0;
static int called=-1;
public int height=0;
public int width=0; Image offscreen;
Graphics offGraph;
Image pic; public static Point center[]=new Point[21];
// 0-nothing 1-blue 2-red
public static int state[]=new int[21];
public static int neighbor[][]=new int[21][4];
Point now_point=null;
int now_id=-1; Socket sock;
DataInputStream datain;
DataOutputStream dataout;
Thread m_mygame = null;
boolean m_fAllLoaded=false; AudioClip a_one,a_back,a_eat,a_eatted,a_win,a_start,a_your_turn,a_lose; public void ini_global(){
myturn=1;
mode=idle;
otherId=-1;
turn=1;
for (int i=0;i<21;i++){ center[i]=new Point(0,0);} center[0].x=190;
center[0].y=21;
center[1].x=131;
center[1].y=32;
center[2].x=254;
center[2].y=35;
center[3].x=190;
center[3].y=81;
center[4].x=32;
center[4].y=129;
center[5].x=190;
center[5].y=127;
center[6].x=346;
center[6].y=129;
center[7].x=22;
center[7].y=190;
center[8].x=85;
center[8].y=190;
center[9].x=130;
center[9].y=190;
center[10].x=190;
center[10].y=190;
center[11].x=255;
center[11].y=190;
center[12].x=300;
center[12].y=190;
center[13].x=358;
center[13].y=190;
center[14].x=35;
center[14].y=252;
center[15].x=190;
center[15].y=250;
center[16].x=345;
center[16].y=250;
center[17].x=190;
center[17].y=295;
center[18].x=130;
center[18].y=345;
center[19].x=253;
center[19].y=343;
center[20].x=190;
center[20].y=355;
neighbor[0][0]=1;
neighbor[0][1]=2;
neighbor[0][2]=3;
neighbor[0][3]=-1;
neighbor[1][0]=0;
neighbor[1][1]=3;
neighbor[1][2]=4;
neighbor[1][3]=-1;
neighbor[2][0]=0;
neighbor[2][1]=3;
neighbor[2][2]=6;
neighbor[2][3]=-1;
neighbor[3][0]=0;
neighbor[3][1]=1;
neighbor[3][2]=2;
neighbor[3][3]=5;
neighbor[4][0]=1;
neighbor[4][1]=7;
neighbor[4][2]=8;
neighbor[4][3]=-1;
neighbor[5][0]=3;
neighbor[5][1]=9;
neighbor[5][2]=10;
neighbor[5][3]=11;
neighbor[6][0]=2;
neighbor[6][1]=12;
neighbor[6][2]=13;
neighbor[6][3]=-1;
neighbor[7][0]=4;
neighbor[7][1]=8;
neighbor[7][2]=14;
neighbor[7][3]=-1;
neighbor[8][0]=4;
neighbor[8][1]=7;
neighbor[8][2]=9;
neighbor[8][3]=14;
neighbor[9][0]=5;
neighbor[9][1]=8;
neighbor[9][2]=10;
neighbor[9][3]=15;
neighbor[10][0]=5;
neighbor[10][1]=9;
neighbor[10][2]=11;
neighbor[10][3]=15;
neighbor[11][0]=5;
neighbor[11][1]=10;
neighbor[11][2]=12;
neighbor[11][3]=15;
neighbor[12][0]=6;
neighbor[12][1]=11;
neighbor[12][2]=13;
neighbor[12][3]=16;
neighbor[13][0]=6;
neighbor[13][1]=12;
neighbor[13][2]=16;
neighbor[13][3]=-1;
neighbor[14][0]=7;
neighbor[14][1]=8;
neighbor[14][2]=18;
neighbor[14][3]=-1;
neighbor[15][0]=9;
neighbor[15][1]=10;
neighbor[15][2]=11;
neighbor[15][3]=17;
neighbor[16][0]=12;
neighbor[16][1]=13;
neighbor[16][2]=19;
neighbor[16][3]=-1;
neighbor[17][0]=15;
neighbor[17][1]=18;
neighbor[17][2]=19;
neighbor[17][3]=20;
neighbor[18][0]=14;
neighbor[18][1]=17;
neighbor[18][2]=20;
neighbor[18][3]=-1;
neighbor[19][0]=16;
neighbor[19][1]=17;
neighbor[19][2]=20;
neighbor[19][3]=-1;
neighbor[20][0]=17;
neighbor[20][1]=18;
neighbor[20][2]=19;
neighbor[20][3]=-1;
init_circle();
}
import java.io.*;
import java.applet.*;
import java.applet.Applet;
import java.net.URL;
import java.net.Socket;
import java.net.InetAddress;
import java.net.MalformedURLException;
//import gameframe;public class Netgame extends Applet implements Runnable{ ControlPanel Control;
GridBagLayout gridbag = new GridBagLayout(); String name="";
int otherId; public static int travel[];
int final_die[]=new int[21]; public final int r = 34; // 半径
int turn=1; // 1: blue, 2:red
int myturn=1; // 1 or 2;
int mode=0;
static int play=1;
static int calling=-2;
static int idle=0;
static int called=-1;
public int height=0;
public int width=0; Image offscreen;
Graphics offGraph;
Image pic; public static Point center[]=new Point[21];
// 0-nothing 1-blue 2-red
public static int state[]=new int[21];
public static int neighbor[][]=new int[21][4];
Point now_point=null;
int now_id=-1; Socket sock;
DataInputStream datain;
DataOutputStream dataout;
Thread m_mygame = null;
boolean m_fAllLoaded=false; AudioClip a_one,a_back,a_eat,a_eatted,a_win,a_start,a_your_turn,a_lose; public void ini_global(){
myturn=1;
mode=idle;
otherId=-1;
turn=1;
for (int i=0;i<21;i++){ center[i]=new Point(0,0);} center[0].x=190;
center[0].y=21;
center[1].x=131;
center[1].y=32;
center[2].x=254;
center[2].y=35;
center[3].x=190;
center[3].y=81;
center[4].x=32;
center[4].y=129;
center[5].x=190;
center[5].y=127;
center[6].x=346;
center[6].y=129;
center[7].x=22;
center[7].y=190;
center[8].x=85;
center[8].y=190;
center[9].x=130;
center[9].y=190;
center[10].x=190;
center[10].y=190;
center[11].x=255;
center[11].y=190;
center[12].x=300;
center[12].y=190;
center[13].x=358;
center[13].y=190;
center[14].x=35;
center[14].y=252;
center[15].x=190;
center[15].y=250;
center[16].x=345;
center[16].y=250;
center[17].x=190;
center[17].y=295;
center[18].x=130;
center[18].y=345;
center[19].x=253;
center[19].y=343;
center[20].x=190;
center[20].y=355;
neighbor[0][0]=1;
neighbor[0][1]=2;
neighbor[0][2]=3;
neighbor[0][3]=-1;
neighbor[1][0]=0;
neighbor[1][1]=3;
neighbor[1][2]=4;
neighbor[1][3]=-1;
neighbor[2][0]=0;
neighbor[2][1]=3;
neighbor[2][2]=6;
neighbor[2][3]=-1;
neighbor[3][0]=0;
neighbor[3][1]=1;
neighbor[3][2]=2;
neighbor[3][3]=5;
neighbor[4][0]=1;
neighbor[4][1]=7;
neighbor[4][2]=8;
neighbor[4][3]=-1;
neighbor[5][0]=3;
neighbor[5][1]=9;
neighbor[5][2]=10;
neighbor[5][3]=11;
neighbor[6][0]=2;
neighbor[6][1]=12;
neighbor[6][2]=13;
neighbor[6][3]=-1;
neighbor[7][0]=4;
neighbor[7][1]=8;
neighbor[7][2]=14;
neighbor[7][3]=-1;
neighbor[8][0]=4;
neighbor[8][1]=7;
neighbor[8][2]=9;
neighbor[8][3]=14;
neighbor[9][0]=5;
neighbor[9][1]=8;
neighbor[9][2]=10;
neighbor[9][3]=15;
neighbor[10][0]=5;
neighbor[10][1]=9;
neighbor[10][2]=11;
neighbor[10][3]=15;
neighbor[11][0]=5;
neighbor[11][1]=10;
neighbor[11][2]=12;
neighbor[11][3]=15;
neighbor[12][0]=6;
neighbor[12][1]=11;
neighbor[12][2]=13;
neighbor[12][3]=16;
neighbor[13][0]=6;
neighbor[13][1]=12;
neighbor[13][2]=16;
neighbor[13][3]=-1;
neighbor[14][0]=7;
neighbor[14][1]=8;
neighbor[14][2]=18;
neighbor[14][3]=-1;
neighbor[15][0]=9;
neighbor[15][1]=10;
neighbor[15][2]=11;
neighbor[15][3]=17;
neighbor[16][0]=12;
neighbor[16][1]=13;
neighbor[16][2]=19;
neighbor[16][3]=-1;
neighbor[17][0]=15;
neighbor[17][1]=18;
neighbor[17][2]=19;
neighbor[17][3]=20;
neighbor[18][0]=14;
neighbor[18][1]=17;
neighbor[18][2]=20;
neighbor[18][3]=-1;
neighbor[19][0]=16;
neighbor[19][1]=17;
neighbor[19][2]=20;
neighbor[19][3]=-1;
neighbor[20][0]=17;
neighbor[20][1]=18;
neighbor[20][2]=19;
neighbor[20][3]=-1;
init_circle();
}
int i;
turn=1;
for (i=0;i<21;i++){
state[i]=0;
}
for (i=0;i<=6;i++){
state[i]=myturn%2+1;
}
for (i=14;i<=20;i++){
state[i]=myturn;
}
state[5]=0;
state[15]=0;
repaint();
} public void destroy(){
message("$b\n");
stop();
} loginframe namedlg; public void init(){
height=380;
width=380; namedlg = new loginframe(this,"User Name"); // Loading Sound a_start = getAudioClip(getCodeBase(),"start.au");
a_one = getAudioClip(getCodeBase(),"one.au");
a_back = getAudioClip(getCodeBase(),"back.au");
a_your_turn = getAudioClip(getCodeBase(),"your_turn.au");
a_eat = getAudioClip(getCodeBase(),"eat.au");
a_eatted = getAudioClip(getCodeBase(),"eatted.au");
a_win = getAudioClip(getCodeBase(),"win.au");
a_lose = getAudioClip(getCodeBase(),"lose.au"); // Loading 棋盘图
pic = getImage(getCodeBase(),"ft.jpg");
offscreen = createImage(width,height);
offGraph = offscreen.getGraphics(); MediaTracker tracker = new MediaTracker(this);
tracker.addImage(pic, 0);
try{
tracker.waitForAll();
m_fAllLoaded = !tracker.isErrorAny();
}
catch (InterruptedException e){
}
} public void start() {
ini_global(); now_id=-1;
//turn=1;
height=380;
width=380;
try {
while (name.equals("")&&!namedlg.ok){
Thread.sleep(2000);
};
}
catch (InterruptedException e){
System.out.println(e.toString());
} setLayout(new BorderLayout(0,0));
Control=new ControlPanel(this);
setBackground(new Color(172,148,136));
add(Control,"West");
Control.reshape(0,5,250,384); if (a_start==null){
play(getCodeBase(),"start.au");
}
else a_start.play(); try{
sock=new Socket(InetAddress.getLocalHost(),4444); //********************************************************************
//* // Change to your own host name
//sock=new Socket(InetAddress.getByName("192.168.1.212"),4444);
datain = new DataInputStream(sock.getInputStream());
dataout = new DataOutputStream(sock.getOutputStream());
if (m_mygame == null){
m_mygame=new Thread(this);
m_mygame.start();
} message("$n"+name+"\n");
message("$w\n");
}
catch (Exception e){};
}
public void stop() {
if (m_mygame != null){
m_mygame.stop();
m_mygame = null;
}
Control.Message=null;
Control.Outgoing=null;
Control.who=null;
Control=null;
try{
dataout.close();
datain.close();
sock.close();
}
catch (IOException e){};
}
public void run(){
while (true){
try{
String msg=null;
msg=datain.readLine(); // 接收welcome msg
if (msg.charAt(0)=='$') // 如果接收到命令
{
switch (msg.charAt(1)){
// 接收到移动棋子
case 'm':
receive_state(msg.substring(2));
break;
case 'w':
Control.do_who(msg.substring(2));
break;
// 有人 call
case 'r':
Control.u_button(0,1,1,0,0);
otherId=Integer.parseInt(msg.substring(2));
mode=called;
break;
// 别人接受
case 'y':
Control.u_button(1,0,0,1,1);
Control.Message.appendText("** Your partner accepted your request.\r\n");
Control.Message.appendText("** Starting the game.\r\n");
Control.Message.appendText("** You are blue (the first player).\r\n");
Control.Message.appendText("** It is your turn now.\r\n");
mode=play;
break;
// 别人不接受
case 'u':
Control.u_button(1,0,0,0,0);
Control.Message.appendText("** Your partner rejected your request.\r\n");
mode=idle;
break;
// user not available
case 's':
Control.u_button(1,0,0,0,0);
Control.Message.appendText("** Your partner is busy.\r\n");
mode=idle;
break;
// 对方要 restart
case 'a':
Control.u_button(1,0,0,1,1);
Control.Message.appendText("** Your partner requested to restart.\r\n");
init_circle();
break;
// 离开棋桌
case 'b':
Control.u_button(1,0,0,0,0);
myturn=1;
Control.Message.appendText("** Your partner quitted.\r\n");
init_circle();
mode=idle;
break;
default:
break;
}
}
else {
Control.Message.appendText(msg+"\r\n");
}
}
catch (Exception e){};
}
}
int i;
//a_one.play(); // 对家下子了
for (i=0;i<21;i++){
if (x.charAt(i)=='1') state[20-i]=1;
else if (x.charAt(i)=='0') state[20-i]=0;
else if (x.charAt(i)=='2') state[20-i]=2;
}
repaint();
Control.Message.appendText("** Your Turn.\r\n");
turn=turn%2+1;
}public void message(String msg){
try{
m_mygame.suspend();
dataout.writeBytes(msg);
dataout.flush();
m_mygame.resume();
}
catch(Exception e){
System.out.println("Send Message error : "+msg);
}
}
// mydraw Paint Handler
public void paint_circle(Graphics g,int x,int y,int color,int move){
if (move==0){
g.setColor(new Color(171,118,46)); //画阴影
g.fillOval(x-2,y+8,24,10);
}
color=Math.abs(color);
if (color==1) g.setColor(Color.blue);
else if (color==2) g.setColor(Color.red);
int a,b,c,d,e;
a=x-r/2;b=y-r/2;
c=x-2*r/5;d=y-2*r/5;
e=4*r/5;
g.fillOval(a,b,r,r);
g.setColor(Color.white);
g.drawArc(c,d,e,e,100,70);
g.drawArc(c,d,e+1,e+1,100,70);
g.drawArc(c,d,e-1,e-1,100,70);
g.drawArc(c,d,e+2,e+2,100,70);
g.setColor(Color.lightGray);
g.drawArc(c,d,e,e,286,35);
g.drawArc(c,d,e-1,e-1,286,35);
g.drawArc(c,d,e-2,e-2,286,33);
}
public void paint(Graphics g){ offGraph.drawImage(pic,0,0,this); // 画背景 int now=-1;
for (int i=0;i<21;i++){
if (state[i]>0){
paint_circle(offGraph,center[i].x,center[i].y,state[i],0);
}
else if (state[i]<0){
now=i;
//paint_circle(g,now_point.x,now_point.y,-state[i],1);
}
}
// now moving
if (now!=-1){
paint_circle(offGraph,now_point.x,now_point.y,-state[now],1);
}
g.drawImage(offscreen,250,5,this);
}
public int find_which(int x,int y){ // return -1 not found
int i; // return i 为 棋子之号码
if (y<100){
for (i=0;i<=3;i++){
if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2)
return i;
}
return -1;
}
else if (y<150){
for (i=4;i<=6;i++){
if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2)
return i;
}
return -1;
}
else if (y<210){
for (i=7;i<=13;i++){
if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2)
return i;
}
return -1;
}
else if (y<270){
for (i=14;i<=16;i++){
if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2)
return i;
}
return -1;
}
else{
for (i=17;i<=20;i++){
if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2)
return i;
}
}
return -1;
}
public int is_neighbor(int x,int y){
int i;
for (i=0;i<4;i++){
if (neighbor[x][i]==y) return 1;
}
return 0;
}
public boolean consider(int color,int id){
int die_list[]=new int[21];
boolean game_over=false;
int i,j;
travel=new int[21];
for (i=0;i<21;i++){
travel[i]=-1; // -1 not travel
die_list[i]=0;
final_die[i]=0;
} // 0 no chess 1 color 1 , 2 color 2
travel[id]=state[id];
boolean has_die=false;
for (i=3;i>=0;i--){
if(die(color,neighbor[id][i],die_list)==1){
for (j=0;j<21;j++){
if (die_list[j]==1) final_die[j]=1;
}
has_die=true;
}
}
int alive_sum=0;
for (i=0;i<21;i++){
if (final_die[i]==1){
state[i]=0;
}
if (state[i]==(color%2+1)) alive_sum++;
} if (alive_sum==0) {
Control.Message.appendText("\n** Game Over.\n");
Control.Message.appendText("\n** You win.\n"); game_over=true;
}
if (game_over) a_win.play();
else if (has_die) a_eat.play();
return game_over;
}
public int die(int color,int id,int[] d_list){
if (id==-1||state[id]==color||travel[id]==-2) return 0;
if (state[id]==0||travel[id]==-3) return -1;
int i,j;
int bak_list[]=new int[21];
for (i=0;i<21;i++){
bak_list[i]=0;
}
if (travel[id]==-1) travel[id]=-2;
for (i=0;i<4;i++){
if (neighbor[id][i]<0) break;
if (travel[neighbor[id][i]]==-1||travel[neighbor[id][i]]!=-2){
int re=die(color,neighbor[id][i],d_list);
if (re==-1){
for (j=0;j<21;j++){
d_list[j]=0;
}
travel[id]=-3;
return -1;
}
else if (re==1){
for (j=0;j<21;j++){
if (d_list[j]!=0){
bak_list[j]=1;
}
}
}
}
}
for (i=0;i<21;i++){
d_list[i]=bak_list[i];
}
d_list[id]=1;
travel[id]=-1;
return 1;
}
switch(evt.id){
case Event.KEY_PRESS:
if (evt.key=='\n'){
try{
if (!Outgoing.getText().equals("")){
mp_game.message(Outgoing.getText()+"\n");
if (Outgoing.getText().indexOf("$n")!=-1){
mp_game.name=Outgoing.getText().substring(2);
mp_game.message("$w\n");
}
Outgoing.setText("");
}
}catch (Exception e){};
}
return super.handleEvent(evt);
case Event.LIST_SELECT:
return true;
default:
return super.handleEvent(evt);
}
}}
class loginframe extends gameframe {
boolean ok=false;
Netgame mp_game;
Label msg;
Button b_ok;
TextField name;
GridBagLayout gridbag=new GridBagLayout(); public loginframe(Netgame mp,String title) {
super(title);
msg = new Label("Please enter user name!", Label.CENTER);
b_ok=new Button("OK");
name=new TextField(10);
mp_game=mp; setLayout(gridbag); setBackground(new Color(172,148,136));
constrain(this,msg,0,0,10,1,GridBagConstraints.HORIZONTAL,
GridBagConstraints.NORTHWEST,1.0,0.0,5,3,2,3);
constrain(this,name,0,10,8,1,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,1.0,0.0,0,3,2,3);
constrain(this,b_ok,4,11,4,1,GridBagConstraints.NONE,
GridBagConstraints.CENTER,1.0,0.0,0,3,5,3); name.setBackground(Color.white);
name.setForeground(Color.black);
name.setFont(new Font("System",Font.BOLD,13));
//Label.setFont(new Font("System",Font.BOLD,12)); pack();
show();
} public boolean action(Event evt,Object what){
if (evt.target instanceof Button){
mp_game.name=name.getText();
if (!mp_game.name.equals("")){
ok=true;
hide();
}
}
return false;
}
public boolean handleEvent(Event evt) {
switch (evt.id) {
case Event.WINDOW_DESTROY:
return true;
case Event.KEY_PRESS:
if (evt.key=='\n'){
mp_game.name=name.getText();
if (!mp_game.name.equals("")){
ok=true;
hide();
}
name.setText("");
}
default:
return super.handleEvent(evt);
}
}
}
class gameframe extends Frame
{
// mydrawFrame constructor
//--------------------------------------------------------------------------
public gameframe(String str)
{
super (str);
} public static void constrain(Container container,Component component,
int grid_x,int grid_y,int grid_width,int grid_height,
int fill,int anchor,double weight_x,double weight_y,
int top,int left,int bottom,int right){
GridBagConstraints c=new GridBagConstraints();
c.gridx=grid_x;c.gridy=grid_y;
c.gridwidth=grid_width;c.gridheight=grid_height;
c.fill=fill;c.anchor=anchor;
c.weightx=weight_x;c.weighty=weight_y;
if (top+bottom+left+right>0){
c.insets=new Insets(top,left,bottom,right);
}
((GridBagLayout)container.getLayout()).setConstraints(component,c);
container.add(component); } public static void constrain(Container container,Component component,
int grid_x,int grid_y,int grid_width,int grid_height){ constrain(container,component,grid_x,grid_y,
grid_width,grid_height,GridBagConstraints.NONE,
GridBagConstraints.NORTHWEST,
0.0,0.0,0,0,0,0);
} public static void constrain(Container container,Component component,
int grid_x,int grid_y,int grid_width,int grid_height,
int top,int left,int bottom,int right){
constrain(container,component,grid_x,grid_y,
grid_width,grid_height,GridBagConstraints.NONE,
GridBagConstraints.NORTHWEST,
0.0,0.0,top,left,bottom,right);
} public boolean handleEvent(Event evt)
{
switch (evt.id)
{
case Event.WINDOW_DESTROY:
dispose();
System.exit(0);
return true; default:
return super.handleEvent(evt);
}
}
}
public class gameserver
{
static Vector clientList=new Vector(10);
static ServerSocket ss;
public static void main (String arg[])
{
try
{
ss= new ServerSocket(4444);
}catch(IOException e)
{
System.out.println("Error"+e);
}
while(true)
{
if(clientList.size()<10)
{
try
{
Socket theSocket = ss.accept();
System.out.println(socket);
}
catch(IOException e)
{
System.out.println("Error : "+e);
}
}
}
}
}
class ServerThread extends Thread {
public ServerThread(Socket m,int me,
client_data[] mp_client,table_data[] mp_table)
throws IOException
{
int g;
mySocket =m;
myId=me;
client =mp_client;
table= mp_table;
//接收
datatain = new DataInputStream (mySocket.getlnputStream());
//返回
dataout = new DataOutputStream(mySocket.getoutputStream());
}
}
class client_data {
int myId;
int otherId;
ServerThread thread;
String name;
int mode;
int table_id;
ServerSockeL socket;
final stactic int MAX=20;
static int Idle=0;
static int called=-1;
static int calling=-2;
static int playing=1;
client_data(int id)
{
myId=id;
otherld=0;
mode=Idle;
name=new String("");
table_id=-1;
}
}
public void do_restart(){
int oo=-1;
try{
System.out.println("***" +
client[myId].name + " send Restart.");
oo=table[client[myId].table_id].Player1_id;
if (oo==myId) oo=table[client[myId].table_id].Player2_id;
if (client[oo].thread.isAllve()){
client[oo].thread.dataout.writeBytes("$a\n");
client[oo].thread.dataout.flush();
}
}
catch(IOException e) {
System.out.println("Error occurred in do_restart()."+oo+" "+myId);
}
}public void do_exit(){
int oo =-1;
if (client[myId].mode!=client_data.playing) return;
try{
System.out.println("***" +
client[myId].name + " send exit.");
oo=table[client[myId].table_id].Player1_id;
if (oo==myId) oo=table[client[myId].table_id].Player2_id;
if (client[oo].thread.isAllve()){
client[oo].thread.dataout.writeBytes("$b\n");
client[oo].thread.dataout.flush();
}
table[client[myId].table_id]=null;
client[oo].mode=client_data.Idle;
client[myId].mode=client_data.Idle;}
catch(IOException e) {
System.out.println("Error occurred in do_exit()."+oo+" "+myId);
}
}
//$m
public void do_exit(){
int oo =-1;try{System.out.println("***" +
client[myId].name + " send state"+msg);
oo=table[client[myId].table_id].Player1_id;
if (oo==myId) oo=table[client[myId].table_id].Player2_id;if (!client[oo] instanceof client_data)){
client[myId].thread.dataout.writeBytes("$b\n");
client[myId].thread.dataout.flush();
client[myId].mode=client_data.Idle;
do_who();
return;
}
if (client[oo].thread.isAllve()){
System.out.println(
client[oo].name + " "+msg);
client[oo].thread.dataout.writeBytes("$m"+msg+"\n");
client[oo].thread.dataout.flush();
}
//$r
public void do_request(){ try{System.out.println("***" +
client[myId].name + "request the play whit "+name);
if (client[myId].name.equals(name)) return;
client[myId].mode=client_data.calling;
for(int i=0;i<MAX;i++){
if (client[i] instanceof client_data&&client[i].name.equals(name)
&&(client[i].mode==client_data.Idle)){
System.out.println("***client["+i+"] is " +client[i].name);
client[i].mode=client_data.called;
client[myId].thread.dataout.writeBytes("***"+client[myId].name+
"want to play with you.\n");
client[myId].thread.dataout.flush();
client[myId].thread.dataout.writeBytes("$r"+myId+"\n");
client[myId].thread.dataout.flush();
return;
}
}client[myId].thread.dataout.writeBytes("$s\n");
client[myId].thread.dataout.flush();
client[myId].mode=client_data.Idle;
return;
}catch(IOException e) {
System.out.println("Error occurred in do_requeat().");
}
}
//w
public void do_who(){
int i;
StringBuffer msg= new StringBuffer();
msg.append("$w");try{
for(int i=0;i<MAX;i++){
if (client[i] instanceof client_data&&client[i].thread.isAlive()){
if(msg.length()!=2)msg.append('+');
msg.append(client[i].name);
}
}
msg.append("\n");
for(int i=0;i<MAX;i++){
if (client[i] instanceof client_data&&client[i].thread.isAlive()){
client[i].thread.dataout.writeBytes("$r"+myId+"\n");
client[i].thread.dataout.flush();
}
}
System.out.println("***there are"+msg.substring(2));}catch(IOException e) {
System.out.println("Error occurred in do_who().");
}
}
//$y开始游戏
public void do_paly(int myId,int ooId){
int i;
try{ client[myId].mode=client[ooId].mode=client_data.palying;
for(int i=0;i<MAX;i++){
if (!(table[i] instanceof table_data)||(table[i]==null)
break;
}
if(g<MAX){
System.out.println("\n***Table"+i+"is open.");
table[g]=new table_data(myId,ooId);
client[myId].table_id=client[ooId].table_id=g;
client[ooId].thread.dataout.writeBytes("$y\n");
client[ooId].thread.dataout.flush();
}
else
System.out.println("*** Reject open table.");
}catch(IOException e) {
System.out.println("Error occurred in do_play().");
}
}
//在线对话
public void do_talk(int myId,String msg){
System.out.println("*** "+client[myId].name+"said"+msg); try{
if (client[myId].mode !=client_data.playing||clinet[myId].table_id=-1)
for(int i=0;i<MAX;i++)
if (client[i] instanceof client_data &&(client [i].mode==client_data.Idle)){
client[ooId].thread.dataout.writeBytes(client[myId].name+">"+"\n");
client[ooId].thread.dataout.flush();
}
}
}
else
{
int oo=table[client[myId].table_id].Player1_id;
if (oo==myId) oo=table[client[myId].table_id].Player2_id;
if (!client[oo] instanceof client_data)){
client[myId].thread.dataout.writeBytes("$b\n");
client[myId].thread.dataout.flush();
client[myId].mode=client_data.Idle;
do_who();
return;
}
client[ooId].thread.dataout.writeBytes(client[myId].name+">"+"\n");
client[ooId].thread.dataout.flush();
client[ooId].thread.dataout.writeBytes(client[myId].name+">"+"\n");
client[ooId].thread.dataout.flush();
}
}catch(IOException e) {
System.out.println("Error occurred in do_talk().");
}
}