1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <stdlib.h>
#include <sstream>
#include <iostream>
#include <map>
#include <string>
using namespace std;
// NOTE: need to add entry/entries in FIVE PLACES (1-5 below)
// engine --------------------------------------------------------------
enum eCHOICE { kROCK=0, kPAPER, kSCISSORS, kLIZARD, kSPOCK, kMAX }; // 1. add entry
static char charChoices[] = { 'r', 'p', 'X', 'l', 'K' }; // 2. add entry
static const char* strChoices[] = { "Rock", "Paper", "Scissors", "Lizard", "Spock" }; // 3. add entry
class Choice
{
public:
Choice( eCHOICE choice ) : m_choice( choice ) { }
Choice( char choice ) {
switch( choice ) {
case 'r': m_choice = kROCK; break; // 4. add entry
case 'p': m_choice = kPAPER; break;
case 'X': m_choice = kSCISSORS; break;
case 'l': m_choice = kLIZARD; break;
case 'K': m_choice = kSPOCK; break;
}
}
static void GetMenu( string& menu ) {
ostringstream oss;
oss << "Choose ";
for ( unsigned i=kROCK; i<kMAX; ++i )
oss << "(" << charChoices[i] << ")" << strChoices[i] << " ";
oss << ": ";
menu = oss.str();
}
Choice& operator=( const Choice& rhs ) {
if ( this==&rhs ) return *this;
m_choice = rhs.m_choice;
return *this;
}
int intval() const {
return static_cast<int>( m_choice );
}
bool operator==( const Choice& rhs ) const { return m_choice==rhs.m_choice; }
friend ostream& operator<<( ostream& os, const Choice& rhs ) {
os << strChoices[ rhs.intval() ];
return os;
}
private:
eCHOICE m_choice;
};
struct Combination {
Combination( const Choice& playerChoice, const Choice& computerChoice ) :
m_player( playerChoice ), m_computer( computerChoice ) { }
Combination( const Combination& rhs ) :
m_player( rhs.m_player ), m_computer( rhs.m_computer ) { }
Combination reverse() const {
return Combination( m_computer, m_player );
}
Combination& operator=( const Combination& rhs ) {
if ( this==&rhs ) return *this;
m_player = rhs.m_player;
m_computer = rhs.m_computer;
return *this;
}
int intval() const {
return m_player.intval() * kMAX + m_computer.intval();
}
bool isTied() const {
return m_player==m_computer;
}
bool operator<( const Combination& rhs ) const {
return intval() < rhs.intval();
}
friend ostream& operator<<( ostream& os, const Combination& rhs ) {
os << "Player=" << rhs.m_player << ", ";
os << "Computer=" << rhs.m_computer;
return os;
}
Choice m_player, m_computer;
};
class Judge {
public:
Judge() {
// 5. add entries in ONE DIRECTION ONLY with Combination( winner, loser ) and proper comments
m_results[ Combination( kROCK, kLIZARD ) ] = "Rock crushes Lizard.";
m_results[ Combination( kROCK, kSCISSORS ) ] = "Rock crushes Scissors.";
m_results[ Combination( kPAPER, kROCK ) ] = "Paper covers Rock.";
m_results[ Combination( kPAPER, kSPOCK ) ] = "Paper disproves Spock.";
m_results[ Combination( kSPOCK, kROCK ) ] = "Spock vaporizes Rock.";
m_results[ Combination( kSPOCK, kSCISSORS ) ] = "Spock smashes Scissors.";
m_results[ Combination( kSCISSORS, kLIZARD ) ] = "Scissors cuts the head off of Lizard.";
m_results[ Combination( kSCISSORS, kPAPER ) ] = "Scissors cuts Paper.";
m_results[ Combination( kLIZARD, kPAPER ) ] = "Lizard eats Paper.";
m_results[ Combination( kLIZARD, kSPOCK ) ] = "Lizard poisons Spock.";
}
void getResultFor( string& result, const Combination& combo )
{
if ( combo.isTied() ) {
result = "Tie game.";
} else {
map< Combination, string >::const_iterator it;
if ( (it=m_results.find( combo ))!=m_results.end() ) {
result = it->second + " You win!";
} else if ( (it=m_results.find( combo.reverse() ))!=m_results.end() ) {
result = it->second + " You lose!";
} else {
cerr << "ERROR IN PROGRAM - could not find " << combo << " or " << combo.reverse() << endl;
}
}
}
private:
map< Combination, string > m_results;
};
// main ----------------------------------------------------------------
int main()
{
string menu;
char userchoice;
Choice::GetMenu( menu );
cout << menu << endl;
cin >> userchoice;
srand((unsigned) time(0));
int computerchoice = (rand() % kMAX);
Choice userChoice( userchoice );
Choice computerChoice( charChoices[ computerchoice ] );
cout << "You chose : " << userChoice << endl;
cout << "The computer chooses: " << computerChoice << endl;
// -------------------------------------------------------------------
Judge judge;
string result;
Combination combo( userChoice, computerChoice );
judge.getResultFor( result, combo );
cout << result << endl;;
return 0;
}
|