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

📄 catfish.java.bak

📁 卡耐基梅隆大学 SSD课程体系 ssd1 exesice8 答案
💻 BAK
字号:
import java.util.Vector;
import java.util.Random;

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

	/**
	 * Energy needed to swim in a block of time.
	 */
	private static final int ENERGY_TO_SWIM = 2;
    /**
	 *  
	 */
    
	private static final int DEBUG=0;
	/**
	 * Energy needed to look for food once.
	 */
	 private static final int ENERGY_TO_LOOK_FOR_FOOD = 1;
	/**
	  *amount of energy a catfish must expend to eat.
	  */
     private static final int ENERGY_TO_EAT = 1;
	/**
	  *energy a catfish must expend to look for food.
	  */
    
	 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;
    /**
	 * the amount by which the minimum energy stored by each catfish increases in a time block 
	 */
	 private static final int ENERGY_GROWTH_INCREMENT = 5;
     /**
	  *the amount by which the maximum energy stored by each catfish increases in a time block 
	  */
	 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";
		}else
        {
		 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;
	}
	/*******************************create a method let catfish swim***********************************/
    protected void swimIfPossible()
    {
        if(isDead())
        {
            return;//return the value of the Catfish.
        }
		// If hungry, swim to a location where there is food.
        if(isHungry())
        {
			// Naive swimming - We are not checking if a predator is in the destination.
            LivingBeing food = lookForFoodInNeighborhood();
            if(food != null)
            {
				// Consume energy to swim.
                setEnergy(getEnergy() - 2);
                if(isDead())
                {
                    return;//return the value of the Catfish.
                }
                moveToRow(food.getRow());
                moveToColumn(food.getColumn());
            }
			// Do not swim. Need to conserve energy.
            return;
        }
		// Consume energy to swim.
        setEnergy(getEnergy() - ENERGY_TO_LOOK_FOR_FOOD);
        if(isDead())
        {
            return;
        }
		// Swim at random in one of four directions.
		// Naive swimming - We are not checking if a predator is in the destination.
        int direction = simulation.getRand().nextInt(4);
        
		// Swim up.
		if(direction == 0)
        {
            moveToRow(getRow() - 1);
        }

		// Swim down.
        if(direction == 1)
        {
            moveToRow(getRow() + 1);
        }

		// Swim left
        if(direction == 2)
        {
            moveToColumn(getColumn() - 1);
        }

		// Swin right.
        if(direction == 3)
        {
            moveToColumn(getColumn() + 1);
        }
    }
/*******************************************finish*************************************************************/
/*********************************create a method that let the Catfish eat*************************************/
private void eatIfPossible()
    {
	    // If Catfish is dead, return the value of the Catfish .
        if(isDead())
        {
            return;
        }
		/**
		 *creat a vector .
		 *let the Catfish find the nearest neighbors of the AlgaeColony.
		 */
        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);
				// Spend ENERGY_TO_EAT, irrespective of amount gained.
                setEnergy((getEnergy() + energyGained) - ENERGY_TO_EAT);
                return;
            }
        }

    }
/*************************************************************************************************************/
	/**
	 * Catfish lives its life. It may lose or gain energy.
	 */
	public void liveALittle() {
		if (isDead()) {
			return;// If Catfish is dead, return the value of the Catfish .
		}else
		{
			super.liveALittle();//use method liveALittle.
			swimIfPossible();//use method swimIfPossible.
			eatIfPossible();//use method eatIfPossible.
			minEnergy=getMinEnergy()+ENERGY_GROWTH_INCREMENT;// As I am growing bigger, I need to increase my minEnergy .
            return;//return the value of the Catfish.
		}
	/*************************************************************/
	}
}

⌨️ 快捷键说明

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