#ifndef _AQUAFISH_H #define _AQUAFISH_H /** * Aquarium Lab Series * Copyright (C) 2000 Alyce Brady * * 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. * * 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 fish constructed * in the context of 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. * * 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 AP Marine Biology Case Study found at * http://www.collegeboard.org/ap/computer-science/marine_biology/ * 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. * * License: * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * */ #include "apstring.h" #include "aquarium.h" using namespace std; 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 fish "correctly" constructed in the context of 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; // 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: // helper functions void InitLocDir(); // determines initial location and direction // for constructors // class variables (shared by all instances of the class) static int nextAvailableID; // the next fish constructed will // get this ID nbr // instance variables (each instance has its own copy of these) int myId; // ID of this AquaFish apstring myColor; // color of this AquaFish Aquarium myEnviron; // the aquarium in which this fish swims int myX; // X-coordinate of this AquaFish int myY; // Y-coordinate of this AquaFish int myDir; // direction of this AquaFish: // 1 means moving right, -1 means moving left }; #endif