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

📄 catfish.java

📁 《湖泊生态的简单模拟》(Simulation Of Living Beings In A Lake )。这是我修读美国卡耐基梅隆大学Carnegie Mellon University(CMU)课程s
💻 JAVA
字号:
import java.util.Vector;

/**
 * Catfish - simulates a catfish - can swim, eat, and consume 
 * energy in the process.
 * 
 * @author 张维
 * @version 1.0.0
 */
public class Catfish extends Animal {

	/**
	 * Energy needed to swim in a block of time.
	 */
	private static final int ENERGY_TO_SWIM = 2;

	/**
	 * Energy needed to look for food once.
	 */
	private static final int ENERGY_TO_LOOK_FOR_FOOD = 1;
	
	/**
	 * Energy expended to eat once.
	 */
	private static final int ENERGY_TO_EAT = 1;
	
	/**
	 * Energy gained when a full meal is eaten.
	 */
  private static final int ENERGY_IN_A_FULL_MEAL = 10;	
  
	/**
	 * Lowest possible energy needed for a baby to survive. 
	 */
	private static final int BABY_MIN_ENERGY = 15;
	
	/**
	 * Maximum energy that a baby can store. 
	 */
	private static final int BABY_MAX_ENERGY = 100;
	
	/**
	 * For each block of time, the min energy grows by a certain amount
	 */
	private static final int MIN_ENERGY_GROWTH_INCREMENT = 5;
	
	/**
	 * For each block of time, the max energy grows by a certain amount
	 */
  private static final int MAX_ENERGY_GROWTH_INCREMENT = 10;
	
	/**
	 * Name of species
	 */
	private static final String SPECIES = "Catfish";
	
	/**
	 * 
	 * Number of Catfish created
	 */
	private static int nCatfishCreated = 0; 

	/**
	 * Constructor. Initialize an algae to start life at a specified 
	 * location with a specified energy. If location is out of bounds,
	 * locate the catfish at the nearest edge.
	 * 
	 * @param initialRow - the row at which the catfish is located
	 * @param initialColumn - the column at which the catfish is located
	 * @param initialSimulation - the simulation that the catfish belongs to
	 */
	public Catfish(
		int initialRow,
		int initialColumn,
		Simulation initialSimulation) {
		super(
			initialRow,
			initialColumn,
			initialSimulation,
			SPECIES + nCatfishCreated,
			BABY_MIN_ENERGY,
			BABY_MAX_ENERGY);
			
			++nCatfishCreated;
	}
	
	
	/**
	 * This individual belongs to the Catfish species.
	 *  
	 * @return The string indicating the species
	 */
	public String getSpecies() {
		return SPECIES;
	}

	/**
	 * Catfish should be displayed as an image.
	 * 
	 * @return a constant defined in {@link Simulation#IMAGE Simulation} class
	 */
	public String getDisplayMechanism() {
		return Simulation.IMAGE;
	}

	/**
	 * Get the image of the catfish
	 * 
	 * @return filename of Catfish image
	 */
	public String getImage() {
		
		if (getDirection() == RIGHT) {
			return "/Catfish-right.gif";
		}
		if (getDirection() == LEFT) {
			return "/Catfish-left.gif";
		}
		if (getDirection() == UP) {
			return "/Catfish-up.gif";
		}
		if (getDirection() == DOWN) {
			return "/Catfish-down.gif";
		}
		
		return "Catfish-right.gif";
	}

	/**
	 * Look for food in the neighborhood. Consume some energy in the process.
	 * 
	 * @return a neighboring living being that is food.
	 */
	private LivingBeing lookForFoodInNeighborhood() {
		
		int neighborIndex;
		// Looking for food consumes energy.
		setEnergy(getEnergy() - ENERGY_TO_LOOK_FOR_FOOD);

		if (isDead()) {

			return null;
		}

		Vector neighbors =
			simulation.getNeighbors(getRow(), getColumn(), 1);

		for (neighborIndex = 0;
			neighborIndex < neighbors.size();
			++neighborIndex) {
			if (neighbors.get(neighborIndex) instanceof AlgaeColony) {

				return (AlgaeColony) neighbors.get(neighborIndex);
			}
		}
		return null;
	}
  
  /**
   * The catfish swims to a new location in the lake if possible.  
   */
  protected void swimIfPossible() {
   	
        if(isDead())
            return;
            
        if(isHungry())
        {
            LivingBeing food = lookForFoodInNeighborhood();
            if(food != null)//Check if food is an instance of AlgaeColony.
            {
                setEnergy(getEnergy() - ENERGY_TO_SWIM);
                
                //Every time energy decreases , check if there is any energy left after consumption.
                if(isDead())
                    return;
                    
                moveToRow(food.getRow());
                moveToColumn(food.getColumn());
            }
        }
        
        setEnergy(getEnergy() - ENERGY_TO_SWIM);
        
        if(isDead())
            return;
            
        //Use random numbers ,which are generated by the simulation that the catfish belongs to ,
        //to simulate the probability of every direction--25% for each.     
        int direction = simulation.getRand().nextInt(4);
        
        if(direction == 0)
            moveToRow(getRow() - 1);
        if(direction == 1)
            moveToRow(getRow() + 1);
        if(direction == 2)
            moveToColumn(getColumn() - 1);
        if(direction == 3)
            moveToColumn(getColumn() + 1);
    }
   
   /**
    * The Catfish eats AlgaeColony and gains  energy  from a full meal of AlgaeColony
    * (i.e.the energy it stores is increased).
    */
   private void eatIfPossible() {
    	
        if(isDead())
            return;
            
        // Let us assume that the distant between the Catfish and the possible food(AlgaeColon) is 0,and
        // there may be other livebeings such as catfish ,algaecolon,crocodile.    
        Vector foodMaybe = simulation.getNeighbors(getRow(), getColumn(), 0);
        
        for(int neighborIndex = 0; neighborIndex < foodMaybe.size(); neighborIndex++)
            if(foodMaybe.get(neighborIndex) instanceof AlgaeColony)
            {
                AlgaeColony alg = (AlgaeColony)foodMaybe.get(neighborIndex);
                
                int energyGained = alg.giveUpEnergy(ENERGY_IN_A_FULL_MEAL);
                setEnergy((getEnergy() + energyGained) - ENERGY_TO_EAT);
            }
    }

	/**
	 * Catfish lives its life. It may lose or gain energy.
	 */
	public void liveALittle() {
		
		if (isDead()) {
			return;
		}
		else {
			super.liveALittle();
	  
	    swimIfPossible();
      eatIfPossible();
     
      // As I am growing bigger, I need to increase my minEnergy and maxEnergy.	       
      minEnergy = getMinEnergy() + MIN_ENERGY_GROWTH_INCREMENT	;
      maxEnergy = getMaxEnergy() + MAX_ENERGY_GROWTH_INCREMENT ;
    } 			
	}
}

⌨️ 快捷键说明

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