#ifndef _POSITION1_H #define _POSITION1_H /** * A Position represents a (row,column) in a grid * whose (0,0) is upper-left as in matrix coordinates. * * Once constructed, a position doesn't change * (all member functions are const), although * a Position can be assigned to an existing Position. * For example, * * Position p(2,3); * Position q; // default (-1,-1) * q = p; // q now at (2,3) * * Adjacent Positions of a given Position can be * determined as illustrated: * * Position p(5,5); * Position q; * * q = p.North(); // q is (4,5) * q = p.South(); // q is (6,5) * q = p.East(); // q is (5,6) * q = p.West(); // q is (5,4) * */ #include #include "apstring.h" class Position { public: // constructors Position(); Position(int r, int c); // Accessor functions int Row() const; int Col() const; Position North() const; Position South() const; Position East() const; Position West() const; bool Equals(const Position & rhs) const; apstring ToString() const; // Mutator fucntions void Input(istream & in); private: int myRow; int myCol; }; // Free fucntions ostream & operator << (ostream & out, const Position & pos); // postcondition: pos inserted onto stream out istream & operator >> (istream & in, Position & pos); // postcondition: Position value is read from in input stream bool operator == (const Position & lhs, const Position & rhs); // postcondition: returns true iff lhs == rhs bool operator < (const Position & lhs, const Position & rhs); // postcondition: returns true iff lhs < rhs /* Position() // post: Row() == -1, Col() == -1 Position(int r, int c) // post: Row() == r, Col() == c int Row() const // post: returns row of Position int Col() const // post: returns column of Position Position North() const // post: returns Position north of (up from) *this position Position South() const // post: returns Position south of (down from) *this position Position East() const // post: returns Position east (right) of *this position Position West() const // post: returns Position west (left) of *this position bool Equals(const Position & rhs) const // post: returns true iff *this position equals rhs apstring ToString() const // post: returns stringized form of Position [e.g. "(2, 3)"] void Input(istream & in) // pre: in is open // post: extracts from "in" and places values in *this ostream & operator << (ostream & out, const Position & pos) // pre: out is open // post: pos inserted onto stream out istream & operator >> (istream & in, Position & pos) // post: Position value is read from in input stream bool operator == (const Position & lhs, const Position & rhs) // post: returns true iff lhs == rhs bool operator < (const Position & lhs, const Position & rhs) // post: returns true iff lhs < rhs */ #endif