// Filename: NamedTimerPlay.cxx // // A implementation of countdown chess clocks // Two clocks are initialized to the minute:second values given as input // Minute:second pairs are read and applied to alternate clocks // until end-of-file or a clock expires // Values of both clocks are printed after each clock update // By Beth Katz and David Hutchens, updated Spring 1998 and Fall 2002 #include #include "NamedTimer.h" using namespace std; // read in a time as minutes:seconds int readTime(int & minutes, int & seconds) { char ch; // should be a colon between minutes and seconds cout << "Time used in minutes:seconds? "; cin >> minutes >> ch >> seconds; return cin.good(); } // get initial time values from user and fill timer information NamedTimer initializeTimer(const char * timerName) { int minutes, seconds; char colon; cout << "Enter time for " << timerName << " in minutes:seconds => "; cin >> minutes >> colon >> seconds; NamedTimer theTimer(minutes, seconds, timerName); return theTimer; } // print out the timer values for both players void printTimers(const NamedTimer & white, const NamedTimer & black) { cout << " " << white << " " << black << endl; } int main ( ) { NamedTimer white; NamedTimer black; int blacksTurn = 0; int minutes, seconds; white = initializeTimer("White"); black = initializeTimer("Black"); printTimers(white, black); while ( !(white.outOfTime( ) || black.outOfTime( )) && readTime(minutes, seconds) ) { if (blacksTurn) { blacksTurn = false; black.subtract(minutes, seconds); } else { blacksTurn = true; white.subtract(minutes, seconds); } printTimers(white, black); } if (white.outOfTime( )) { cout << "White ran out of time" << endl; } else { cout << "Black ran out of time" << endl; } return 0; }