电脑随机产生答案,并给出答案所在范围。多位玩家依次根据电脑所给范围给出一个符合的数,该数如果不是答案,电脑则会改变范围,并继续游戏;如果该数是答案,则该玩家失败,游戏结束。
Client.java
Command.java
DealReceiveMessage.java
GameServer.java
Playing.java
SendMessage.java
public class Client {
//connected clients' information
int id;
String Name/*,IP*/;
boolean ready;
Socket s;
public Client(int id,String Name/*,String IP*/,Socket s,boolean ready) {//IP后来想了想并没有任何用处,所以注释掉了
this.id=id;
this.Name=Name;
//this.IP=IP;
this.s=s;
this.ready=ready;
}
}
public void run() {
message.getChars(0, message.length(), chmsg, 0);
int i=0;
while(i<message.length()) {
if(chmsg[i]=='/'&&chmsg[i+1]=='/')
{
i+=2;
while(chmsg[i]!=' ') {
msgtype+=chmsg[i];
i++;
}
System.out.println(msgtype+msgtype.equals("msg"));
if(msgtype.equals("msg"))msg(i);
else if(msgtype.equals("ans"))ans(i);
else if(msgtype.equals("ready"))ready(i);
}
i++;
}
}
public GameServer() {
new Command().title("猜数游戏服务器 - Alpha-1");
Init();
try {
server=new ServerSocket(25321);
System.out.println("服务器已开启,端口:25321");
while(true) {
Socket socket=server.accept();
System.out.println("有客户端连接");
BufferedReader bfr=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
if(nowplayer<maxplayer&&status==0) {
pw.println("//AGREE");
String name=bfr.readLine();
System.out.println("已收到连接用户的用户名:"+name);
pw.println(++nowplayer);
System.out.println("已发送id");
Client client=new Client(nowplayer,name,socket,false);
clients.add(client);
System.out.println(name+" 加入了房间 "+nowplayer+"/"+maxplayer);
for(Client c:clients) {
new SendMessage(("//msg "+name+" 加入了房间 "+nowplayer+"/"+maxplayer),c.s);
}
new Thread(new ReceiveMesasge(client)).start();
if(nowplayer==maxplayer)
{
for(Client c:clients) {
new SendMessage(("//msg 房间已满,在聊天框内输入/READY进行准备"),c.s);
new SendMessage(("//msg 游戏将在所有人准备后开始"),c.s);
}
new Thread(new BeforePlay(clients,maxplayer)).start();
}
}
else if(nowplayer>=maxplayer) {
pw.println("//REFUSE\n服务器已满");
}
else if(status==1) {
pw.println("//REFUSE\n游戏已开始");
}
}
}catch(IOException e) {e.printStackTrace();}
}
class BeforePlay implements Runnable{
boolean begin=false;
List<Client> clients;
int maxplayer;
public BeforePlay(List<Client> clients,int maxplayer) {
this.clients=clients;
this.maxplayer=maxplayer;
}
public void run() {
while(true) {
//System.out.println("检测中....");
begin=true;
for(Client c:clients)
if(c.ready==false) {
begin=false;
break;
}
if(begin)break;
}
System.out.println("已开始游戏");
playing =new Playing();
playing.init(maxplayer, clients);
}
}
/*
* 发送消息专用类
*
* 构造函数:message:已打包成string类型的消息 s:要送达的客户端
*
* 格式: //[消息类型] /[数据,其中包括整型,字符串等其他数据类型]\ //[消息类型] /.....
*
* 每个消息以两个左斜杠开始,以一个右斜杠结束,有斜杠后可以再加做斜杠做下一消息
*
* 消息类型及数据有:
* 1:msg 聊天消息 数据数量:1 数据:String msg
* 2:ques [仅服务器发送]问题数据 数据数量:4 数据1:int id 数据2:String Name 数据3:int big 数据4:int small
* 3:ans [仅客户端发送]回答数据 数据数量:1 数据:int ans
* 4:gameover [仅服务器发送]指令 数据数量:1 数据:String loser
*
*/
Command.java
DealReceiveMessage.java
GameClient.java
Playing.java
SendMessage.java
TalkingWindow.java
public class TalkingWindow extends JFrame{
JTextArea ta=new JTextArea("在该窗口进行聊天!\n");
JPanel show=new JPanel();
JPanel input=new JPanel();
JButton jb=new JButton();
JScrollPane sp=new JScrollPane(ta);
JTextField tf=new JTextField(20);
Container c=this.getContentPane();
Socket server;
public TalkingWindow(String name,Socket server)
{
ta.setEditable(false);
ta.setLineWrap(true);
jb.setText("发送");
this.setSize(275,300);
c.setSize(275,300);//设置JFrame容器的大小
this.setVisible(true);
input.add(tf);
input.add(jb);
this.add(sp);
this.add(input,BorderLayout.SOUTH);
this.setTitle("聊天");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message=tf.getText().toString();
if(!message.equals("")) {
if(message.equals("/READY")||message.equals("/ready"))
new SendMessage(("//ready "),server); //这里就是当你输入的时“/READY”时的一个处理,让服务器知道你准备了
else new SendMessage(("//msg <"+name+">"+message),server);
tf.setText("");
}
}
});
}
public void Put(String message)
{
ta.append(message+'\n');
}
}
else if(msgtype.equals("ready"))ready(i);