singlegamepool.java

来自「软件工程实践课程的答案哦」· Java 代码 · 共 470 行

JAVA
470
字号
/**This class plays the role as
 * core controller in this software,
 * handling all the controling functions
 *
 */
class SingleGamePool 
{
	static int HEIGHT = 18;
	static int  WIDTH = 11;
	static int[][] gamePool;
	static boolean isFull;
	static SingleGameFace gameFace;
	static int score;
    static SingleMyQueue elimination;
	private static SingleMyQueue q1;
	private static SingleMyQueue q2;
	private static SingleMyQueue q3;
	private static SingleMyQueue q4;
	private final static int l=3;
	SingleGamePool(SingleGameFace 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 SingleMyQueue();
		q1=new SingleMyQueue();
		q2=new SingleMyQueue();
		q3=new SingleMyQueue();
		q4=new SingleMyQueue();
	}
	
	
	public static void init()
	{
		for(int i=0;i<18;i++)
		for(int 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);
	}
	
	/**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(SingleBar bar)
	{
		SingleLocation[] locations =  bar.getSquares();
		for(int i=0;i<3;i++)
		{
			int x = locations[i].x;
			int y = locations[i].y;
			gamePool[x][y] = 0;
		}
		gameFace.clear(bar);
	}
	
	/**Clear the color of a specified location
	 * @param location the location to be cleared
	 */
	public static void clear(SingleLocation location)
	{  
		int x=location.x;
		int y=location.y;
		gamePool[x][y]=0;
		
		gameFace.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(SingleBar bar)
	{
		SingleLocation[] 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;
		}
		/*for(int i=0;i<18;i++)
		{
			for(int j=0;j<11;j++)
				System.out.print(gamePool[i][j]);
			System.out.println();
		}*/
		gameFace.set(bar);
	}
	
	/**Set the color of a specified location
	 * @param location the location to be colored
	 */
	public static void set(SingleLocation location)
	{
		int x=location.x;
		int y=location.y;
		int color=location.color;
		gamePool[x][y]=color;
		
		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;
	
	    SingleLocation location=new SingleLocation();
	    SingleLocation location1=new SingleLocation();
	    SingleLocation location2=new SingleLocation();
	    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 SingleLocation(x1,y,gamePool[x1][y]));
			   
			 	score+=y2-y1-l+2;
			 	gameFace.setMessage("     COOL!");
			    FadeLayer fade=new FadeLayer();
			   fade.init();
			   fade.start();
			   fade=null;
			   gameFace.canvas.requestFocus();
		   }
			   
	    }
	    
	    /*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 SingleLocation(x,y1,gamePool[x][y1]));
	    	    
	    	    score+=x2-x1-l+2;
	    	    gameFace.setMessage("     COOL!");
	    	     FadeLayer fade=new FadeLayer();
			   fade.init();
			   fade.start();
			   fade=null;
			   gameFace.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 SingleLocation(x,y,gamePool[x][y]));
	    	   
	    	   score+=y2-y1-l+2;
	    	   gameFace.setMessage("     COOL!");
	    	    FadeLayer fade=new FadeLayer();
			   fade.init();
			   fade.start();
			   fade=null;
			   gameFace.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 SingleLocation(x,y,gamePool[x][y]));
	    	   
	    	score+=y2-y1-l+2;
	    	gameFace.setMessage("     COOL!");
	    	 FadeLayer fade=new FadeLayer();
			   fade.init();
			   fade.start();
			   fade=null;
			   gameFace.canvas.requestFocus();
	     }
	    }
		
		
		setScore(score);//assign the score
		
		
		/*drop the rest of the squares hung in the air*/
		int i,j,k;
		SingleLocation locationX=new SingleLocation();
		SingleLocation locationY=new SingleLocation();
		
		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)
		   	 	     {  SingleGamePool.clear(locationX);
		   	 	     
		   	 	       SingleGamePool.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(SingleLocation location)
	{    //System.out.println("findSeries");
	     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;
	     
	     SingleLocation location1=new SingleLocation();
	     SingleLocation location2=new SingleLocation();
	     
	     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 + =
减小字号Ctrl + -
显示快捷键?