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

📄 gamepool.java

📁 软件工程实践课程的答案哦
💻 JAVA
字号:
import java.io.*;
/**This class plays the role as
 * core controller in this software,
 * handling all the controling functions
 
 *
 */
class GamePool 
{   
    /**The flag indicating the fast network or the slow network*/
    static boolean Fast=false;
    
	static int HEIGHT = 18;
	static int  WIDTH = 11;
	static int[][] gamePool;
	static boolean isFull;
	static GameFace gameFace;
	static int score;
    /**The queue used for elimination algorithm*/
	static MyQueue elimination;
	private static MyQueue q1;
	private static MyQueue q2;
	private static MyQueue q3;
	private static MyQueue q4;
	
	private final static int l=3;
	
	public static boolean lock=false;
	
	/**The constructor*/
	GamePool(GameFace gameFace)
	{
		this.gameFace = gameFace;
		this.isFull = false;
		gamePool = new int[HEIGHT][WIDTH];
		for(int i=0;i<HEIGHT;i++)
		{
			for(int j = 0;j<WIDTH;j++)
			gamePool[i][j] = 0;
		}
		
		elimination=new MyQueue();
		q1=new MyQueue();
		q2=new MyQueue();
		q3=new MyQueue();
		q4=new MyQueue();
	}
	
	/**Switch between fast network version and slow network version
	 * @param flag
	 */
	public static void setFast(boolean flag)
	{
		Fast=flag;
	}

	/**Initialization
	 * 
	 */
	public static void init()
	{  int i,j=0;
		for( i=0;i<18;i++)
		for( j=0;j<11;j++)
		{
					gamePool[i][j]=0;
		}
		score = 0;
		isFull = false;
	}
	
	/**Judging whether the playing field is full
	 * @return whether the playing field is full
	 */
	public static boolean isFull()
	{    
	  	for(int i=0;i<WIDTH;i++)
		{
			if(gamePool[3][i]!=0)
			{
				isFull = true;
				break;
			}
			
		}
		return isFull;
	}
	
	
	/**Set the score of the local client and send the score to the server
	 * @param x the score
	 */
	private static void setScore(int x)
	{
		 gameFace.setScore(x);
		 Communication.setScore(x);
	}
	 
	     
	/**Clear the colors of the specified bar and send the information to the server
	 * @param bar the bar to be cleared
	 */
	public static void clear(Bar bar)
	{
		Location[] locations =  bar.getSquares();
		for(int i=0;i<3;i++)
		{
			int x = locations[i].x;
			int y = locations[i].y;
			gamePool[x][y] = 0;
			
			
		}
	
	        if(Fast)
			  Communication.clear(bar);
		
		    gameFace.clear(bar);
		
		
	}
	
	
	/**Clear the color of a specified location
	 * @param location the location to be cleared
	 */
	public static void clear(Location location)
	{  
		int x=location.x;
		int y=location.y;
		gamePool[x][y]=0;
		
		gameFace.clear(location);
		
		if(!lock)
		Communication.clear(location);
    }
	
	
	/**Set the colors of the specified bar and send the information to the server
	 * @param bar the bar to be colored
	 */
	public static void set(Bar bar)
	{
		Location[] locations =  bar.getSquares();
		for(int i=0;i<3;i++)
		{
			int x = locations[i].x;
			int y = locations[i].y;
			int color = locations[i].color;
			gamePool[x][y] = color;
			
			
		
		}
	
			if(Fast)
			Communication.set(bar);
		
		gameFace.set(bar);
	}
	
	
	/**Set the color of a specified location
	 * @param location the location to be colored
	 */
	public static void set(Location location) 
	{
		int x=location.x;
		int y=location.y;
		int color=location.color;
		gamePool[x][y]=color;
		
		if(!lock)
		Communication.set(location);
		
		gameFace.set(location);
			
    }
    
    public static int getRow()
    {
    	return HEIGHT;
    }
    
