AquaFish: A Fish in an Aquarium

Inspired by Part I of the Marine Biology Simulation Case Study,
available from the College Board for face-to-face teaching purposes.


General Description

Objects of the AquaFish class represent fish that can swim in an aquarium. These fish have color, a location (represented as x, y coordinates), and a direction (facing right or facing left). Each fish also has a unique ID number to distinguish it from other fish.

For a fish in an aquarium, the format produced by the ToString method is:

    ID(C,X,Y,D)
        where ID is the fish's unique ID,
              C  is the fish's color,
              X  is its X coordinate in the aquarium,
              Y  is its Y coordinate in the aquarium, and
              D  is the direction it is facing ('R' = right, 'L' = left)
For a fish not in an aquarium, the format is:
    ID(C,D)
        where ID is the fish's unique ID,
              C  is the fish's color,
              D  is the direction it is facing ('R' = right, 'L' = left)

The following object invariant is always true for an AquaFish instance:

    FacingRight() == ! FacingLeft()  and
    InAquarium() implies that the X and Y Coordinates represent valid
                         locations in the aquarium.

Author: Alyce Brady
Created: 7/1/99

Acknowledgements: This class is part of the Aquarium Lab Series, which was inspired by the College Board AP CS Marine Biology Case Study.

This AquaFish class is NOT the same as the AquaFish class in Part I of the Case Study, but is loosely based on that class and the Fish class in Part II.



Interface

class AquaFish
{
  public:

    // Constructors
        // The default constructor is necessary in order to create
	// arrays or vectors of fish, but otherwise should not be used.
	// AquaFish in an array or vector should immediately be replaced
	// with "correctly" constructed fish in an aquarium.
	// Unless otherwise specified, new AquaFish fish are black and
	// are placed at a random valid location in the aquarium.  (This
	// assumes that the aquarium has width and height greater than 0.)
	// It is also possible to specify the color and location of a
	// new fish when it is constructed.

    AquaFish();
        // postcondition: InAquarium() == false, Color() == "black",
        //     fish is facing right

    AquaFish(Aquarium a);
        // postcondition: Color() == "black",
	//     InAquarium() implies the location of fish is a valid
	//     location in a, fish is facing right

    AquaFish(Aquarium a, apstring initColor);
        // postcondition: Color() == initColor,
	//     InAquarium() implies the location of fish is a valid
	//     location in a, fish is facing right

    AquaFish(Aquarium a, int x_coord, int y_coord);
        // precondition:  (x_coord, y_coord) is a valid location in a
        // postcondition: InAquarium() == true, Color() == "black",
        //     location of fish is (x_coord, y_coord), fish is facing right

    AquaFish(Aquarium a, apstring initColor,
                int x_coord, int y_coord);
        // precondition:  (x_coord, y_coord) is a valid location in a
        // postcondition: InAquarium() == true, Color() == initColor,
        //     location of fish is (x_coord, y_coord), fish is facing right

    // Observer Functions

    int Id()                  const;
        // postcondition: returns unique id number of fish

    apstring Color()            const;
        // postcondition: returns fish color

    bool InAquarium()         const;
        // postcondition: returns true iff fish was constructed
        //     in an aquarium with width and height > 0

    bool AtWall()         const;
        // precondition:  InAquarium() is true
        // postcondition: returns true iff fish is against a side wall

    bool AtLeftWall()         const;
        // precondition:  InAquarium() is true
        // postcondition: returns true iff fish is against the left side wall

    bool AtRightWall()        const;
        // precondition:  InAquarium() is true
        // postcondition: returns true iff fish is against the right side wall

    bool AtSurface()          const;
        // precondition:  InAquarium() is true
        // postcondition: returns true iff fish is at the water's surface

    bool AtBottom()           const;
        // precondition:  InAquarium() is true
        // postcondition: returns true iff fish is at the bottom of the aquarium

    bool FacingRight()        const;
        // postcondition: returns true if the fish is facing right

    bool FacingLeft()         const;
        // postcondition: returns true if the fish is facing left

    bool FacingWall()         const;
        // precondition:  InAquarium() is true
        // postcondition: returns true if the fish is against a wall and
        //                is facing it

    int XCoord()              const;
        // precondition:  InAquarium() is true
        // postcondition: returns x-coordinate of current fish position

    int YCoord()              const;
        // precondition:  InAquarium() is true
        // postcondition: returns y-coordinate of current fish position

    apstring ToString()         const;    
        // postcondition: returns a stringized form of AquaFish

    // Modifier Functions

    void MoveForward();
        // precondition:  InAquarium() is true
	// postcondition: fish has moved forward one space in the
        //                current direction

    void ChangeDir();
        // precondition:  InAquarium() is true
        // postcondition: fish is now facing in the opposite direction

  private:
     // not part of the public interface
};