/** * Aquarium Lab Series * Copyright (C) 2000 Alyce Brady * * Implementation file for AquaFish class -- see aquafish.h for details. * * 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 "aquafish.h" #include "randgen.h" #include "utils.h" // initialization of class variables int AquaFish::nextAvailableID = 0; // constructors AquaFish::AquaFish() : myId(nextAvailableID++), myColor("black"), myEnviron(0, 0), myX(0), myY(0) // postcondition: InAquarium() == false, Color() == "black" // fish is facing right { } AquaFish::AquaFish(Aquarium a) : myId(nextAvailableID++), myColor("black"), myEnviron (a) // postcondition: Color() == "black", // InAquarium() implies the location of fish is a valid // location in a, fish is facing right { // Initialize location and direction InitLocDir(); } AquaFish::AquaFish(Aquarium a, apstring initColor) : myId(nextAvailableID++), myColor(initColor), myEnviron (a) // postcondition: Color() == initColor, // InAquarium() implies the location of fish is a valid // location in a, fish is facing right { // Initialize location and direction InitLocDir(); } AquaFish::AquaFish(Aquarium a, int x_coord, int y_coord) : myId(nextAvailableID++), myColor("black"), myX(x_coord), myY(y_coord), myDir(1), myEnviron (a) // 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 { if ( ! a.ValidLoc (myX, myY) ) InitLocDir(); } AquaFish::AquaFish(Aquarium a, apstring initColor, int x_coord, int y_coord) : myId(nextAvailableID++), myColor(initColor), myX(x_coord), myY(y_coord), myDir(1), myEnviron (a) // 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 { if ( ! a.ValidLoc (myX, myY) ) InitLocDir(); } // public observer functions int AquaFish::Id() const // postcondition: returns unique id number of fish { return myId; } apstring AquaFish::Color() const // postcondition: returns fish color { return myColor; } bool AquaFish::InAquarium() const // postcondition: returns true iff fish was constructed in, or has been placed // in, an aquarium with width and height > 0 { return myEnviron.Width() > 0 && myEnviron.Height() > 0; } bool AquaFish::AtWall() const // precondition: InAquarium() is true // postcondition: returns true iff fish is against a side wall { return AtLeftWall() or AtRightWall(); } bool AquaFish::AtLeftWall() const // precondition: InAquarium() is true // postcondition: returns true iff fish is against the left side wall { return (myX == myEnviron.LeftWallLoc() + 1); } bool AquaFish::AtRightWall() const // precondition: InAquarium() is true // postcondition: returns true iff fish is against the right side wall { return (myX == myEnviron.RightWallLoc() - 1); } bool AquaFish::AtSurface() const // precondition: InAquarium() is true // postcondition: returns true iff fish is at the water's surface { return (myY == 0); } bool AquaFish::AtBottom() const // precondition: InAquarium() is true // postcondition: returns true iff fish is at the bottom of the aquarium { return (myY == myEnviron.Height() - 1); } bool AquaFish::FacingRight() const // postcondition: returns true if the fish is facing right { return myDir == 1; } bool AquaFish::FacingLeft() const // postcondition: returns true if the fish is facing left { return ! FacingRight(); } bool AquaFish::FacingWall() const // postcondition: returns true if the fish is against a wall and facing it { return ( AtLeftWall() and FacingLeft() ) or ( AtRightWall() and FacingRight() ); } int AquaFish::XCoord() const // precondition: InAquarium() is true // postcondition: returns x-coordinate of current fish position { return myX; } int AquaFish::YCoord() const // precondition: InAquarium() is true // postcondition: returns y-coordinate of current fish position { return myY; } apstring AquaFish::ToString() const // postcondition: returns a stringized form of AquaFish { apstring fishDesc=IntToString(myId) + "(" + myColor + ","; if ( InAquarium() ) fishDesc += IntToString(myX) + "," + IntToString(myY) + ","; if ( FacingRight() ) fishDesc+="R)"; else fishDesc+="L)"; return fishDesc; } // public modifier functions void AquaFish::MoveForward() // precondition: InAquarium() is true // postcondition: fish has moved forward one space in the current direction { if ( InAquarium() ) myX += myDir; } void AquaFish::ChangeDir() // precondition: InAquarium() is true // postcondition: fish is now facing in the opposite direction { if ( InAquarium() ) myDir *= -1; } // private helper functions void AquaFish::InitLocDir() // precondition: InAquarium() is true // determines initial location and direction for constructors { if ( InAquarium() ) { RandGen r; myX = r.RandInt(myEnviron.Width()); myY = r.RandInt(myEnviron.Height()); myDir = 1; // For now, all fish are constructed facing right } }