    public static int getColumn()
    {
    	return WIDTH;
    }
    
    
    /**Eliminate the series
     * @return whether there are still some series to be eliminated
     */
    public synchronized static boolean eliminate()
	{   
	    boolean flag=true;
	
	    Location location=new Location();
	    Location location1=new Location();
	    Location location2=new Location();
	    int x1,x2,y1,y2;
	    
		while(!elimination.isEmpty())
		{
			location=elimination.deQueue();
			
			if(findSeries(location))
			        flag=false;
			
			
		}
		
		/*eliminate the horizontal series*/
		while(!q1.isEmpty())
		{
			location1=q1.deQueue();
			location2=q1.deQueue();
			
			x1=location1.x;
			y1=location1.y;
			y2=location2.y;
			
			
		  if(y2-y1>=l-1)
		   {
		   
			for(int y=y1;y<=y2;y++)
			   clear(new Location(x1,y,gamePool[x1][y]));
			   
			 	score+=y2-y1-l+2;
			 	
			    FadeLayer fade=new FadeLayer();
			   fade.init();
			   fade.start();
			   fade=null;
			   gameFace.selfPanel.canvas.requestFocus();
		   }
			   
	    }
	    
	    Location location3=new Location();
	    Location location4=new Location();
	    /*eliminate the vertical series*/
	    while(!q2.isEmpty())
	    {
	    	location1=q2.deQueue();
	    	location2=q2.deQueue();
	    	
	    	y1=location1.y;
	    	x1=location1.x;
	    	x2=location2.x;
	     
	     if(x2-x1>=l-1)
	      {
	      	
	    	for(int x=x1;x<=x2;x++)
	    	    clear(new Location(x,y1,gamePool[x][y1]));
	    	    
	    	    if(location1.x==location3.x && location1.y==location3.y
	    			&&location2.x==location4.x&&location2.y==location4.y)
	    			break;
	    			
	    	    location3=location1;
	    	    location4=location2;
	    	   
	    	    score+=x2-x1-l+2;
	    	    
	    	     FadeLayer fade=new FadeLayer();
			   fade.init();
			   fade.start();
			   fade=null;
			   gameFace.selfPanel.canvas.requestFocus();
	      }
	    }
	    
	    /*eliminate the major diagonal series*/
	    while(!q3.isEmpty())
	    {
	    	location1=q3.deQueue();
	    	location2=q3.deQueue();
	    	
	    	x1=location1.x;
	    	y1=location1.y;
	    	
	    	x2=location2.x;
	    	y2=location2.y;
	    	
	      if(y2-y1>=l-1)
	      {
	        int x=0; int y=0;
	    	for(x=x1,y=y1;x<=x2&&y<=y2;x++,y++)
	    	   clear(new Location(x,y,gamePool[x][y]));
	    	   
	    	   score+=y2-y1-l+2;
	    	   
	    	    FadeLayer fade=new FadeLayer();
			   fade.init();
			   fade.start();
			   fade=null;
			   gameFace.selfPanel.canvas.requestFocus();
	      }
	    	   
	    }
	    
	    /*eliminate the subsidy diagonal series*/
	    while(!q4.isEmpty())
	    {
	    	location1=q4.deQueue();
	    	location2=q4.deQueue();
	    	
	    	x1=location1.x;
	    	y1=location1.y;
	    	
	    	x2=location2.x;
	    	y2=location2.y;
	    
	     if(y2-y1>=l-1)	
	     {
	        int x=0; int y=0;
	    	for(x=x1,y=y1;x>=x2&&y<=y2;x--,y++)
	    	   clear(new Location(x,y,gamePool[x][y]));
	    	   
	    	score+=y2-y1-l+2;
	    	
	    	 FadeLayer fade=new FadeLayer();
			   fade.init();
			   fade.start();
			   fade=null;
			   gameFace.selfPanel.canvas.requestFocus();
	     }
	    }
		
		
		setScore(score);//assign the score
		
		
		/*drop the rest of the squares hung in the air*/
		int i,j,k;
		Location locationX=new Location();
		Location locationY=new Location();
		
		for(i=HEIGHT-2;i>=0;i--)
		   for(j=0;j<=WIDTH-1;j++)
		   {
		   	 if(gamePool[i][j]!=0)
		   	 {  k=i;
		   	 	i++;
		   	 	while(i<=HEIGHT-1&&gamePool[i][j]==0)
		   	 	     i++;
		   	 	     
		   	 	     i--;
		   	 	     
		   	 	     locationX.setX(k);
		   	 	     locationX.setY(j);
		   	 	     locationX.color=gamePool[k][j];
		   	 	     
		   	 	     locationY.setX(i);
		   	 	     locationY.setY(j);
		   	 	     locationY.color=gamePool[k][j];
		   	 	     
		   	 	  if(k!=i)
		   	 	     {  GamePool.clear(locationX);
		   	 	     
		   	 	       GamePool.set(locationY);
		   	 	     
		   	 	       elimination.enQueue(locationY);
		   	 	     }
		   	 }
		   }
		   
		   return flag;	
	 }
	
	
	
