history
rules
expert advice
online challenge
game source
links
test yourself
board room
the last minute
about us




Weiqi.Java source file


import java.awt.*;
import java.applet.*;
import WelcomeDialog;
import Client;
import java.util.StringTokenizer;
import Board;
import Chess;

public class Weiqi extends Applet implements Runnable
{

String Username="";

Client cl;

Thread controller;

int Id;

String message="";

String[] nameList=new String[100];
int[] ptn=new int[100];

int nu=0;

Label msg=new Label("");

Button bt_play=new Button("Play");

TextArea chat=new TextArea("Hello ...",40,40);

TextField tf=new TextField(60);

List names=new List();
Chess ch;

public void showNames()
{
names.removeAll();
names.add("Player List :");
names.add("");
for (int i=0;i<=nu;i++)
if (!nameList[i].equals("None"))
{
if (ptn[i]==-1)
names.add("WQ_"+i+": "+nameList[i]+" Waiting...");
else
names.add("WQ_"+i+": "+nameList[i]+" Playing with
"+nameList[ptn[i]]);
};
repaint();
};

public void start()
{
if (controller==null)
{
controller=new Thread(this);
controller.start();
};
};

public void init()
{
setBackground(Color.white);
setLayout(new BorderLayout());
add("North",msg);
add("East",bt_play);

chat.setEditable(false);
chat.append("\n\n");
add("West",chat);
add("Center",names);

Panel sth=new Panel();
sth.add(new Label("Send : "));
sth.add(tf);
add("South",sth);

};

public void process(String str)
{
StringTokenizer t=new StringTokenizer(str);
String command=t.nextToken();
if (command.equals("ID"))
Id=Integer.parseInt(t.nextToken());
else
if (command.equals("Noofusers"))
nu=Integer.parseInt(t.nextToken());
else
if (command.equals("Playername"))
nameList[Integer.parseInt(t.nextToken())]=t.nextToken();
else
if (command.equals("Playercomb"))

ptn[Integer.parseInt(t.nextToken())]=Integer.parseInt(t.nextToken());
else
if (command.equals("PlayerUpdateOver"))
{
showNames();
}
else
if (command.equals("Tell"))
{
Board b=new Board(str.substring(5,str.length()));
}
else
if (command.equals("Chat"))
chat.append(str.substring(5,str.length())+"\n");
else
if (command.equals("Opengame"))
{
if (Id ch=new Chess(nameList[Id],nameList[ptn[Id]],1,cl);
else
ch=new Chess(nameList[ptn[Id]],nameList[Id],-1,cl);
//appletFrame=new Frame(nameList[Id]+" V.S. "+nameList[ptn[Id]]);
//appletFrame.add(ch);
//ch.init();
//ch.start();
//appletFrame.resize(440,600);
//appletFrame.show();
ch.show();

}
else
if (command.equals("Move"))
{
int k=Integer.parseInt(t.nextToken());
int y=(int) k/100;
int x=k%100;
ch.rstct=new int[20][20];
ch.oppmove(y,x);
ch.checkSurround(ch.qizi);
ch.checkSurround(-ch.qizi);
}
else
if (command.equals("Closegame"))
{
Board b=new Board("Your opponent quitted the game !");
//ch.destroy();
//appletFrame.dispose();
ch.dispose();
}
else
if (command.equals("Youquitgame"))
{
Board b=new Board("You quitted the game !");
//ch.destroy();
//appletFrame.dispose();
ch.dispose();
};
};

public void run()
{
cl=new Client();
display(cl.net_Status);
while (!cl.net_Status.equals("Connected"))
{
cl=new Client();
};

display("Welcome ! Please enter your nick name ...");

WelcomeDialog wd = new WelcomeDialog();
wd.show();
while (Username.equals(""))
Username=wd.getUserName;
wd.dispose();

Username=new StringTokenizer(Username).nextToken();
display("Welcome "+Username+" ! Retrieving info from server ...");

cl.sendRequest("NEW "+Username);

String rep=cl.getResponse();
while (!rep.equals("Over"))
{
process(rep);
rep=cl.getResponse();
};

display("Hello "+Username+" ! Your Id is WQ_"+Id);

showNames();

rep="None";
while (true)
{
rep=cl.getResponse();
//display(rep);
if (rep!="None")
{
process(rep);
};
};
}

public void stop()
{
if (controller!=null)
{
cl.shutDown();
controller.stop();
};
};

public void destroy()
{
cl.shutDown();
};

void display(String str)
{
msg.setText(str);
};

public void paint(Graphics g)
{
};

public int findUser(String name)
{
for (int i=0;i<=nu;i++)
if (name.equals(nameList[i]))
return i;
return -1;
};

public boolean action(Event e,Object arg)
{
if ((e.target==tf) && (!tf.getText().equals("")))
{
cl.sendRequest("Chat "+tf.getText());
tf.setText("");
}
else
if ((e.target==bt_play) && (ptn[Id]==-1))
{
String nn=names.getSelectedItem();
if (nn!=null)
if (nn!="")
{
StringTokenizer temp=new StringTokenizer(nn,"_");
String index=temp.nextToken();
if (index.equals("WQ"))
{
index=temp.nextToken().substring(0,1);
if ((!index.equals(Integer.toString(Id))) && (ptn[Integer.parseInt(index)]==-1))
cl.sendRequest("Join "+index);
};
};
};
return super.action(e,arg);
};

};

view the source file

By visiting the below links to the source code, you have agreed to our license agreement. The source codes are copyright and if you wish to use the source code or part of the engine of the game, you must obtain weiqi.com permission first. The final decision lies with weiqi.com .

Source for Board.Java

Source for Chess.Java

Source for Client.Java

Source for Server.Java

Source for ServerThread.Java

Source for User.Java

Source for Weiqi.Java

Source for WelcomeDialog.Java

top

©1999. weiqi.com Last updated on 14/08/99