part4

Set 7

  • What methods are implemented in Critter?
    act 、getActors、processActors、getMoveLocations、selectionMoveLocation、makeMove

  • What are the five basic actions common to all critters when they act?
    getActors、processActors、getMoveLocations、selectionMoveLocation、makeMove

  • Should subclasses of Critter override the getActors method? Explain.
    Yes, if the new critter subclass selects its actors from different locations, it will need to override this method

  • Describe the way that a critter could process actors.
    one is to make all of the actors in it’s list change colors, two is to eat them all, three is to ask them to move

  • What three methods must be invoked to make a critter move? Explain each of these methods.
    getMoveLocations, selectMoveLocation, makeMove

    Moving a critter is a three-step process. First, the act method calls the getMoveLocations method. For a basic critter, this method returns a list of all the empty adjacent locations around the critter. After receiving the list of possible empty locations, the selectMoveLocation randomly chooses one of the locations and returns that location. If there are no empty locations to choose from, selectMoveLocation returns the current location of the critter. The returned location is then passed to the makeMove method, and the critter is moved to the new location

      //@file: info/gridworld/actor/Critter.java
      //@line: 38~47
       public void act()
      {
      if (getGrid() == null)
          return;
      ArrayList<Actor> actors = getActors();
      processActors(actors);
      ArrayList<Location> moveLocs = getMoveLocations();
      Location loc = selectMoveLocation(moveLocs);
      makeMove(loc);
      }
    
  • Why is there no Critter constructor?
    Critter extends Actor. The Actor class has a default constructor. If you do not create a constructor in a class, Java will write a default constructor for you. The Critter default constructor that Java provides will call super(), which calls the Actor default constructor. The Actor default constructor will make a blue critter that faces north.

      //@file: info/gridworld/actor/Actor.java
      //@line: 38~47
      public Actor()
      {
      color = Color.BLUE;
      direction = Location.NORTH;
      grid = null;
      location = null;
       }
    

Set 8

  • Why does act cause a ChameleonCritter to act differently from a Critter even though ChameleonCritter does not override act?
    The act method calls getActors, processActors, getMoveLocations, selectMoveLocation, and makeMove. The ChameleonCritter class overrides the processActors and makeMove methods. Therefore, calling act for a ChameleonCritter will produce different behavior than calling act for a Critter.

      //@file: info/gridworld/actor/Critter.java
      //@line: 38~47
       public void act()
      {
      if (getGrid() == null)
          return;
      ArrayList<Actor> actors = getActors();
      processActors(actors);
      ArrayList<Location> moveLocs = getMoveLocations();
      Location loc = selectMoveLocation(moveLocs);
      makeMove(loc);
      }
    
  • Why does the makeMove method of ChameleonCritter call super.makeMove?
    The makeMove method of the ChameleonCritter first changes the direction of the critter to face its new location. Then it calls super.makeMove of the Critter class to actually move to the new location. After it changes its direction, it behaves like (makeMove like) a Critter.

      //@file:info/gridworld/actor/Critter.java
      //@line: 125~131
        public void makeMove(Location loc)
       {
    	  if (loc == null)
         	 removeSelfFromGrid();
      	else
         	 moveTo(loc);
       }
    
  • How would you make the ChameleonCritter drop flowers in its old location when it moves?
    Modify the makeMove method to drop flowers in the old location. A variable is needed to keep track of the ChameleonCritter’s current location. After the critter moves, put a flower in its old location only if the critter actually moved to a new location. See the modified makeMove method below.

      public void makeMove(Location loc)
      {
      	Location oldLoc = getLocation();
      	setDirection(getLocation().getDirectionToward(loc));
      	super.makeMove(loc);
      	if(!oldLoc.equals(loc)) //don't replace yourself if you did not move
       	{
      	Flower flo = new Flower(getColor());
      	flo.putSelfInGrid(getGrid(), oldLoc);
      	}
      }
    

* Why doesn’t ChameleonCritter override the getActors method?

Because it processes the same list of actors that its base class Critter does. Since ChameleonCritter does not define a new behavior for getActors, it does not need to override this method.

  • Which class contains the getLocation method?
    The Actor class contains the getLocation method. All Actor subclasses inherit this method

      //@file:info/gridworld/actor/Actor.java
      //@line: 102~105
       public Location getLocation()
      {
      	 return location;
       }
    
  • How can a Critter access its own grid?
    A critter can access its grid by calling the getGrid method that it inherits from the Actor class.

      //@file:info/gridworld/actor/Actor.java
      //@line: 92~95
       public Grid<Actor> getGrid()
       {
     	 return grid;
      }
    

