Card NoddyAi::play_card() { Cards legal = legal_plays(); assert(!legal.empty()); std::uniform_int_distribution<size_t> card_chooser(0, legal.size()-1); size_t which = card_chooser(random_engine_); Cards::iterator i = legal.begin(); std::advance(i, which); return *i; }
S_C Human::play(Deck & deck, bool is_lead) { char tmp_s; char tmp_v; printf("%s:\n", name_); S_C card; auto cards = legal_plays(deck, is_lead); if(0) { for(auto c : cards) { printf(" %s\n", c->string()); } } assert(!cards.empty()); while(1) { scanf("%s %s", &tmp_s, &tmp_v); Suit s; unsigned char c; try { s = char_to_suit(tmp_s); } catch(...) { continue; } try { c = char_to_card(tmp_v); } catch(...) { continue; } /* if(!tmp->is_valid()) { printf("invalid card\n"); fflush(stdout); continue; } */ try { card = get_card(s, c); } catch(Player::card_not_in_hand & e) { printf("you dont have that card\n"); continue; } auto it = std::find(cards.begin(), cards.end(), card); if(it == cards.end()) { printf("not a legal move\n"); fflush(stdout); continue; } break; } remove_from_hand(card); return card; }