Created
March 19, 2015 15:25
-
-
Save arsane/156d546723d31aceecc6 to your computer and use it in GitHub Desktop.
mastermind
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
string colors("BGR"), comb(4, '.'), l(comb), guess; | |
typedef map<int,int> M; | |
struct Color { | |
Color( M& cm, M& gm, int& color ) | |
: cm_(cm), gm_(gm), color_(color=0) { } | |
void operator()( char c ) | |
{ color_ += min( cm_[c], gm_[c] ); } | |
M &cm_, &gm_; | |
int& color_; | |
}; | |
struct Count { | |
Count( int& color, int& exact ) | |
: color_(color), exact_(exact=0) { } | |
char operator()( char c, char g ) | |
{ return ++cm_[c], ++gm_[toupper(g)], | |
exact_ += c == toupper(g), '.'; } | |
~Count() | |
{ for_each( colors.begin(), colors.end(), | |
Color( cm_, gm_, color_ ) ); } | |
M cm_, gm_; | |
int &color_, &exact_; | |
}; | |
char ChoosePeg() | |
{ return colors[rand() % colors.size()]; } | |
int main() { | |
int color, exact = 0; | |
srand( time(0) ), | |
generate( comb.begin(), comb.end(), ChoosePeg ); | |
while( exact < comb.length() ) { | |
cout << "\n\nguess--> ", cin >> guess; | |
transform( comb.begin(), comb.end(), | |
guess.begin(), l.begin(), | |
Count( color, exact ) ); | |
cout << color << ' ' << exact; | |
} | |
cout << " - solved!\n"; | |
} |
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 <iostream> | |
#include <algorithm> | |
#include <map> | |
#include <string> | |
#include <ctime> | |
#include <cstdlib> | |
using namespace std; | |
typedef map<int,int> M; | |
class ChoosePeg | |
{ | |
public: | |
ChoosePeg( const string& colors ) | |
: colors_(colors) { } | |
char operator()() const | |
{ return colors_[rand() % colors_.size()]; } | |
private: | |
const string& colors_; | |
}; | |
class CountPlace | |
{ | |
public: | |
CountPlace( M& cm, M& gm, int& pok ) | |
: cm_(cm), gm_(gm), pok_(pok=0) { } | |
char operator()( char c, char g ) | |
{ | |
return ++cm_[c], | |
++gm_[g], | |
pok_ += (c == g), | |
g; | |
} | |
private: | |
M &cm_, &gm_; | |
int& pok_; | |
}; | |
class CountColor | |
{ | |
public: | |
CountColor( M& cm, M& gm, int& cok ) | |
: cm_(cm), gm_(gm), cok_(cok=0) { } | |
void operator()( char c ) const | |
{ cok_ += min( cm_[c], gm_[c] ); } | |
private: | |
M &cm_, &gm_; | |
int& cok_; | |
}; | |
int main() { | |
const string colors("BGR"); // possible colors | |
string comb(4, '.'), // combination | |
guess; // current guess | |
int cok, pok = 0; // right color & place | |
M cm, gm; // helper structures | |
srand( time(0) ), | |
generate( comb.begin(), comb.end(), | |
ChoosePeg( colors ) ); | |
while( pok < comb.size() ) | |
cout << "\n\nguess--> ", | |
cin >> guess, | |
guess.resize( comb.size(),' '), | |
cm = gm = M(), | |
transform( comb.begin(), comb.end(), | |
guess.begin(), guess.begin(), | |
CountPlace( cm, gm, pok ) ), | |
for_each ( colors.begin(), colors.end(), | |
CountColor( cm, gm, cok ) ), | |
cout << cok << ' '<< pok; | |
cout << " - solved!\n"; | |
} |
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 <iostream> | |
#include <algorithm> | |
#include <numeric> | |
#include <functional> | |
#include <string> | |
#include <ctime> | |
#include <cstdlib> | |
using namespace std; | |
// http://flylib.com/books/en/3.259.1.33/1/ | |
class ColorMatch { | |
public: | |
ColorMatch( int i, const string* s1, const string* s2 ) | |
: cok_(i), s1_(s1), s2_(s2) { } | |
operator int() const { return cok_; } | |
static ColorMatch Count( ColorMatch& cm, char c ) | |
{ | |
return | |
ColorMatch( | |
cm.cok_ + | |
min( count( cm.s1_->begin(), cm.s1_->end(), c ), | |
count( cm.s2_->begin(), cm.s2_->end(), c ) ), | |
cm.s1_, cm.s2_ ); | |
} | |
private: | |
int cok_; | |
const string *s1_, *s2_; | |
}; | |
class Combination | |
{ | |
public: | |
Combination() | |
: comb_(4, '.') | |
{ | |
generate( comb_.begin(), comb_.end(), ChoosePeg ), | |
Prompt(); | |
} | |
bool operator()( string guess ) const // one round | |
{ | |
int cok, pok; // right color & place | |
return | |
guess.resize( comb_.size(),' '), | |
cok = accumulate( colors.begin(), colors.end(), | |
ColorMatch( 0, &comb_, &guess ), | |
ColorMatch::Count ), | |
pok = inner_product( comb_.begin(), comb_.end(), | |
guess.begin(), 0, | |
plus<int>(), | |
equal_to<char>() ), | |
cout << cok <<' '<< pok, | |
(pok == comb_.size()) | |
? (cout << " - solved!\n", true) | |
: (Prompt(), false); | |
} | |
private: | |
string comb_; // actual combination | |
static char ChoosePeg() { return colors[rand() % colors.size()]; } | |
static void Prompt() { cout << "\n\nguess--> "; } | |
static const string colors; // possible colors | |
}; | |
const string Combination::colors = "BGR"; | |
int main() | |
{ | |
srand( time(0) ), | |
find_if( istream_iterator<string>(cin), | |
istream_iterator<string>(), | |
Combination() ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment