server.java
来自「软件工程实践课程的答案哦」· Java 代码 · 共 894 行 · 第 1/2 页
JAVA
894 行
}
/**send the chatting information of a client to other clients
* @param No the id of the client
* @param in the input stream
*/
public static synchronized void chat(int No, BufferedReader in)
{
OutputStream output=null;
PrintWriter printWriter=null;
String chatInformation=new String();
try
{
chatInformation=in.readLine();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
int i=0;
for(i=1;i<=amount;i++)
{
if(i!=No)
{
try
{
output=(clientSockets[i]).getOutputStream();
printWriter=new PrintWriter(output);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
printWriter.println("chat");
printWriter.flush();
printWriter.println(chatInformation);
printWriter.flush();
printWriter.println(names[No]);
printWriter.flush();
}
}
}
/**A client has clicked the "start" button*/
public static synchronized void clientStartGame()
{
System.out.println("clientStartGame");
startNumber++;
/*If the number of the clients who have clicked the "start" button
*equals to the number of clients connected, call GameStart() to
*start the game*/
if(amount==startNumber)
{
gameBegin=true;
GameStart();
currentTime=System.currentTimeMillis();
}
}
/**Send random numbers to a client for them to create bars
* @param No the id of the client
* @param printWriter the output stream
*/
public static synchronized void sendNumber(int No,PrintWriter printWriter)
{
ColorSet colorSet=new ColorSet();
int color0;
int color1;
int color2;
String s="";
/*send 50 bars to the client at a time*/
for(int i=1;i<=50;i++)
{
colorSet=ServerRandomQueue.deQueue(No);
color0=colorSet.getColor0();
color1=colorSet.getColor1();
color2=colorSet.getColor2();
s+=color0;
s+=color1;
s+=color2;
}
printWriter.println("Bar");
printWriter.flush();
printWriter.println(s);
printWriter.flush();
}
/**send the playing field of a client to other clients
* @param No the ID of the client
* @param in the input stream
*/
public static synchronized void sendPlayingField(int No,BufferedReader in)
{
OutputStream output=null;
PrintWriter printWriter=null;
String s=new String();
try
{
s=in.readLine();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
int i=0;
for(i=1;i<=amount;i++)
{
if(i!=No)
{
try
{
output=(clientSockets[i]).getOutputStream();
printWriter=new PrintWriter(output);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
printWriter.println("playingField");
printWriter.flush();
printWriter.println(s);
printWriter.flush();
printWriter.println(new Integer(No).toString());
printWriter.flush();
}
}
}
/**send the clearing information of a client to other clients
* @param No the ID of the client
* @param in the input stream
*/
public static synchronized void clear(int No,BufferedReader in)
{
OutputStream output=null;
PrintWriter printWriter=null;
String s=new String();
try
{
s=in.readLine();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
int i=0;
for(i=1;i<=amount;i++)
{
if(i!=No)
{
try
{
output=(clientSockets[i]).getOutputStream();
printWriter=new PrintWriter(output);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
printWriter.println("clear");
printWriter.flush();
printWriter.println(s);
printWriter.flush();
printWriter.println(new Integer(No).toString());
printWriter.flush();
}
}
}
/**send the coloring information of a client to other clients
* @param No the id of the client
* @param in the input stream
*/
public static synchronized void set(int No,BufferedReader in)
{
OutputStream output=null;
BufferedWriter writer=null;
PrintWriter printWriter=null;
String s=new String();
try
{
s=in.readLine();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
int i=0;
for(i=1;i<=amount;i++)
{
if(i!=No)
{
try
{
output=(clientSockets[i]).getOutputStream();
printWriter=new PrintWriter(output);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
printWriter.println("set");
printWriter.flush();
printWriter.println(s);
printWriter.flush();
printWriter.println(new Integer(No).toString());
printWriter.flush();
}
}
}
/**send the score of a client to other clients
* @param No the ID of the client
* @param in the input stream
*/
public static synchronized void setScore(int No,BufferedReader in)
{
OutputStream output=null;
PrintWriter printWriter=null;
String s=new String();
int score=0;
try
{
s=in.readLine();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
score=Integer.valueOf(s).intValue();
scores[No]=score;
int i=0;
for(i=1;i<=amount;i++)
{
if(i!=No)
{
try
{
output=clientSockets[i].getOutputStream();
printWriter=new PrintWriter(output);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
printWriter.println("score");
printWriter.flush();
printWriter.println(s);
printWriter.flush();
printWriter.println(new Integer(No).toString());
printWriter.flush();
}
}
}
public static void main(String[] args)throws IOException
{
Server server=new Server();
server.startUp();
}
}
/**The thread used for timing
* @author zheng
*
*/
class GameTimer extends Thread
{
int timer;
public GameTimer()
{
timer=10;
}
public void run()
{
while(timer>0)
{ try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
}
timer--;
System.out.println("TIMER:"+timer);
}
Server.AllAreOver=true;
Server.GameOver();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?