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

📄 catfish.java

📁 ssd1 exercise8以前做的 希望有帮助
💻 JAVA
字号:
import java.util.Vector;
/*
 * Created on Jul 5, 2003
 *
 */

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

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

	/**
	 * debugging level. 
	 */
	private static final int DEBUG = 0;
		
	/**
	 * 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;
	}

	/** 
	 * Swim to a new location if possible.
	 * Consumes some energy.
	 */
	protected void swimIfPossible() {

		LivingBeing food;

		if (isDead()) {

			return;
		}
		// 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.
			food = lookForFoodInNeighborhood();
			if (food != null) {
				// Consume energy to swim. 
				setEnergy(getEnergy() - ENERGY_TO_SWIM);
				if (isDead()) {

					return;
				}
				if (DEBUG > 50) {
					System.out.println(
						"moving from (row, col) = ("
							+ getRow()
							+ ", "
							+ getColumn()
							+ ") to ("
							+ food.getRow()
							+ ", "
							+ food.getColumn()
							+ ")");
				}
				moveToRow(food.getRow());
				moveToColumn(food.getColumn());

			} else {

				// Do not swim. Need to conserve energy.
			}

			return;
		} else {
			// Consume energy to swim.
			setEnergy(getEnergy() - ENERGY_TO_SWIM);
			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);
			}

			// Swim right.
			if (direction == 3) {
				moveToColumn(getColumn() + 1);
			}
		}
	}

	/**
	 * Eat food if available.
	 *
	 */
	private void eatIfPossible() {
		Vector foodMaybe;
		int neighborIndex;

		if (isDead()) {
			return;
		}

		foodMaybe = simulation.getNeighbors(getRow(), getColumn(), 0);
		for (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;
		}
		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 + -