Class Field


public class Field extends Object
A field, which contains things and roaming critters. The field is represented as a grid, with each thing occupying exactly one grid cell and each grid cell occupied by at most one thing at a time.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Field(int numrows, int numcols)
    Create a new field with the specified dimensions.
  • Method Summary

    Modifier and Type
    Method
    Description
    Thing
    at(int row, int col)
    Return the thing at the specified row and column in the field.
    boolean
    canMove(Animal animal, int direction)
    Determine if the specified animal can move one spot in the specified direction.
    void
    Clear the field so that it is empty except for the hedge of bushes around the outside edge.
    int
    distance(Animal animal, int direction)
    Determine the distance between the specified animal and the next thing, looking in the specified direction.
    void
    draw(javafx.scene.canvas.GraphicsContext g, int cellsize)
    Draw the field, including everything in it.
    int
    Returns the number of columns in the field.
    int
    Returns the number of rows in the field.
    Thing
    look(Animal animal, int direction)
    Return the thing seen by the specified animal, looking in the specified direction.
    void
    move(Animal animal, int direction)
    Move the specified animal one space in the specified direction.
    Thing
    neighbor(Animal animal, int direction)
    Return the thing in the cell which is a neighbor of the specified animal and is in the specified direction.
    void
    place(Thing thing, int row, int col)
    Place the specified thing at the specified spot in the field.
    void
    placeRandomly(Thing thing)
    Place the specified thing at a random open spot in the field.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Field

      public Field(int numrows, int numcols)
      Create a new field with the specified dimensions.
      Parameters:
      numrows - number of rows (0 < numrows)
      numcols - number of columns (0 < numcols)
  • Method Details

    • at

      public Thing at(int row, int col)
      Return the thing at the specified row and column in the field. Returns null if the spot is empty.
      Parameters:
      row - row of thing to return (0 <= row < height())
      col - column of thing to return (0 <= col < width())
      Returns:
      thing at the specified row and column in the field
    • canMove

      public boolean canMove(Animal animal, int direction)
      Determine if the specified animal can move one spot in the specified direction. Returns true if so, false if the animal is blocked by another thing or would move outside the bounds of the field.
      Parameters:
      animal - the animal whose movement is to be checked (animal != null and the animal's position is in the field)
      direction - the direction to check (Direction.MIN_DIRECTION <= direction <= Direction.MAX_DIRECTION or direction == Direction.NONE)
      Returns:
      true if the animal can move in the specified direction, false otherwise
    • clearField

      public void clearField()
      Clear the field so that it is empty except for the hedge of bushes around the outside edge.
    • distance

      public int distance(Animal animal, int direction)
      Determine the distance between the specified animal and the next thing, looking in the specified direction. The distance is 0 if direction is Direction.NONE. Returns the distance to the edge of the field if nothing blocks the animal's view.
      Parameters:
      animal - the animal who is looking (animal != null and the animal's position is in the field)
      direction - the direction to look in (Direction.MIN_DIRECTION <= direction <= Direction.MAX_DIRECTION or direction == Direction.NONE)
      Returns:
      the distance between the specified animal and the next thing in the specified direction, or 0 if the direction is Direction.NONE
    • getNumCols

      public int getNumCols()
      Returns the number of columns in the field.
      Returns:
      the number of columns in the field
    • getNumRows

      public int getNumRows()
      Returns the number of rows in the field.
      Returns:
      the number of rows in the field
    • look

      public Thing look(Animal animal, int direction)
      Return the thing seen by the specified animal, looking in the specified direction. Returns null if the direction is Direction.NONE or nothing is visible before the edge of the field is reached.
      Parameters:
      animal - the animal whose is looking (animal != null and the animal's position is in the field)
      direction - the direction to look in (Direction.MIN_DIRECTION <= direction <= Direction.MAX_DIRECTION or direction == Direction.NONE)
      Returns:
      the closest thing to the specified animal in the specified direction, or null if the direction is Direction.NONE or nothing is visible in the specified direction
    • move

      public void move(Animal animal, int direction)
      Move the specified animal one space in the specified direction. If there is something already in that cell, it is overwritten.
      Parameters:
      animal - the animal to move (animal != null and the animal's position is in the field)
      direction - the direction to move (canMove(animal,direction) is true, or it is desired to overwrite the thing in the neighboring cell and the neighboring cell is within the confines of the field)
    • neighbor

      public Thing neighbor(Animal animal, int direction)
      Return the thing in the cell which is a neighbor of the specified animal and is in the specified direction. Returns null if the direction is Direction.NONE, the cell is empty, or the cell is outside the bounds of the field.
      Parameters:
      animal - the animal whose neighbor is being found (animal != null and the animal's position is in the field)
      direction - the direction to look in (Direction.MIN_DIRECTION <= direction <= Direction.MAX_DIRECTION or direction == Direction.NONE and the animal is in the field)
      Returns:
      the thing in the adjacent cell in the specified direction, or null if the direction is Direction.NONE, the neighboring cell is empty, or the animal is not in the field
    • draw

      public void draw(javafx.scene.canvas.GraphicsContext g, int cellsize)
      Draw the field, including everything in it.
      Parameters:
      g - graphics context
      cellsize - size for one grid cell
    • placeRandomly

      public void placeRandomly(Thing thing)
      Place the specified thing at a random open spot in the field. For animals, the animal's position is updated to reflect where the animal was placed.
      Parameters:
      thing - the thing to place
    • place

      public void place(Thing thing, int row, int col)
      Place the specified thing at the specified spot in the field. If there is already something there, it will be replaced. For animals, the animal's position is updated to reflect where the animal was placed.
      Parameters:
      thing - the thing to place
      row - row in the field
      col - col in the field