#include #include #include Card& Card::operator++() { rank++; return *this; } Card Card::successor() const { return {suit, static_cast(rank + 1)}; } const Card Card::operator++(int) { Card ret = *this; rank++; return ret; } template std::ostream& operator<<(std::ostream& os, const Stacks& stacks) { for (size_t i = 0; i < stacks.size() - 1; i++) { os << +stacks[i] << ", "; } os << +stacks.back(); return os; } template const player_t & CardPositions::operator[](const Card& card) const { return _card_positions[card.suit][card.rank][card.copy]; }; template player_t & CardPositions::operator[](const Card& card) { return _card_positions[card.suit][card.rank][card.copy]; }; template Action HanabiState::clue() { assert(_num_clues > 0); --_num_clues; incr_turn(); return Action {ActionType::clue, {}, {}}; } template void HanabiState::incr_turn() { _turn = (_turn + 1) % num_players; } template void HanabiState::decr_turn() { _turn = (_turn + num_players - 1) % num_players; } template Action HanabiState::play(std::uint8_t index) { assert(index < _hands[_turn].size()); const Card card = _hands[_turn][index]; assert(card.rank == _stacks[card.suit] - 1); --_stacks[card.suit]; Action ret {ActionType::play, _hands[_turn][index], index}; if (card.rank == 0) { // update clues if we played the last card of a stack _num_clues++; } draw(index); incr_turn(); return ret; } template Action HanabiState::discard(std::uint8_t index) { assert(index < _hands[_turn].size()); assert(_num_clues != max_num_clues); _num_clues++; Action ret {ActionType::discard, _hands[_turn][index], index}; draw(index); incr_turn(); return ret; } template std::ostream& operator<<(std::ostream& os, const HanabiState hanabi_state) { os << "Stacks: " << hanabi_state._stacks << std::endl; os << "Draw pile: "; for (const auto &[card, mul] : hanabi_state._draw_pile) { os << card; if (mul > 1) { os << " (" << +mul << ")"; } } os << std::endl; os << "Hands: "; for (const auto& hand: hanabi_state._hands) { for (const auto &card: hand) { os << card << ", "; } os << " | "; } return os; } template void HanabiState::draw(std::uint8_t index) { assert(index < _hands[_turn].size()); _card_positions[_hands[_turn][index]] = trash_or_play_stack; // draw a new card if the draw pile is not empty if (!_draw_pile.empty()) { --_draw_pile_size; CardMultiplicity draw = _draw_pile.front(); _draw_pile.pop_front(); assert(draw.multiplicity > 0); if (draw.multiplicity > 1) { draw.multiplicity--; _draw_pile.push_back(draw); } _hands[_turn][index] = draw.card; _card_positions[draw.card] = _turn; } } template void HanabiState::revert_draw(std::uint8_t index, Card card) { assert(index < _hands[_turn].size()); _card_positions[_hands[_turn][index]] = draw_pile; // draw a new card if the draw pile is not empty if (_draw_pile.back().card == _hands[_turn][index]) { _draw_pile.back().multiplicity++; } else { _draw_pile.push_back({_hands[_turn][index], 1}); } _hands[_turn][index] = card; _card_positions[card] = _turn; _draw_pile_size++; } template void HanabiState::revert(const Action &action) { decr_turn(); switch (action.type) { case ActionType::clue: assert(_num_clues < max_num_clues); _num_clues++; break; case ActionType::discard: assert(_num_clues > 0); _num_clues--; revert_draw(action.index, action.discarded); break; case ActionType::play: if (action.discarded.rank == 0) { _num_clues--; } revert_draw(action.index, action.discarded); _stacks[action.discarded.suit]++; } }