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

📄 bar.java

📁 软件工程实践课程的答案哦
💻 JAVA
字号:
import java.io.*;
/*
 * The class encapsulating the data needed to describe a bar,
 * also containing the needed operations to control the movement
 * of the bar.
 */

public class Bar {
	public Location[] squares;//each bar has three squares
	//private boolean isBottom;
	public GamePool gamePool;//the referrence to the GamePool
							   //it contains all the information of the gameFace
	
	public Bar()
	{
		squares=new Location[3];
		for(int i=0;i<3;i++)
		{
			squares[i]=new Location();
		}
		
		this.gamePool=null;
	}
	
	public Bar(GamePool pGamePool)
	{
		squares=new Location[3];
		for(int i=0;i<3;i++)
		{
			squares[i]=new Location();
		}
		
		this.gamePool=pGamePool;
	}
	
	public Bar(Location[] pSquares)
	{
		this.squares=pSquares;
		this.gamePool=null;
	}
	
	/**
	 * get and set gamePool
	 */
	public void setGamePool(GamePool pGamePool)
	{
		this.gamePool=pGamePool;
	}
	
	public GamePool getGamePool()
	{
		return this.gamePool;
	}
	/**
	 *get squares
	 */
	public Location[] getSquares()
	{
		return this.squares;
	} 
	
	/**
	 * Check if the bar hits the ground or other bar
	 */
	/*public boolean checkBottom()
	{
		if(squares[2].getX()+1==GamePool.getRow())
			return true;
		else 
		return false;
	}*/
	
		public boolean checkBottom()
	{   
		if(squares[2].getX()+1==GamePool.getRow()
				||GamePool.get(this.squares[2].getX()+1, this.squares[2].getY())!=0)
			return true;
		else return false;
	}
	/**
	 * Judge if the bar is still in the field
	 */
	public boolean isInside()
	{
		if(squares[0].getX()>=0&&squares[2].getX()<GamePool.getRow()
				&&squares[0].getY()>=0&&squares[2].getY()<GamePool.getColumn())
			return true;
		else return false;
	}
	
	/**
	 * Exchange the color of the bar
	 */
	public synchronized void shift()
	{
		
		
			GamePool.clear(this);
		
			
			int temp=squares[2].getColor();
			squares[2].setColor(squares[1].getColor());
			squares[1].setColor(squares[0].getColor());
			squares[0].setColor(temp);
			
			
				GamePool.set(this);
	
			
			
	
	
	}
	
	/**
	 * Move the bar to the left 
	 */
	public synchronized void left()
	{
		if(squares[0].getY()>0)
		{
			if(gamePool.gamePool[squares[0].x][squares[0].y-1]==0
			&&gamePool.gamePool[squares[1].x][squares[1].y-1]==0
			&&gamePool.gamePool[squares[2].x][squares[2].y-1]==0)
			{
				
				
					GamePool.clear(this);
			
				
				for(int i=0;i<3;i++)
				{
					squares[i].decreaseY();
				}
				
				
					GamePool.set(this);
			
				
			}
			
		}
	}
	
	/**
	 * Move the bar to the right
	 */
	public synchronized void right()
	{
	
		
		if(squares[0].getY()<GamePool.WIDTH-1)
		{
			if(gamePool.gamePool[squares[0].x][squares[0].y+1]==0
			&&gamePool.gamePool[squares[1].x][squares[1].y+1]==0
			&&gamePool.gamePool[squares[2].x][squares[2].y+1]==0)
			{
						
				
				
					GamePool.clear(this);
				
				
				for(int i=0;i<3;i++)
				{
					squares[i].increaseY();
				}
				
				
				
					GamePool.set(this);
	
				
			}
		}
	}
	
	/**
	 * Drop the bar down one step
	 */
	public synchronized boolean dropOneStep()
	{
		if(squares[2].getX()+1<GamePool.getRow()
				&&GamePool.get(this.squares[2].getX()+1, this.squares[2].getY())==0)
		{
			
			
				GamePool.clear(this);
			
			
			for(int i=0;i<3;i++)
			{
				squares[i].increaseX();
			}
		
			
			
				GamePool.set(this);
		
			
			
			return true;
		}
		else return false;
	}
	
	/**
	 * Drop the bar down till to the bottom 
	 */
	public synchronized void drop()
	{
		boolean tag=dropOneStep();
		while(tag)
		{
			tag=dropOneStep();
		}
	}
	
}

⌨️ 快捷键说明

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