⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 otherclientsthread.java

📁 软件工程实践课程的答案哦
💻 JAVA
字号:
/**This file describes the thread working
 *on client computer which handles the 
 *server information
 */
 import java.io.*;
 import java.net.*;
 import java.util.*;
 public class OtherClientsThread extends Thread
 {  
    /**The information from the server*/
 	private String serverInformation;
 	
 	/**The reference to the game interface*/
 	private GameFace gameFace;
 	

 	
 	private int maxSize=4;
 	
 	private ColorSet randomColorSet;
 	
 	private ColorSet nextColorSet;
 	
 	public OtherClientsThread(GameFace gameFace)
 	{
 		super();
 		serverInformation=new String();
 		
 		this.gameFace=gameFace;
 		
 		randomColorSet=new ColorSet();
 		 
 		nextColorSet=new ColorSet();
 		 
 	}
 	
 	public OtherClientsThread()
 	{
 		serverInformation=new String();
 		
 		randomColorSet=new ColorSet();
 	}
 	
 	public void run()
 	{  
 		while(true)
 		{
             try
             {
               
                while(Communication.in==null)
                {
                	try
                	{
                		this.sleep(1000);
                		
                	}catch(Exception e){}
                }
 			 
 			  	serverInformation  = Communication.in.readLine();
 			  
 			  }catch(IOException e)
 			  {
 			  }
 			 
 			 
 	       /*Call specific function to handle the information from the server*/
 			  
 			  if(serverInformation.equals("Bar"))
 			     receiveBar();
 			     
 			  if(serverInformation.equals("name"))
 			     setName();
 			  
 			  if(serverInformation.equals("chat"))
 			     chat();
 			  
 			  if(serverInformation.equals("Start"))
 			     start2();      
 			  
 			  if(serverInformation.equals("The game begins!"))
 			     gameStart();
 			  
 			  if(serverInformation.equals("The game over"))
 			     gameOver();   
 			  
 			  if(serverInformation.equals("playingField"))
 			     playingField();  
 			  
 			  if(serverInformation.equals("clear"))
 			     clear();
 			  
 			  if(serverInformation.equals("set"))
 			     set();
 			     
 			     
 			  if(serverInformation.equals("score"))
 			     setScore();   
 			  
 			  if(serverInformation.equals("gameOver"))
 			     gameOver();
 			  
 			  if(serverInformation.equals("rank"))
 			     rank(); 
 			     
 			     serverInformation=null;
 		}
 	}
 	
 	/**Receive the bars sent from the server*/
 	public void receiveBar()
 	{  String s=new String();
 	   char color0;
 	   char color1;
 	   char color2;
 	   
 	   
 	   int c0;
 	   int c1;
 	   int c2;
 	   
 	   
 		try
	     {  
	      
	     	s=Communication.in.readLine();
	     
	     	
	     }
		 catch(IOException e)
		 {
		 	System.out.println(e);
		 } 
	     
	     /*The server sends 50 ColorSet objects at a time, use the first ColorSet
	      *to create a bar, the second to create the next bar, and put the rest of
	      *the bars to a buffering queue on the client*/
		 for(int i=0;i<150;i+=3)
		 {
		 	color0=s.charAt(i);
		    color1=s.charAt(i+1);
		    color2=s.charAt(i+2);
		    
		    c0=(Integer.valueOf(new Character(color0).toString())).intValue();
		    c1=(Integer.valueOf(new Character(color1).toString())).intValue();
		    c2=(Integer.valueOf(new Character(color2).toString())).intValue();
		    
		    randomColorSet=new ColorSet(c0,c1,c2);
		    
		    Queue.enQueue(randomColorSet);
		 }
		 
		 
		
 	}
 	
 	/**Call the gameFace to start the game*/
 	public void gameStart()
 	{
 		
         gameFace.startGame();
 	    
 	   
 	}
 	
 	
 
     /**Receive the names sent from the server and transmit them to
      *the game interface to display on certain panels*/
     public void setName()
     {  System.out.println("setName");
     	String name=new String();
     	String no=new String();
     	
     	int No=0;
     	
     	try
     	   {
     	   	 name=Communication.in.readLine();
     	   	 no=Communication.in.readLine();
     	   }
     	   catch(IOException e)
     	   {
     	   }
     	   
     	   No=Integer.valueOf(no).intValue();
     	   
     	  
     	   
     	   if(No>=0)
     	      gameFace.setName(name,No);
     	        else gameFace.setName(name);
     	   
     }
     
     /**Receive the chatting information from other clients*/
     public void chat()
     {
     	String chatInformation=new String();
     	String name=new String();
     	
     	
   
     	try
     	   {
     	   	 chatInformation=Communication.in.readLine();
     	   	 name=Communication.in.readLine();
     	   }
     	   catch(IOException e)
     	   {
     	   }
     	   
     	  
     	   
     	   gameFace.chat.receiveChatContent(name,chatInformation);
     }
     
     public void start2()
     {
     	gameFace.chat.receiveChatContent("System message: ", "Connect successfully. You can start the game! ");
     }
     
     /**Receive the playing field sent from the server which corresponds to another client*/
     public void playingField()
     {
     	int No=0;
 	  String s=new String();
 	  String no=new String();
 	  
 	   
 	   int x;
 	   int y;
 	   int color;
 	   
 	   try
 	    {
 	    	s=Communication.in.readLine();
 	        no=Communication.in.readLine();
 	    }	
 	    catch(IOException e)
 	    {
 	    	System.out.println(e.getMessage());
 	    }
 	    catch(Exception e )
	    {}
	    
	    
	    No=Integer.valueOf(no).intValue();
	    
	    gameFace.displayPlayingField(s,No);
     }
     
     /**Receive and handle the clear information from the server*/
     public void clear()
 	{ int No=0;
 	  String s=new String();
 	  String no=new String();
 	  String X=new String();
 	  String Y=new String();
 	  String COLOR=new String();
 	   
 	   int x;
 	   int y;
 	   int color;
 	  
 	  try
 	    {
 	    	s=Communication.in.readLine();
 	   
 	  
 	    	no=Communication.in.readLine();
 	    }	
 	    catch(IOException e)
 	    {
 	    	System.out.println(e.getMessage());
 	    }
 	    catch(Exception e )
	    {}
	    
	    No=Integer.valueOf(no).intValue();
	    StringTokenizer tokens=new StringTokenizer(s);
	    
	    while(tokens.hasMoreTokens())
	     {
	     
	     X=tokens.nextToken();
		 Y=tokens.nextToken();
		 COLOR=tokens.nextToken();
		 
		 x=(Integer.valueOf(X.toString())).intValue();
		 y=(Integer.valueOf(Y.toString())).intValue();
		 color=(Integer.valueOf(COLOR.toString())).intValue();
		 
 	   
 	    
 	     gameFace.clear(new Location(x,y,color),No);
 	    }
 	    
     }
     
     
     /**Receive and handle the coloring information from the server*/
     public void set()
 	{ int No=0;
 	  String s=new String();
 	  String no=new String();
 	  String X=new String();
 	  String Y=new String();
 	  String COLOR=new String();
 	   
 	   int x;
 	   int y;
 	   int color;
 	  try
 	    {
 	    	s=Communication.in.readLine();
 	 
 	    	no=Communication.in.readLine();
 	    
 	    }	
 	    catch(IOException e)
 	    {
 	    	System.out.println(e.getMessage());
 	    }
 	    catch(Exception e )
	    {}
	     No=Integer.valueOf(no).intValue();
	     
	     StringTokenizer tokens=new StringTokenizer(s);
	    
	    while(tokens.hasMoreTokens())
	     {
	     
	     X=tokens.nextToken();
		 Y=tokens.nextToken();
		 COLOR=tokens.nextToken();
		 
		 x=(Integer.valueOf(X.toString())).intValue();
		 y=(Integer.valueOf(Y.toString())).intValue();
		 color=(Integer.valueOf(COLOR.toString())).intValue();
		 
 	   
 	 
 	    gameFace.set(new Location(x,y,color),No);
 	    }
 	    
     }
 	
 	/**Receive the score corresponding to another client and notifies the game interface
 	 *to display it*/
 	 public void setScore()
 	{ String SCORE=new String();
 	  String NO=new String();
 	  
 	  int No=0;
 	  int score=0;
 	  try
 	    {
 	    	SCORE=Communication.in.readLine();
 	    	
 	    	NO=Communication.in.readLine();
 	    }	
 	    catch(IOException e)
 	    {
 	    	System.out.println(e.getMessage());
 	    }
 	    
 	    score=Integer.valueOf(SCORE).intValue();
 	    No=Integer.valueOf(NO).intValue();
 	    
 	    gameFace.setScore(score,No);
 	    
 	    
 	}
 	
 	
 	/**The game over*/
 	public void gameOver()
 	{ 
 	    
 	    gameFace.endGame();
 	    
 	    
 	}
 	
 	/**Receive the sorting information sent from the server and display the places of each player*/
 	public void rank()
 	{
 		int order=0;
 		int no=0;
 		char ch;
 		String ORDER=new String();
 		String No=new String();
 		
 		
 		try
 		   {
 		   	 No=Communication.in.readLine();
 		   	 
 		   	 ORDER=Communication.in.readLine();
 		   }
 		   catch(IOException e)
 		   {
 		   }
 		   
 		
 		   no=Integer.valueOf(No).intValue();
 		   
 		   for(int i=0;i<ORDER.length();i++)
 		   {
 		   	 ch=ORDER.charAt(i);
 		   	 
 		   	 order=Integer.parseInt(new Character(ch).toString());
 		   	 
 		   	 if(i+1==no)
 		   	   gameFace.displayOrder(order);
 		   	     else
 		   	         gameFace.setOrder(order,i+1);
 		   }
 		   
 		   
 		}
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -