Created
May 18, 2016 13:53
-
-
Save parkpd/d3f9cbba5e26adb30298ad694806b054 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
class Actor { | |
public: | |
Actor(const char* name) : name_(name) {} | |
virtual ~Actor() {} | |
virtual void update() = 0; | |
void swap() { | |
currentSlapped_ = nextSlapped_; | |
nextSlapped_ = false; | |
} | |
void slap() { nextSlapped_ = true; } | |
bool wasSlapped() { return currentSlapped_; } | |
const std::string& name() const { return name_; } | |
private: | |
bool currentSlapped_ = false; | |
bool nextSlapped_ = false; | |
std::string name_; | |
}; | |
class Stage { | |
public: | |
void add(Actor* actor, int index) { | |
actors_[index] = actor; | |
} | |
void update() { | |
std::cout << "Stage updates starts\r\n"; | |
for (int i = 0; i < NUM_ACTORS; ++i) { | |
actors_[i]->update(); | |
} | |
for (int i = 0; i < NUM_ACTORS; ++i) { | |
actors_[i]->swap(); | |
} | |
std::cout << "Stage updates ends\r\n"; | |
} | |
private: | |
static const int NUM_ACTORS = 3; | |
Actor* actors_[NUM_ACTORS]; | |
}; | |
class Comedian : public Actor { | |
public: | |
Comedian(const char* name) : Actor(name) {} | |
void face(Actor* actor) { facing_ = actor; } | |
virtual void update() { | |
if (wasSlapped()) { | |
std::cout << " " << name() << " was slapped, so he slaps " << facing_->name() << std::endl; | |
facing_->slap(); | |
} else { | |
std::cout << " " << name() << " was not slapped, so he does nothing\r\n"; | |
} | |
} | |
private: | |
Actor* facing_; | |
}; | |
int _tmain(int argc, _TCHAR* argv[]) { | |
Stage stage; | |
Comedian* harry = new Comedian("Harry"); | |
Comedian* baldy = new Comedian("Baldy"); | |
Comedian* chump = new Comedian("Chump"); | |
harry->face(baldy); | |
baldy->face(chump); | |
chump->face(harry); | |
stage.add(harry, 2); | |
stage.add(baldy, 1); | |
stage.add(chump, 0); | |
harry->slap(); | |
stage.update(); | |
stage.update(); | |
stage.update(); | |
stage.update(); | |
stage.update(); | |
stage.update(); | |
stage.update(); | |
int temp = 0; | |
std::cin >> temp; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment