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

📄 catfish.java.bak

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


/**
 * Catfish - simulates a catfish - can swim, eat, and consume 
 * energy in the process.
 * 
 *
 */
public class Catfish extends LivingBeing {

	/**
	 * The catfish is born "alive". 
	 * Then it dies, becoming a corpse. 
	 */
	private static final String ALIVE = "alive";

	/**
	 * The catfish is born "alive". 
	 * Then it dies, becoming a "dead" corpse. 
	 */
	private static final String DEAD = "dead";
     /**
	  *
	  */
	  /**
	  *amount of energy a catfish must expend to swim.
	  */
	       private static final int ENERGY_TO_SWIM = 2;
	  /**
	  *amount of energy a catfish must expend to eat.
	  */
           private static final int ENERGY_TO_EAT = 1;
      /**
	  *
	  */
          private static final int DEBUG = 0;
	  /**
	  *energy a catfish must expend to look for food.
	  */
	      private static final int ENERGY_TO_LOOK_FOR_FOOD = 1;
      /**
	  *energy a catfish can gain from a full meal of AlgaeColony.
	  */
	       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;

	
	// Concept example: final. since it is a constant 
	// Concept example: static. since only one value is needed 
	// 						irrespective of number of object instances 
	
	/**
	 * 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;
	 
	
	/**
	 * String constant - used to indicate the direction catfish is facing.
	 */
	private static final String RIGHT = "right";

	/**
	 * String constant - used to indicate the direction catfish is facing.
	 */
	private static final String LEFT = "left";

	/**
	 * String constant - used to indicate the direction catfish is facing.
	 */
	private static final String UP = "up";

	/**
	 * String constant - used to indicate the direction catfish is facing.
	 */
	private static final String DOWN = "down";

	/**
	 * Name of species
	 */
	private static final String SPECIES = "Catfish";

	/**
	 * Row-wise location of the catfish
	 */
	private int row;

	/**
	 * Column-wise location of the catfish
	 */
	private int column;

	/**
	 * Is the catfish dead or alive?
	 */
	private String deadOrAlive;
	
	/**
	 * Amount of energy the catfish has.
	 */
	private int energy;

	/**
	 * Age expressed as blocks of time lived
	 */
	private int age = 0;

	/**
	 * Name of this catfish.
	 */
	private final String name;

	/**
	 * The simulation to which this catfish belongs.
	 * This is needed so the catfish can send a message 
	 * to simulation and ask
	 * for prey (or predator) in the neighboring locations. 
	 * Prey is food. Food is good!
	 */
	private Simulation simulation;

	/**
	 * Minimum energy level needed to survive.
	 * The minimum could increase as the individual grows.
	 */
	private int minEnergy;
	
	/**
	 * Maximum energy level that the catfish could carry.
	 * The maximum could change as the individual grows.
	 */
	private int maxEnergy;
	
	/**
	 * Which direction am I facing.
	 */
	private String direction; 

	/**
	 * 
	 * Number of Catfish created
	 */
	private static int nCatfishCreated = 0; 

	/**
	 * Constructor. Initialize an catfish 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) {

			name = SPECIES + nCatfishCreated;
			
	}
	
	/**
	 * Get the row at which the catfish is located 
	 * 
	 * @return - the row of the catfish's location. 
	 */		
	public int getRow() {
		return row;
	}

	/**
	 * Get the column at which the catfish is located
	 * 
	 * @return - the column of the catfish's location. 
	 */		
	public int getColumn() {
		return column;
	}

	/**
	 * Get the catfish's age
	 * 
	 * @return the age of the catfish expressed in blocks of time
	 */
	public int getAge() {
		return age;
	}

	/**
	 * Color of the catfish expressed in hex notation.
	 * For example, the "green-est" color is "#00FF00",
	 * "blue-est" is "#0000FF", the "red-est" is "#FF0000".
	 * 
	 * @return the rgb color in hex notation. preceded by a pound character '#'
	 */
	public String getColor() {
		return "#FFFFFF"; // default is white.
	}

	/**
	 * Get the name of this catfish
	 * 
	 * @return the name of the catfish.
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Get the minimum energy needed to live.
	 * 
	 * @return the minimum energy needed for the catfish to live.
	 */
	private int getMinEnergy() {
		return minEnergy;
	}
	
	/**
	 * get the maximum energy that the catfish can carry.
	 * 
	 * @return the maximum energy the catfish can carry.
	 */
	private int getMaxEnergy() {
		return maxEnergy;
	}
	

	/**
	 * Get the energy currently carried by the catfish.
	 * 
	 * @return current energy level of the organism
	 */
	public int getEnergy() {
		return energy;
	}

