293 lines
No EOL
10 KiB
C++
293 lines
No EOL
10 KiB
C++
#include <cassert>
|
|
#include <algorithm>
|
|
#include <iterator>
|
|
|
|
namespace Hanabi {
|
|
|
|
Card &Card::operator++() {
|
|
rank++;
|
|
return *this;
|
|
}
|
|
|
|
Card Card::successor() const { return {suit, static_cast<rank_t>(rank + 1)}; }
|
|
|
|
const Card Card::operator++(int) {
|
|
Card ret = *this;
|
|
rank++;
|
|
return ret;
|
|
}
|
|
|
|
template<std::size_t num_suits>
|
|
std::ostream &operator<<(std::ostream &os, const Stacks<num_suits> &stacks) {
|
|
for (size_t i = 0; i < stacks.size() - 1; i++) {
|
|
os << starting_card_rank - stacks[i] << ", ";
|
|
}
|
|
os << starting_card_rank - stacks.back();
|
|
return os;
|
|
}
|
|
|
|
template<std::size_t num_suits, typename T, bool respect_card_duplicity>
|
|
CardArray<num_suits, T, respect_card_duplicity>::CardArray(T default_val) {
|
|
for(size_t suit = 0; suit < num_suits; suit++) {
|
|
for (rank_t rank = 0; rank < starting_card_rank; rank++) {
|
|
if constexpr (respect_card_duplicity) {
|
|
std::ranges::fill(_vals.array[suit][rank], default_val);
|
|
} else {
|
|
_vals.array[suit][rank] = default_val;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
template<std::size_t num_suits, typename T, bool respect_card_duplicity>
|
|
const T& CardArray<num_suits, T, respect_card_duplicity>::operator[](const Card &card) const {
|
|
if constexpr (respect_card_duplicity) {
|
|
return _vals.array[card.suit][card.rank][card.copy];
|
|
} else {
|
|
return _vals.array[card.suit][card.rank];
|
|
}
|
|
};
|
|
|
|
template<std::size_t num_suits, typename T, bool respect_card_duplicity>
|
|
T& CardArray<num_suits, T, respect_card_duplicity>::operator[](const Card &card) {
|
|
if constexpr (respect_card_duplicity) {
|
|
return _vals.array[card.suit][card.rank][card.copy];
|
|
} else {
|
|
return _vals.array[card.suit][card.rank];
|
|
}
|
|
};
|
|
|
|
template<size_t num_suits, player_t num_players, size_t hand_size>
|
|
HanabiState<num_suits, num_players, hand_size>::HanabiState(const std::vector<Card> &deck):
|
|
_turn(0),
|
|
_num_clues(max_num_clues),
|
|
_draw_pile_size(deck.size() - num_players * hand_size),
|
|
_stacks(),
|
|
_hands(),
|
|
_card_positions(draw_pile),
|
|
_draw_pile() {
|
|
std::ranges::fill(_stacks, starting_card_rank);
|
|
for(const Card& card: deck) {
|
|
_draw_pile.push_back({card, 1});
|
|
}
|
|
for(player_t player = 0; player < num_players; player++) {
|
|
for(std::uint8_t index = 0; index < hand_size; index++) {
|
|
draw(index);
|
|
}
|
|
incr_turn();
|
|
}
|
|
assert(_turn == 0);
|
|
}
|
|
|
|
template<size_t num_suits, player_t num_players, size_t hand_size>
|
|
BacktrackAction HanabiState<num_suits, num_players, hand_size>::clue() {
|
|
assert(_num_clues > 0);
|
|
--_num_clues;
|
|
|
|
incr_turn();
|
|
|
|
return BacktrackAction{ActionType::clue, {}, {}};
|
|
}
|
|
|
|
template<size_t num_suits, player_t num_players, size_t hand_size>
|
|
void HanabiState<num_suits, num_players, hand_size>::incr_turn() {
|
|
_turn = (_turn + 1) % num_players;
|
|
}
|
|
|
|
template<size_t num_suits, player_t num_players, size_t hand_size>
|
|
void HanabiState<num_suits, num_players, hand_size>::decr_turn() {
|
|
_turn = (_turn + num_players - 1) % num_players;
|
|
}
|
|
|
|
template<std::size_t num_suits, player_t num_players, std::size_t hand_size>
|
|
BacktrackAction HanabiState<num_suits, num_players, hand_size>::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];
|
|
|
|
BacktrackAction 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<std::size_t num_suits, player_t num_players, std::size_t hand_size>
|
|
BacktrackAction HanabiState<num_suits, num_players, hand_size>::discard(
|
|
std::uint8_t index) {
|
|
assert(index < _hands[_turn].size());
|
|
assert(_num_clues != max_num_clues);
|
|
|
|
_num_clues++;
|
|
|
|
BacktrackAction ret{ActionType::discard, _hands[_turn][index], index};
|
|
|
|
draw(index);
|
|
incr_turn();
|
|
|
|
return ret;
|
|
}
|
|
|
|
template<std::size_t num_suits, player_t num_players, std::size_t hand_size>
|
|
std::uint8_t HanabiState<num_suits, num_players, hand_size>::find_card_in_hand(
|
|
const Hanabi::Card &card) const {
|
|
for(std::uint8_t i = 0; i < hand_size; i++) {
|
|
if(_hands[_turn][i].rank == card.rank && _hands[_turn][i].suit == card.suit) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
template<std::size_t num_suits, player_t num_players, std::size_t hand_size>
|
|
std::ostream &operator<<(std::ostream &os, const HanabiState<num_suits, num_players, hand_size> 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 << ", ";
|
|
}
|
|
os << std::endl;
|
|
os << "Hands: ";
|
|
for (const auto &hand: hanabi_state._hands) {
|
|
for (const auto &card: hand) {
|
|
os << card << ", ";
|
|
}
|
|
os << " | ";
|
|
}
|
|
return os;
|
|
}
|
|
|
|
template<std::size_t num_suits, player_t num_players, std::size_t hand_size>
|
|
void HanabiState<num_suits, num_players, hand_size>::draw(std::uint8_t index) {
|
|
assert(index < _hands[_turn].size());
|
|
|
|
const Card& discarded = _hands[_turn][index];
|
|
if (_stacks[discarded.suit] > discarded.rank) {
|
|
_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);
|
|
}
|
|
draw.card.copy = draw.multiplicity - 1;
|
|
_hands[_turn][index] = draw.card;
|
|
if (_stacks[draw.card.suit] > draw.card.rank) {
|
|
_card_positions[draw.card] = _turn;
|
|
}
|
|
}
|
|
}
|
|
|
|
template<std::size_t num_suits, player_t num_players, std::size_t hand_size>
|
|
void HanabiState<num_suits, num_players, hand_size>::revert_draw(std::uint8_t index, Card card) {
|
|
assert(index < _hands[_turn].size());
|
|
const Card& discarded = _hands[_turn][index];
|
|
if (_stacks[discarded.suit] > discarded.rank) {
|
|
_card_positions[discarded] = draw_pile;
|
|
}
|
|
|
|
// put card back into draw pile (at the back)
|
|
if (!_draw_pile.empty() and _draw_pile.back().card == _hands[_turn][index]) {
|
|
_draw_pile.back().multiplicity++;
|
|
} else {
|
|
_draw_pile.push_back({_hands[_turn][index], 1});
|
|
}
|
|
|
|
_hands[_turn][index] = card;
|
|
if (_stacks[card.suit] > card.rank) {
|
|
_card_positions[card] = _turn;
|
|
}
|
|
_draw_pile_size++;
|
|
}
|
|
|
|
template<std::size_t num_suits, player_t num_players, std::size_t hand_size>
|
|
void HanabiState<num_suits, num_players, hand_size>::normalize_draw_and_positions() {
|
|
const Card trash = [this]() -> Card {
|
|
for(suit_t suit = 0; suit < num_suits; suit++) {
|
|
if(_stacks[suit] < starting_card_rank) {
|
|
return {suit, starting_card_rank - 1, 0};
|
|
}
|
|
}
|
|
return {0,0,0};
|
|
}();
|
|
|
|
CardArray<num_suits, std::uint8_t, false> nums_in_draw_pile;
|
|
std::uint8_t num_trash_in_draw_pile = 0;
|
|
for(const auto [card, multiplicity] : _draw_pile) {
|
|
if (_stacks[card.suit] > card.rank) {
|
|
nums_in_draw_pile[card] += multiplicity;
|
|
} else {
|
|
num_trash_in_draw_pile++;
|
|
}
|
|
}
|
|
|
|
_draw_pile.clear();
|
|
for(suit_t suit = 0; suit < num_suits; suit++) {
|
|
for(rank_t rank = 0; rank < starting_card_rank; rank++) {
|
|
Card card {suit, rank, 0};
|
|
if (nums_in_draw_pile[card] > 0) {
|
|
_draw_pile.push_back({card, nums_in_draw_pile[card]});
|
|
for (std::uint8_t copy = 0; copy < nums_in_draw_pile[card]; copy++) {
|
|
card.copy = copy;
|
|
_card_positions[card] = draw_pile;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
_draw_pile.push_back({trash, num_trash_in_draw_pile});
|
|
|
|
for(player_t player = 0; player < num_players; player++) {
|
|
for(Card& card : _hands[player]) {
|
|
if (_stacks[card.suit] > card.rank) {
|
|
card.copy = nums_in_draw_pile[card];
|
|
nums_in_draw_pile[card]++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
template<std::size_t num_suits, player_t num_players, std::size_t hand_size>
|
|
void HanabiState<num_suits, num_players, hand_size>::revert(
|
|
const BacktrackAction &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]++;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
} // namespace Hanabi
|