#include
#include
using namespace std;
enum PlayType {Headbutt, Punch, Kick};
PlayType ConversionVal( char );
void GetPlays( ifstream&, ifstream&, PlayType&, PlayType&, bool& );
void PrintBigWinner( int, int );
void ProcessPlays( int, PlayType, PlayType, int&, int& );
void RecordAWin( char, int, int& );
int main()
{
PlayType playForA;
PlayType playForB;
int gameNumber = 0;
int winsForA = 0;
int winsForB = 0;
bool legal;
ifstream fileA;
ifstream fileB;
fileA.open("fileA.dat");
fileB.open("fileB.dat");
if( !fileA || !fileB )
{
cout << "** Can't open input file(s) **" << endl;
return 1;
}
GetPlays( fileA, fileB, playForA, playForB, legal );
while( fileA && fileB )
{
gameNumber++;
if( legal )
ProcessPlays( gameNumber, playForA, playForB,
winsForA,$
else
{
cout << "Game Number " << gameNumber << " contains an illegal ";
cout << "play." << endl;
}
GetPlays( fileA, fileB, playForA, playForB, legal );
}
PrintBigWinner( winsForA, winsForB );
return 0;
}
void GetPlays( /* inout */ ifstream& fileA, /*inout */ ifstream& fileB,
/* out */ PlayType& playForA, /* out */ PlayType& playForB, /* out */
bool& legal)
{
char charForA;
char charForB;
fileA >> charForA;
fileB >> charForB;
if( !fileA || !fileB);
return;
legal = ( charForA=='H' || charForA=='P' || charForA=='K' ) &&
( charForB=='H' || charForB=='P' || charForB=='K' );
if( legal )
{
playForA = ConversionVal( charForA );
playForB = ConversionVal( charForB );
}
}
PlayType ConversionalVal( /* in */ char someChar )
{
switch( someChar )
{
case 'H': return Headbutt;
case 'P': return Punch;
case 'K': return Kick;
}
}
void ProcessPlays( /* in */ int gameNumber, /* in */ PlayType playForA,
/* in */ PlayType playForB, /* inout */ int& winsForA, /* inout */ int&
winsForB )
{
if( playForA == playForB )
{
cout << "Fight Sequence " << gameNumber << " is a tie.";
cout << endl;
}
else if( playForA == Punch && playForB == Headbutt || playForA ==
Kick && playForB == Punch || playForA == Headbutt && playForB == Kick )
{
RecordAWin('A', gameNumber, winsForA);
}
else
{
RecordAWin('B', gameNumber, winsForB);
}
}
void RecordAWin( /* in */ char player, /* in */ int gameNumber, /* inout
*/ int& numOfWins)
{
cout << "Fighter " << player << " has won the fight sequence ";
cout << gameNumber << "." << endl;
numOfWins++;
}
void PrintBigWinner( /* in */ int winsForA, /* in */ int winsForB )
{
cout << "Ted the Lion Heart has won " << winsForA << " fight sequences.";
cout << endl;
cout << "Emcee ++ has won " << winsForB << " fight sequences.";
cout << endl;
if( winsForA > winsForB )
{
cout << "Ted the Lion Heart beat up Emcee ++." << endl;
cout << "Ted the Lion Heart is the new world champion!!!" << endl;
}
else if( winsForB > winsForA )
{
cout << "Emcee ++ beat up Ted the Lion Heart." << endl;
cout << "Emcee ++ is the new world champion!!!" << endl;
}
else
{
cout << "You knocked each other out! Johnny Boy wins!";
cout << endl;
}
}
References
Appendix B