	/**Find and record the starting and end locations of the series which have the same color
	 * @param location The location whose four directions are to be checked to find series having the same color
	 * @return whether a series having the same color is found
	 */
	private static boolean findSeries(Location location)
	{    
	     boolean flag=false;/*indicating whether there exists series which 
	                         have the same color*/
	
	     int x=location.getX();
	     int y=location.getY();
	     int color=location.color;
	     
	     Location location1=new Location();
	     Location location2=new Location();
	     
	     int x1,x2,y1,y2;
		
		/*search horizontal direction to find series*/
         y1=y;
         while(  y1>=0 && gamePool[x][y1]==color)
               y1--;
               
               y1++;
               
         
         y2=y;
         while(  y2<WIDTH&&gamePool[x][y2]==color)
               y2++;
               
               y2--;
          
         if(y1!=y2)
            flag=true;
                 
         location1.setX(x);
         location1.setY(y1);
         
         location2.setX(x);
         location2.setY(y2);
         
         q1.enQueue(location1); System.out.println(location1.x);System.out.println(location1.y);
         q1.enQueue(location2); System.out.println(location2.x);System.out.println(location2.y);
         
         /*search vertical direction to find series*/
         x1=x;
         while(  x1>=0&&gamePool[x1][y]==color)
               x1--;
               
               x1++;
               
         x2=x;
         while(  x2<HEIGHT&&gamePool[x2][y]==color)
               x2++;
               
               x2--;
         
         if(x1!=x2)
           flag=true;
              
         location1.setX(x1);
         location1.setY(y);
         
         location2.setX(x2);
         location2.setY(y);
         
         q2.enQueue(location1);
         q2.enQueue(location2);
         
         /*search major diagonal direction*/
         x1=x;
         y1=y;
         
         while(  x1>=0 && y1>=0 && gamePool[x1][y1]==color)
              {
              	x1--;
              	y1--;
              }
              x1++;
              y1++;
              
         x2=x;
         y2=y;
         
         while(  x2<HEIGHT && y2<WIDTH && gamePool[x2][y2]==color)
              {
              	x2++;
              	y2++;
              }
              x2--;
              y2--;
          
         if(x1!=x2)
           flag=true;
               
         location1.setX(x1);
         location1.setY(y1);
         
         location2.setX(x2);
         location2.setY(y2);
         
         q3.enQueue(location1);
         q3.enQueue(location2);
         
         /*search subsidy diagonal direction*/
         x1=x;
         y1=y;
         
         while(  x1<HEIGHT && y1>=0&&gamePool[x1][y1]==color)
              {
              	x1++;
              	y1--;
              }
              x1--;
              y1++;
              
         x2=x;
         y2=y;
         
         while(  x2>=0 && y2<WIDTH&&gamePool[x2][y2]==color)
              {
              	x2--;
              	y2++;
              }
              x2++;
              y2--;
         
         if(x1!=x2)
           flag=true;    
         
         location1.setX(x1);
         location1.setY(y1);
         
         location2.setX(x2);
         location2.setY(y2);
         
         q4.enQueue(location1);
         q4.enQueue(location2);
         
         return flag;
		
	}
    
    public static int get(int pX,int pY)
    {
    	return gamePool[pX][pY];
    }
    
}

⌨️ 快捷键说明

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