|
阅读:679回复:0
变态代码的意思。
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"; }//大家才才看 |
|
|