Set 9

  • Why doesn’t CrabCritter override the processActors method?
    A CrabCritter processes its actors by eating all of the neighbors returned when getActors is called. This is the same behavior that it inherits from its base class Critter. There is no need to override this method.

  • Describe the process a CrabCritter uses to find and eat other actors. Does it always eat all neighboring actors? Explain.
    The CrabCritter’s getActors method only looks for neighbors that are immediately in front of the crab critter and to its right-front and left-front locations. Any neighbors found in these locations will be “eaten” when the processActors method is called. Actors in the other neighboring locations will not be disturbed

      //@file:GridWorldCode/projects/critters/CrabCritter.java
      //@line: 44~57
      public ArrayList<Actor> getActors()
       {
      ArrayList<Actor> actors = new ArrayList<Actor>();
      int[] dirs =
          { Location.AHEAD, Location.HALF_LEFT, Location.HALF_RIGHT };
      for (Location loc : getLocationsInDirections(dirs))
      {
          Actor a = getGrid().get(loc);
          if (a != null)
              actors.add(a);
      }
    
      return actors;
       }
    
  • Why is the getLocationsInDirections method used in CrabCritter?
    The parameter for this method brings in an array of directions. For the crab critter, this array contains the directions of the possible neighbors that this crab can eat. The method getLocationsInDirections uses this array to determine and return valid adjacent locations of this critter in the directions given by the array parameter

      //@file:GridWorldCode/projects/critters/CrabCritter.java
      //@line: 101~114
       public ArrayList<Location> getLocationsInDirections(int[] directions)
    {
      ArrayList<Location> locs = new ArrayList<Location>();
      Grid gr = getGrid();
      Location loc = getLocation();
    
      for (int d : directions)
      {
          Location neighborLoc = loc.getAdjacentLocation(getDirection() + d);
          if (gr.isValid(neighborLoc))
              locs.add(neighborLoc);
      }
      return locs;
    }   
    
  • If a CrabCritter has location (3, 4) and faces south, what are the possible locations for actors that are returned by a call to the getActors method?
    (4,3), (4,4), and (4,5)

    //@file:GridWorldCode/projects/critters/CrabCritter.java
    //@line: 44~57
    public ArrayList<Actor> getActors()
     {
    ArrayList<Actor> actors = new ArrayList<Actor>();
    int[] dirs =
        { Location.AHEAD, Location.HALF_LEFT, Location.HALF_RIGHT };
    for (Location loc : getLocationsInDirections(dirs))
    {
        Actor a = getGrid().get(loc);
        if (a != null)
            actors.add(a);
    }
    
    return actors;
     }
    
  • What are the similarities and differences between the movements of a CrabCritter and a Critter?
    Similarities: When critters and crab critters move, they do not turn in the direction that they are moving. They both randomly choose their next location from their list of possible move locations.

    Differences: A crab critter will only move to its left or its right. A critter’s possible move locations are any of its eight adjacent neighboring locations. When a crab critter cannot move, it will randomly turn right or left. When a critter cannot move, it does not turn.

      //@file:GridWorldCode/projects/critters/CrabCritter.java
      //@line: 77~91
      public void makeMove(Location loc)
    {
      if (loc.equals(getLocation()))
      {
          double r = Math.random();
          int angle;
          if (r < 0.5)
              angle = Location.LEFT;
          else
              angle = Location.RIGHT;
          setDirection(getDirection() + angle);
      }
      else
          super.makeMove(loc);
       }
    
      //@file:info/gridworld/actor/Critter.java
      //@line: 102~105
        public void makeMove(Location loc)
       {
      if (loc == null)
          removeSelfFromGrid();
      else
          moveTo(loc);
    }
    
  • How does a CrabCritter determine when it turns instead of moving?
    If the parameter loc in makeMove is equal to the crab critter’s current location, it turns instead of moving

  • Why don’t the CrabCritter objects eat each other?
    A crab critter inherits the processActors method from the Critter class. This method only removes actors that are not rocks and not critters. Since a CrabCritter is a Critter, a crab critter will not eat any other critter

猜你喜欢

转载自blog.csdn.net/winding125/article/details/109142561