Last active
August 29, 2015 13:56
-
-
Save remram44/9232222 to your computer and use it in GitHub Desktop.
o/ \o/
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 <cassert> | |
#include <chrono> | |
#include <iostream> | |
#include <random> | |
#include <string> | |
#include <thread> | |
#define count(x) (sizeof(x)/sizeof(*(x))) | |
std::mt19937 rnd(std::random_device{}()); | |
class O { | |
public: | |
enum S { | |
HURRAY, | |
LEFT, | |
RIGHT, | |
OHNO | |
}; | |
private: | |
S s; | |
public: | |
O(S s_) | |
: s(s_) | |
{} | |
operator const char*() | |
{ | |
switch(s) | |
{ | |
case HURRAY: | |
return "\\o/"; | |
case LEFT: | |
return "\\o "; | |
case RIGHT: | |
return " o/"; | |
case OHNO: | |
return " o "; | |
default: | |
assert(false); | |
return ""; | |
} | |
} | |
void evolve(const O &left_, const O &right_) | |
{ | |
S left = left_.s, right = right_.s; | |
if( rnd() % 64 <= 7 ) | |
s = HURRAY; | |
else if(left == HURRAY && right == HURRAY) | |
s = OHNO; | |
else if(left == HURRAY) | |
s = RIGHT; | |
else if(right == HURRAY) | |
s = LEFT; | |
else if(right == OHNO && left == OHNO) | |
s = (rnd() % 2)?RIGHT:LEFT; | |
else if(right == OHNO || left == OHNO) | |
s = HURRAY; | |
else | |
s = OHNO; | |
} | |
}; | |
int main() | |
{ | |
O o[] = {O::OHNO, O::LEFT, O::RIGHT, O::HURRAY, O::OHNO, O::RIGHT, O::LEFT}; | |
for(;;) | |
{ | |
for(size_t i = 0; i < count(o); ++i) | |
o[i].evolve(o[(i+6) % 7], o[i+1]); | |
std::cout << "\r"; | |
for(size_t i = 0; i < count(o); ++i) | |
std::cout << o[i] << " "; | |
std::cout.flush(); | |
std::this_thread::sleep_for(std::chrono::milliseconds{200}); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment