2023-08-04 16:28:41 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
2023-08-06 15:02:50 +02:00
|
|
|
#include "myassert.h"
|
2023-08-06 22:06:58 +02:00
|
|
|
#include "game_state.h"
|
|
|
|
#include <vector>
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-05 11:55:46 +02:00
|
|
|
namespace Hanabi {
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, HanabiStateIF const& hanabi_state) {
|
|
|
|
hanabi_state.print(os);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:55:46 +02:00
|
|
|
Card &Card::operator++() {
|
|
|
|
rank++;
|
|
|
|
return *this;
|
2023-08-04 16:28:41 +02:00
|
|
|
}
|
2023-08-05 11:55:46 +02:00
|
|
|
|
|
|
|
const Card Card::operator++(int) {
|
|
|
|
Card ret = *this;
|
|
|
|
rank++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
std::ostream &operator<<(std::ostream &os, const Card &card) {
|
|
|
|
os << suit_initials[card.suit] << 5 - card.rank;
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2023-08-07 10:45:11 +02:00
|
|
|
template<size_t num_suits>
|
|
|
|
std::ostream& operator<<(std::ostream &os, const Stacks<num_suits> &stacks) {
|
2023-08-05 11:55:46 +02:00
|
|
|
for (size_t i = 0; i < stacks.size() - 1; i++) {
|
2023-08-06 10:23:29 +02:00
|
|
|
os << starting_card_rank - stacks[i] << ", ";
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
2023-08-06 10:23:29 +02:00
|
|
|
os << starting_card_rank - stacks.back();
|
2023-08-05 11:55:46 +02:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, typename T>
|
2023-08-07 01:51:24 +02:00
|
|
|
void CardArray<num_suits, T>::fill(T val) {
|
|
|
|
for (size_t suit = 0; suit < num_suits; suit++) {
|
2023-08-06 10:23:29 +02:00
|
|
|
for (rank_t rank = 0; rank < starting_card_rank; rank++) {
|
2023-08-07 01:51:24 +02:00
|
|
|
_array[suit][rank] = val;
|
2023-08-05 12:19:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 01:51:24 +02:00
|
|
|
template<suit_t num_suits, typename T>
|
|
|
|
CardArray<num_suits, T>::CardArray(T default_val) {
|
|
|
|
fill(default_val);
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, typename T>
|
2023-08-06 22:20:20 +02:00
|
|
|
const T& CardArray<num_suits, T>::operator[](const Card &card) const {
|
|
|
|
return _array[card.suit][card.rank];
|
2023-08-05 11:55:46 +02:00
|
|
|
};
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, typename T>
|
2023-08-06 22:20:20 +02:00
|
|
|
T& CardArray<num_suits, T>::operator[](const Card &card) {
|
|
|
|
return _array[card.suit][card.rank];
|
2023-08-05 11:55:46 +02:00
|
|
|
};
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 10:23:29 +02:00
|
|
|
HanabiState<num_suits, num_players, hand_size>::HanabiState(const std::vector<Card> &deck):
|
2023-08-05 12:19:34 +02:00
|
|
|
_turn(0),
|
|
|
|
_num_clues(max_num_clues),
|
2023-08-06 12:23:53 +02:00
|
|
|
_weighted_draw_pile_size(deck.size()),
|
2023-08-05 12:19:34 +02:00
|
|
|
_stacks(),
|
|
|
|
_hands(),
|
2023-08-06 11:54:57 +02:00
|
|
|
_draw_pile(),
|
2023-08-06 15:02:50 +02:00
|
|
|
_endgame_turns_left(no_endgame),
|
2023-08-07 11:48:39 +02:00
|
|
|
_card_positions_draw(),
|
|
|
|
_card_positions_hands(),
|
|
|
|
_num_useful_cards_in_starting_hands(0),
|
2023-08-06 11:54:57 +02:00
|
|
|
_pace(deck.size() - 5 * num_suits - num_players * (hand_size - 1)),
|
2023-08-07 11:48:39 +02:00
|
|
|
_score(0),
|
|
|
|
_enumerated_states(0) {
|
2023-08-05 12:19:34 +02:00
|
|
|
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++) {
|
2023-08-07 10:36:03 +02:00
|
|
|
draw<false>(index);
|
2023-08-05 12:19:34 +02:00
|
|
|
}
|
|
|
|
incr_turn();
|
|
|
|
}
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(_turn == 0);
|
2023-08-05 12:19:34 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 16:44:21 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::clue() {
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(_num_clues > 0);
|
2023-08-05 11:55:46 +02:00
|
|
|
--_num_clues;
|
|
|
|
|
|
|
|
incr_turn();
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 10:23:29 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::incr_turn() {
|
2023-08-05 11:55:46 +02:00
|
|
|
_turn = (_turn + 1) % num_players;
|
2023-08-06 15:02:50 +02:00
|
|
|
if(_endgame_turns_left != no_endgame) {
|
|
|
|
_endgame_turns_left--;
|
2023-08-06 11:54:57 +02:00
|
|
|
}
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 10:23:29 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::decr_turn() {
|
2023-08-05 11:55:46 +02:00
|
|
|
_turn = (_turn + num_players - 1) % num_players;
|
2023-08-06 15:02:50 +02:00
|
|
|
if (_endgame_turns_left != no_endgame) {
|
|
|
|
_endgame_turns_left++;
|
2023-08-06 11:54:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 11:54:57 +02:00
|
|
|
bool HanabiState<num_suits, num_players, hand_size>::is_playable(const Hanabi::Card &card) const {
|
|
|
|
return card.rank == _stacks[card.suit] - 1;
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 22:06:58 +02:00
|
|
|
std::uint64_t HanabiState<num_suits, num_players, hand_size>::enumerated_states() const {
|
|
|
|
return _enumerated_states;
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 11:54:57 +02:00
|
|
|
bool HanabiState<num_suits, num_players, hand_size>::is_trash(const Hanabi::Card &card) const {
|
|
|
|
return card.rank >= _stacks[card.suit];
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-07 00:06:50 +02:00
|
|
|
BacktrackAction HanabiState<num_suits, num_players, hand_size>::play(Hanabi::hand_index_t index) {
|
|
|
|
const Card card = _hands[_turn][index];
|
|
|
|
if (!is_playable(card)) {
|
2023-08-07 10:36:03 +02:00
|
|
|
BacktrackAction ret{card, index, draw<false>(index)};
|
2023-08-07 00:06:50 +02:00
|
|
|
incr_turn();
|
|
|
|
return ret;
|
|
|
|
}
|
2023-08-07 10:36:03 +02:00
|
|
|
return play_and_potentially_update<false>(index);
|
2023-08-07 00:06:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-07 10:36:03 +02:00
|
|
|
template<bool update_card_positions>
|
|
|
|
BacktrackAction HanabiState<num_suits, num_players, hand_size>::play_and_potentially_update(hand_index_t index) {
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(index < _hands[_turn].size());
|
2023-08-05 11:55:46 +02:00
|
|
|
const Card card = _hands[_turn][index];
|
2023-08-07 00:06:50 +02:00
|
|
|
ASSERT(is_playable(card));
|
2023-08-05 11:55:46 +02:00
|
|
|
|
|
|
|
--_stacks[card.suit];
|
2023-08-06 11:54:57 +02:00
|
|
|
_score++;
|
2023-08-05 11:55:46 +02:00
|
|
|
|
2023-08-06 17:34:07 +02:00
|
|
|
if (card.rank == 0 and _num_clues < max_num_clues) {
|
2023-08-05 11:55:46 +02:00
|
|
|
// update clues if we played the last card of a stack
|
|
|
|
_num_clues++;
|
|
|
|
}
|
|
|
|
|
2023-08-07 10:36:03 +02:00
|
|
|
BacktrackAction ret{card, index, draw<update_card_positions>(index)};
|
2023-08-05 11:55:46 +02:00
|
|
|
|
2023-08-06 22:15:09 +02:00
|
|
|
incr_turn();
|
2023-08-05 11:55:46 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 11:54:57 +02:00
|
|
|
BacktrackAction HanabiState<num_suits, num_players, hand_size>::discard(std::uint8_t index) {
|
2023-08-07 10:36:03 +02:00
|
|
|
return discard_and_potentially_update<false>(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
template<bool update_card_positions>
|
|
|
|
BacktrackAction HanabiState<num_suits, num_players, hand_size>::discard_and_potentially_update(hand_index_t index) {
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(index < _hands[_turn].size());
|
|
|
|
ASSERT(_num_clues != max_num_clues);
|
2023-08-05 11:55:46 +02:00
|
|
|
|
2023-08-06 22:15:09 +02:00
|
|
|
const Card discarded = _hands[_turn][index];
|
2023-08-05 11:55:46 +02:00
|
|
|
_num_clues++;
|
2023-08-06 11:54:57 +02:00
|
|
|
_pace--;
|
2023-08-05 11:55:46 +02:00
|
|
|
|
2023-08-07 10:36:03 +02:00
|
|
|
BacktrackAction ret{discarded, index, draw<update_card_positions>(index)};
|
2023-08-05 11:55:46 +02:00
|
|
|
|
|
|
|
incr_turn();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 10:23:29 +02:00
|
|
|
std::uint8_t HanabiState<num_suits, num_players, hand_size>::find_card_in_hand(
|
2023-08-05 13:51:55 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 22:06:58 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::print(std::ostream &os) const {
|
2023-08-07 10:45:11 +02:00
|
|
|
os << "Stacks: " << _stacks << " (score " << +_score << ")";
|
2023-08-06 22:06:58 +02:00
|
|
|
os << ", clues: " << +_num_clues << ", turn: " << +_turn << std::endl;
|
2023-08-05 11:55:46 +02:00
|
|
|
os << "Draw pile: ";
|
2023-08-06 22:06:58 +02:00
|
|
|
for (const auto &[card, mul]: _draw_pile) {
|
2023-08-05 11:55:46 +02:00
|
|
|
os << card;
|
|
|
|
if (mul > 1) {
|
|
|
|
os << " (" << +mul << ")";
|
|
|
|
}
|
2023-08-06 10:23:29 +02:00
|
|
|
os << ", ";
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
2023-08-06 22:06:58 +02:00
|
|
|
os << "(size " << +_weighted_draw_pile_size << ")" << std::endl;
|
2023-08-05 11:55:46 +02:00
|
|
|
os << "Hands: ";
|
2023-08-06 22:06:58 +02:00
|
|
|
for (const auto &hand: _hands) {
|
2023-08-05 11:55:46 +02:00
|
|
|
for (const auto &card: hand) {
|
|
|
|
os << card << ", ";
|
|
|
|
}
|
|
|
|
os << " | ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-07 10:36:03 +02:00
|
|
|
template<bool update_card_positions>
|
2023-08-06 11:54:57 +02:00
|
|
|
std::uint8_t HanabiState<num_suits, num_players, hand_size>::draw(uint8_t index) {
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(index < _hands[_turn].size());
|
2023-08-05 11:55:46 +02:00
|
|
|
|
2023-08-07 11:04:53 +02:00
|
|
|
// update card position of the card we are about to discard
|
|
|
|
if constexpr(update_card_positions) {
|
|
|
|
const Card discarded = _hands[_turn][index];
|
|
|
|
if (!discarded.initial_trash) {
|
2023-08-07 11:23:48 +02:00
|
|
|
if (discarded.local_index_among_good_starting_hand_cards != not_in_starting_hand) {
|
|
|
|
ASSERT(_card_positions_hands[discarded.local_index_among_good_starting_hand_cards] == true);
|
|
|
|
_card_positions_hands[discarded.local_index_among_good_starting_hand_cards] = false;
|
2023-08-07 11:04:53 +02:00
|
|
|
} else {
|
|
|
|
auto replaced_card_it = std::ranges::find(_card_positions_draw[discarded], _turn);
|
|
|
|
ASSERT(replaced_card_it != _card_positions_draw[discarded].end());
|
|
|
|
*replaced_card_it = trash_or_play_stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:55:46 +02:00
|
|
|
// draw a new card if the draw pile is not empty
|
|
|
|
if (!_draw_pile.empty()) {
|
2023-08-06 11:54:57 +02:00
|
|
|
--_weighted_draw_pile_size;
|
|
|
|
|
|
|
|
const CardMultiplicity draw = _draw_pile.front();
|
2023-08-05 11:55:46 +02:00
|
|
|
_draw_pile.pop_front();
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(draw.multiplicity > 0);
|
2023-08-06 11:54:57 +02:00
|
|
|
|
2023-08-05 11:55:46 +02:00
|
|
|
if (draw.multiplicity > 1) {
|
|
|
|
_draw_pile.push_back(draw);
|
2023-08-06 11:54:57 +02:00
|
|
|
_draw_pile.back().multiplicity--;
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
2023-08-06 11:54:57 +02:00
|
|
|
|
2023-08-07 10:36:03 +02:00
|
|
|
if constexpr(update_card_positions) {
|
2023-08-07 11:04:53 +02:00
|
|
|
// update card position of the drawn card
|
2023-08-07 10:36:03 +02:00
|
|
|
if (!draw.card.initial_trash) {
|
|
|
|
auto new_card_it = std::ranges::find(_card_positions_draw[draw.card], draw_pile);
|
|
|
|
ASSERT(new_card_it != _card_positions_draw[draw.card].end());
|
|
|
|
*new_card_it = _turn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 22:20:20 +02:00
|
|
|
_hands[_turn][index] = draw.card;
|
2023-08-06 11:54:57 +02:00
|
|
|
|
|
|
|
if(_draw_pile.empty()) {
|
2023-08-06 15:02:50 +02:00
|
|
|
// Note the +1, since we will immediately decrement this when moving to the next player
|
|
|
|
_endgame_turns_left = num_players + 1;
|
2023-08-06 10:23:29 +02:00
|
|
|
}
|
2023-08-06 11:54:57 +02:00
|
|
|
return draw.multiplicity;
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
2023-08-06 11:54:57 +02:00
|
|
|
return 0;
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 13:53:18 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::revert_draw(std::uint8_t index, Card discarded_card) {
|
2023-08-06 15:02:50 +02:00
|
|
|
if (_endgame_turns_left == num_players + 1 || _endgame_turns_left == no_endgame) {
|
2023-08-06 13:53:18 +02:00
|
|
|
// Put the card that is currently in hand back into the draw pile
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(index < _hands[_turn].size());
|
2023-08-06 13:53:18 +02:00
|
|
|
const Card &drawn = _hands[_turn][index];
|
2023-08-05 11:55:46 +02:00
|
|
|
|
2023-08-06 13:53:18 +02:00
|
|
|
// put discarded_card back into draw pile (at the back)
|
|
|
|
if (!_draw_pile.empty() and _draw_pile.back().card.suit == drawn.suit and
|
|
|
|
_draw_pile.back().card.rank == drawn.rank) {
|
|
|
|
_draw_pile.back().multiplicity++;
|
|
|
|
} else {
|
|
|
|
_draw_pile.push_back({drawn, 1});
|
|
|
|
}
|
2023-08-07 10:36:03 +02:00
|
|
|
|
|
|
|
if (!drawn.initial_trash) {
|
|
|
|
auto drawn_card_it = std::ranges::find(_card_positions_draw[drawn], _turn);
|
|
|
|
ASSERT(drawn_card_it != _card_positions_draw[drawn].end());
|
|
|
|
*drawn_card_it = draw_pile;
|
|
|
|
}
|
|
|
|
|
2023-08-06 13:53:18 +02:00
|
|
|
_weighted_draw_pile_size++;
|
2023-08-06 15:02:50 +02:00
|
|
|
_endgame_turns_left = no_endgame;
|
2023-08-07 11:04:53 +02:00
|
|
|
} else {
|
|
|
|
ASSERT(_hands[_turn][index] == discarded_card);
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
2023-08-07 10:36:03 +02:00
|
|
|
|
|
|
|
if (!discarded_card.initial_trash) {
|
2023-08-07 11:23:48 +02:00
|
|
|
if (discarded_card.local_index_among_good_starting_hand_cards != not_in_starting_hand) {
|
|
|
|
ASSERT(_card_positions_hands[discarded_card.local_index_among_good_starting_hand_cards] == false);
|
|
|
|
_card_positions_hands[discarded_card.local_index_among_good_starting_hand_cards] = true;
|
2023-08-07 10:36:03 +02:00
|
|
|
} else {
|
|
|
|
auto hand_card_it = std::ranges::find(_card_positions_draw[discarded_card], trash_or_play_stack);
|
|
|
|
ASSERT(hand_card_it != _card_positions_draw[discarded_card].end());
|
2023-08-07 11:04:53 +02:00
|
|
|
*hand_card_it = _turn;
|
2023-08-07 10:36:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 13:53:18 +02:00
|
|
|
_hands[_turn][index] = discarded_card;
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 10:23:29 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::normalize_draw_and_positions() {
|
2023-08-07 01:51:24 +02:00
|
|
|
// Note that this function does not have to be particularly performant, we only call it once to initialize.
|
2023-08-06 10:23:29 +02:00
|
|
|
const Card trash = [this]() -> Card {
|
|
|
|
for(suit_t suit = 0; suit < num_suits; suit++) {
|
|
|
|
if(_stacks[suit] < starting_card_rank) {
|
2023-08-07 11:23:48 +02:00
|
|
|
return {suit, starting_card_rank - 1, not_in_starting_hand, true};
|
2023-08-06 10:23:29 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-06 22:20:20 +02:00
|
|
|
return {0,0};
|
2023-08-06 10:23:29 +02:00
|
|
|
}();
|
|
|
|
|
2023-08-06 22:20:20 +02:00
|
|
|
CardArray<num_suits, std::uint8_t> nums_in_draw_pile;
|
2023-08-06 10:23:29 +02:00
|
|
|
for(const auto [card, multiplicity] : _draw_pile) {
|
|
|
|
if (_stacks[card.suit] > card.rank) {
|
|
|
|
nums_in_draw_pile[card] += multiplicity;
|
|
|
|
} else {
|
2023-08-07 01:51:24 +02:00
|
|
|
nums_in_draw_pile[trash] += multiplicity;
|
2023-08-06 10:23:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 01:51:24 +02:00
|
|
|
// Prepare draw pile
|
2023-08-06 10:23:29 +02:00
|
|
|
_draw_pile.clear();
|
|
|
|
for(suit_t suit = 0; suit < num_suits; suit++) {
|
|
|
|
for(rank_t rank = 0; rank < starting_card_rank; rank++) {
|
2023-08-07 11:23:48 +02:00
|
|
|
Card card {suit, rank, not_in_starting_hand, is_trash(card)};
|
2023-08-07 10:36:03 +02:00
|
|
|
_card_positions_draw[card].clear();
|
2023-08-06 10:23:29 +02:00
|
|
|
if (nums_in_draw_pile[card] > 0) {
|
|
|
|
_draw_pile.push_back({card, nums_in_draw_pile[card]});
|
2023-08-07 01:51:24 +02:00
|
|
|
if(!is_trash(card)) {
|
|
|
|
_card_positions_draw[card].resize(nums_in_draw_pile[card], draw_pile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare cards in hands
|
|
|
|
for(player_t player = 0; player < num_players; player++) {
|
|
|
|
for(Card& card : _hands[player]) {
|
2023-08-07 10:36:03 +02:00
|
|
|
card.initial_trash = is_trash(card);
|
2023-08-07 11:23:48 +02:00
|
|
|
// Needed to check for dupes in same hand
|
|
|
|
boost::container::static_vector<Card, hand_size> good_cards_in_hand;
|
2023-08-07 01:51:24 +02:00
|
|
|
if(!is_trash(card)) {
|
2023-08-07 11:23:48 +02:00
|
|
|
if(std::count(good_cards_in_hand.begin(), good_cards_in_hand.end(), card) > 0) {
|
2023-08-07 01:51:24 +02:00
|
|
|
// This card is already in hand, so just replace the second copy by some trash
|
|
|
|
card = trash;
|
|
|
|
} else {
|
2023-08-07 11:48:39 +02:00
|
|
|
card.local_index_among_good_starting_hand_cards = _num_useful_cards_in_starting_hands;
|
|
|
|
_num_useful_cards_in_starting_hands++;
|
2023-08-07 11:23:48 +02:00
|
|
|
|
|
|
|
good_cards_in_hand.push_back(card);
|
2023-08-07 01:51:24 +02:00
|
|
|
}
|
2023-08-06 10:23:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-07 11:23:48 +02:00
|
|
|
_card_positions_hands.reset();
|
2023-08-07 11:48:39 +02:00
|
|
|
for(size_t i = 0; i < _num_useful_cards_in_starting_hands; i++) {
|
|
|
|
_card_positions_hands[i] = true;
|
|
|
|
}
|
2023-08-06 10:23:29 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 17:34:07 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::revert_play(const BacktrackAction& action, bool was_on_8_clues) {
|
|
|
|
ASSERT(!was_on_8_clues or _num_clues == 8);
|
2023-08-05 11:55:46 +02:00
|
|
|
decr_turn();
|
2023-08-06 17:34:07 +02:00
|
|
|
if (action.discarded.rank == 0 and not was_on_8_clues) {
|
2023-08-06 16:44:21 +02:00
|
|
|
_num_clues--;
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
2023-08-06 16:44:21 +02:00
|
|
|
revert_draw(action.index, action.discarded);
|
|
|
|
_stacks[action.discarded.suit]++;
|
|
|
|
_score--;
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 16:44:21 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::revert_discard(const BacktrackAction& action) {
|
|
|
|
decr_turn();
|
|
|
|
ASSERT(_num_clues > 0);
|
|
|
|
_num_clues--;
|
|
|
|
_pace++;
|
|
|
|
revert_draw(action.index, action.discarded);
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 16:44:21 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::revert_clue() {
|
|
|
|
decr_turn();
|
|
|
|
ASSERT(_num_clues < max_num_clues);
|
|
|
|
_num_clues++;
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 11:54:57 +02:00
|
|
|
#define UPDATE_PROBABILITY(new_probability) \
|
|
|
|
best_probability = std::max(best_probability, new_probability); \
|
|
|
|
if (best_probability == 1) { \
|
|
|
|
return best_probability; \
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-06 13:53:18 +02:00
|
|
|
double HanabiState<num_suits, num_players, hand_size>::backtrack(size_t depth) {
|
2023-08-06 15:05:34 +02:00
|
|
|
_enumerated_states++;
|
2023-08-06 11:54:57 +02:00
|
|
|
if (_score == 5 * num_suits) {
|
|
|
|
return 1;
|
|
|
|
}
|
2023-08-06 15:02:50 +02:00
|
|
|
if(_pace < 0 || _endgame_turns_left == 0) {
|
2023-08-06 11:54:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Have some endgame analysis here?
|
|
|
|
|
|
|
|
// First, check if we have any playable cards
|
|
|
|
double best_probability = 0;
|
|
|
|
const std::array<Card, hand_size> hand = _hands[_turn];
|
|
|
|
|
|
|
|
// First, check for playables
|
|
|
|
for(std::uint8_t index = 0; index < hand_size; index++) {
|
|
|
|
if(is_playable(hand[index])) {
|
2023-08-06 12:49:52 +02:00
|
|
|
if (_draw_pile.empty()) {
|
2023-08-06 17:34:07 +02:00
|
|
|
bool on_8_clues = _num_clues == 8;
|
2023-08-07 10:36:03 +02:00
|
|
|
BacktrackAction action = play_and_potentially_update<true>(index);
|
2023-08-06 13:53:18 +02:00
|
|
|
const double probability_for_this_play = backtrack(depth + 1);
|
2023-08-06 17:34:07 +02:00
|
|
|
revert_play(action, on_8_clues);
|
2023-08-06 12:49:52 +02:00
|
|
|
UPDATE_PROBABILITY(probability_for_this_play);
|
|
|
|
} else {
|
|
|
|
double sum_of_probabilities = 0;
|
|
|
|
uint8_t sum_of_mults = 0;
|
|
|
|
for (size_t i = 0; i < _draw_pile.size(); i++) {
|
2023-08-06 17:34:07 +02:00
|
|
|
bool on_8_clues = _num_clues == 8;
|
2023-08-07 10:36:03 +02:00
|
|
|
BacktrackAction action = play_and_potentially_update<true>(index);
|
2023-08-06 13:53:18 +02:00
|
|
|
sum_of_probabilities += backtrack(depth + 1) * action.multiplicity;
|
2023-08-06 12:49:52 +02:00
|
|
|
sum_of_mults += action.multiplicity;
|
2023-08-06 17:34:07 +02:00
|
|
|
revert_play(action, on_8_clues);
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(sum_of_mults <= _weighted_draw_pile_size);
|
2023-08-06 12:49:52 +02:00
|
|
|
}
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(sum_of_mults == _weighted_draw_pile_size);
|
2023-08-06 12:49:52 +02:00
|
|
|
const double probability_for_this_play = sum_of_probabilities / _weighted_draw_pile_size;
|
|
|
|
UPDATE_PROBABILITY(probability_for_this_play);
|
2023-08-06 11:54:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for discards now
|
2023-08-06 17:34:07 +02:00
|
|
|
if(_pace > 0 and _num_clues < max_num_clues) {
|
2023-08-06 11:54:57 +02:00
|
|
|
for(std::uint8_t index = 0; index < hand_size; index++) {
|
|
|
|
if (is_trash(hand[index])) {
|
|
|
|
double sum_of_probabilities = 0;
|
2023-08-06 12:49:52 +02:00
|
|
|
if (_draw_pile.empty()) {
|
2023-08-07 10:36:03 +02:00
|
|
|
BacktrackAction action = discard_and_potentially_update<true>(index);
|
2023-08-06 13:53:18 +02:00
|
|
|
const double probability_for_this_discard = backtrack(depth + 1);
|
2023-08-06 16:44:21 +02:00
|
|
|
revert_discard(action);
|
2023-08-06 12:49:52 +02:00
|
|
|
UPDATE_PROBABILITY(probability_for_this_discard);
|
|
|
|
} else {
|
2023-08-06 13:53:18 +02:00
|
|
|
uint8_t sum_of_mults = 0;
|
2023-08-06 12:49:52 +02:00
|
|
|
for (size_t i = 0; i < _draw_pile.size(); i++) {
|
2023-08-07 10:36:03 +02:00
|
|
|
BacktrackAction action = discard_and_potentially_update<true>(index);
|
2023-08-06 13:53:18 +02:00
|
|
|
sum_of_probabilities += backtrack(depth + 1) * action.multiplicity;
|
|
|
|
sum_of_mults += action.multiplicity;
|
2023-08-06 16:44:21 +02:00
|
|
|
revert_discard(action);
|
2023-08-06 12:49:52 +02:00
|
|
|
}
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(sum_of_mults == _weighted_draw_pile_size);
|
2023-08-06 12:49:52 +02:00
|
|
|
const double probability_discard = sum_of_probabilities / _weighted_draw_pile_size;
|
|
|
|
UPDATE_PROBABILITY(probability_discard);
|
2023-08-06 11:54:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// All discards are equivalent, do not continue searching for different trash
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last option is to stall
|
|
|
|
if(_num_clues > 0) {
|
2023-08-06 16:44:21 +02:00
|
|
|
clue();
|
2023-08-06 13:53:18 +02:00
|
|
|
const double probability_stall = backtrack(depth + 1);
|
2023-08-06 16:44:21 +02:00
|
|
|
revert_clue();
|
2023-08-06 11:54:57 +02:00
|
|
|
UPDATE_PROBABILITY(probability_stall);
|
|
|
|
}
|
|
|
|
|
|
|
|
return best_probability;
|
|
|
|
}
|
|
|
|
|
2023-08-07 11:48:39 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
std::uint64_t HanabiState<num_suits, num_players, hand_size>::unique_id() const {
|
|
|
|
unsigned long id = _card_positions_hands.to_ulong();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:55:46 +02:00
|
|
|
} // namespace Hanabi
|