	/**
	 * Sets energy level.
	 * If new energy level is less than minimum energy level, the organism dies.
	 * New energy level is capped at maximum energy level.
	 */
	private void setEnergy(int newEnergy) {
	/*************************************************/	
		if(newEnergy < getMinEnergy())
        {
            energy = newEnergy;
            die();
        } else
        if(newEnergy > getMaxEnergy())
        {
            energy = getMaxEnergy();
        } else
        {
            energy = newEnergy;
        }
    /***************************************************/
	}

	/**
	 * Die: Change the deadOrAlive to DEAD.
	 */
	public void die() {
		deadOrAlive = DEAD;
	}

	/**
	 * Is the catfish dead?
	 * 
	 * @return <code>true</code> if dead. <code>false</code>, otherwise.
	 */
	public boolean isDead() {
		return (deadOrAlive == DEAD);
	}

	/**
	 * Get the direction faced by the catfish.
	 * 
	 * @return the facing direction.
	 */
	private String getDirection() {
		return direction;
	}

	/** 
	 * Is the catfish hungry?
	 * 
	 * @return True, if hungry. False, otherwise.
	 */
	private boolean isHungry() {
		
		// Hungry, if current energy level is less than twice the 
		// amount needed for survival.
	/***************************************/
      return getEnergy() < 2 * getMinEnergy();
	/****************************************/
	
	}

	/**
	 * Move the catfish to a new row, if new row is within lake bounds.
	 * 
	 * @param newRow - the row to move to.
	 * @return the row moved to. Lake boundary limits movement. -1, if dead.
	 */
	private int moveToRow(int newRow) {
	/*****************************************************/	
		if(isDead())
        {
            return -1;
        }
        if(newRow > simulation.getLastRow())
        {
            newRow = simulation.getLastRow();
        } else
        if(newRow < simulation.getFirstRow())
        {
            newRow = simulation.getFirstRow();
        }
        if(newRow < row)
        {
            direction = "up";
        } else
        if(newRow > row)
        {
            direction = "down";
        }

	/*****************************************************/	
		return row;
	}

	/**
	 * Move the catfish to a new column, if new column is within lake bounds.
	 * 
	 * @param newColumn - the column to move to.
	 * @return the column moved to. Lake boundary limits movement.
	 */
	private int moveToColumn(int newColumn) {
	/************************************************************/
        if(isDead())
        {
            return -1;
        }
        if(newColumn > simulation.getLastColumn())
        {
            newColumn = simulation.getLastColumn();
        } else
        if(newColumn < simulation.getFirstColumn())
        {
            newColumn = simulation.getFirstColumn();
        }
        if(newColumn < column)
        {
            direction = "left";
        } else
        if(newColumn > column)
        {
            direction = "right";
        }
        column = newColumn;
	/**********************************************************/

		return column;
	}

	/**
	 * 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() {
				
		return "/Catfish-right.gif";
	}


	/**
	 * Look for food in the neighborhood. Consume some energy in the process.
	 * 
	 * @return a neighboring algae that is food.
	 */
	private AlgaeColony lookForFoodInNeighborhood() {
	/**********************************************************/
	   setEnergy(getEnergy() - 1);
        if(isDead())
        {
            return null;
        }
        Vector neighbors = simulation.getNeighbors(getRow(), getColumn(), 1);
        for(int neighborIndex = 0; neighborIndex < neighbors.size(); neighborIndex++)
        {
            if(neighbors.get(neighborIndex) instanceof AlgaeColony)
            {
                return (AlgaeColony)neighbors.get(neighborIndex);
            }
        }
	/***********************************************************/
		return null;
	}
    /************************************************************/
	private void swimIfPossible()
    {
        if(isDead())
        {
            return;
        }
        if(isHungry())
        {
            AlgaeColony food = lookForFoodInNeighborhood();
            if(food != null)
            {
                setEnergy(getEnergy() - 2);
                if(isDead())
                {
                    return;
                }
                moveToRow(food.getRow());
                moveToColumn(food.getColumn());
            }
            return;
        }
        setEnergy(getEnergy() - 2);
        if(isDead())
        {
            return;
        }
        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);
        }
    }
/*************************************************************/

    private void eatIfPossible()
    {
        if(isDead())
        {
            return;
        }
        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(10);
                setEnergy((getEnergy() + energyGained) - 1);
                return;
            }
        }

    }
/*************************************************************/

	/**
	 * Catfish lives its life. It may lose or gain energy.
	 */
	public void liveALittle() {
/*******************************************************/		 
		 if(isDead())
        {
            return;
        } else
        {
            age++;
            swimIfPossible();
            eatIfPossible();
            minEnergy = getMinEnergy() + 5;
            maxEnergy = getMaxEnergy() + 10;
            return;
        }
	/*********************************************/

   
		
	}

}

⌨️ 快捷键说明

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