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

📄 crocodile.java

📁 exm 3 Object-Oriented Programming and Design Introduction This course introduces students to proble
💻 JAVA
字号:
import java.util.Vector;
/*
 * Created on Jul 6, 2003
 *
 */

/**
 * Crocodile - to simulate artificial life. Eats small fish.
 * 
 * @author iCarnegie av
 *
 */
public class Crocodile extends Animal {
	
	/**
	 * Energy expended to wade during a block of time.
	 */
	private static final int ENERGY_TO_WADE = 10;
	
	/**
	 * Energy expended to eat once.
	 */
	private static final int ENERGY_TO_EAT = 10;
		
	/**
	 * Lowest possible energy needed for a baby to survive. 
	 */
	private static final int BABY_MIN_ENERGY = 1000;
	
	/**
	 * Maximum energy that a baby can store. 
	 */
	private static final int BABY_MAX_ENERGY = 2000;

	/**
	 * 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 = "Crocodile";
	
	/**
	 * Number of Crocodiles created so far
	 */
	private static int nCrocodilesCreated = 0;

	/**
	 * Construct and initialize a Crocodile.
	 * 
	 * @param initialRow - the row at which the crocodile is located
	 * @param initialColumn - the column at which the crocodile is located
	 * @param initialSimulation - the simulation that the crocodile belongs to
	 */
	public Crocodile(
		int initialRow,
		int initialColumn,
		Simulation initialSimulation) {
		super(
			initialRow,
			initialColumn,
			initialSimulation,
			SPECIES + nCrocodilesCreated,
			BABY_MIN_ENERGY,
			BABY_MAX_ENERGY);
			
			++nCrocodilesCreated;
	}

	/**
	 * Get the species
	 * 
	 * @return a string indicating the species
	 */
	public String getSpecies() {
		return SPECIES;
	}

	/**
	 * The display mechanism to use to display a crocodile.
	 * 
	 * @return a constant defined in {@link Simulation#IMAGE Simulation} class
	 */
	public String getDisplayMechanism() {
		return Simulation.IMAGE;
	}
	
	/**
	 * Get the filename that contains an image of the Crocodile
	 * 
	 * @return filename of Crocodile image
	 */
	public String getImage() {

		if (getDirection() == RIGHT) {
			return "/Crocodile-right.gif";
		}
		if (getDirection() == LEFT) {
			return "/Crocodile-left.gif";
		}
		if (getDirection() == UP) {
			return "/Crocodile-up.gif";
		}
		if (getDirection() == DOWN) {
			return "/Crocodile-down.gif";
		}

		return "Crocodile-right.gif";
	}

	/** 
	 * Wade to a new location if possible.
	 * Consumes some energy.
	 */
	private void wadeIfPossible() {

		int firstRow; // lake boundary
		int firstColumn; // lake boundary
		int lastRow; // lake boundary
		int lastColumn; // lake boundary
		int newRow; // new location for croc
		int newColumn; // new location for croc
		
		// Consume energy to swim.
		setEnergy(
			getEnergy() - ENERGY_TO_WADE);
		if (isDead()) {
			return;
		}

		// get the lake boundary.
		firstRow = simulation.getFirstRow();
		firstColumn = simulation.getFirstColumn();
		lastRow = simulation.getLastRow();
		lastColumn = simulation.getLastColumn();

		// Wade to a random location in the lake.
		newRow = simulation.getRand().nextInt(lastRow - firstRow + 1);
		newColumn = simulation.getRand().nextInt(lastColumn - firstColumn + 1);
		newRow = newRow + firstRow;
		newColumn = newColumn + firstColumn;

		// Move to the new location.
		moveToRow(newRow);
		moveToColumn(newColumn);

		return;
	}

	/**
	 * Eat if food is available in the current location.
	 */
	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 Catfish) {

				Catfish fish = (Catfish) foodMaybe.get(neighborIndex);
				int energyGained = fish.getEnergy();
				fish.die();

				// Gain energy. 
				// And spend ENERGY_TO_EAT, irrespective of amount gained.
				setEnergy(getEnergy() + energyGained - ENERGY_TO_EAT);
			}
		}
	}

	/**
	 * Live for a block of time.
	 */
	public void liveALittle() {

		if (isDead()) {
			return;
		}
		super.liveALittle();
		wadeIfPossible();
		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 + -