//****************************************************************// // // // Copyright (c) 1997. // // Dr. Michael A. Redmond // // // // This software may not be distributed further without permission from // // Dr Michael A. Redmond. // // // // This software is distributed WITHOUT ANY WARRANTY. No claims are made // // as to its functionality or purpose. // // // // Title: Tw_Color.cpp // Use: Provides abstraction for the color of pegs and links // for twixt // Author: Dr Michael A. Redmond // // Date: 3/05/97 // // // // //**************************************************************************// #include #include "tw-color.h" // constructor useful with 0 or 1 args (0 args color is empty) Tw_Color::Tw_Color (color col) : colorv(col) { } color Tw_Color::GetColor () const { return colorv; } // give the opposite color from the current color color Tw_Color::GetRevColor () const { if (colorv == black) return white; if (colorv == white) return black; // if empty just return empty return colorv; } void Tw_Color::SetColor (color col) { colorv = col; } // reverse the color in the object void Tw_Color::RevColor () { if (colorv == black) colorv = white; if (colorv == white) colorv = black; // if empty just keep empty } // returns a one char abreviation for the color // like insert but returns the char instead of putting it in // the output stream char Tw_Color::GetCharRep() const { char colchar; //DEBUG clog << "Color getCharRep working with: " << GetColor() << endl; switch(GetColor()) { case empty: colchar = '.'; break; case black: colchar = 'B'; break; case white: colchar = 'W'; break; case illegal: colchar = ' '; break; default: colchar = 'E'; } return colchar; } // Insert displays a one char abreviation for the color void Tw_Color::Insert(ostream &sout) const { char colchar; switch(GetColor()) { case empty: colchar = '.'; break; case black: colchar = 'B'; break; case white: colchar = 'W'; break; case illegal: colchar = 'I'; break; default: colchar = 'E'; } sout << colchar << " "; } // the problem here is that this could be done in the middle of // function chaining. So we can't just assume that anything after the // first char should be ignored void Tw_Color::Extract(istream &sin) { char inchar = ' '; bool gotit = false; while ((inchar != '\n') && !gotit) { sin >> inchar; switch(inchar) { case ' ': break; // just skip blanks case '.': case 'E': case 'e': SetColor(empty); gotit = true; break; case 'B': case 'b': SetColor(black); gotit = true; break; case 'W': case 'w': SetColor(white); gotit = true; break; default: SetColor(empty); gotit = true; cerr << "Invalid Color Entered - Set to Empty" << endl; } } return; } // inserting a Color // overloading built-in << so can use same syntax as with built-in object types ostream& operator<<(ostream &sout, const Tw_Color &col) { col.Insert(sout); // return the result so that this insertion can be cascaded // with other insertions in a single expression return sout; } // extracting a Color // overloading built-in >> so can use same syntax as with built-in object types istream& operator>>(istream &sin, Tw_Color &col) { // uses member function for the class to actually carry out // the operation col.Extract(sin); // return the stream object so that this extraction can be cascaded // with other extractions in a single expression return sin; } bool operator==(const Tw_Color &left, const Tw_Color &right) { return (left.GetColor() == right.GetColor()); } bool operator!=(const Tw_Color &left, const Tw_Color &right) { return (left.GetColor() != right.GetColor()); } bool operator!=(const Tw_Color &left, const color &right) { return (left.GetColor() != right); }