2023-08-04 16:28:41 +02:00
|
|
|
#include <algorithm>
|
2023-08-12 19:43:22 +02:00
|
|
|
|
2023-08-06 15:02:50 +02:00
|
|
|
#include "myassert.h"
|
2023-08-06 22:06:58 +02:00
|
|
|
#include "game_state.h"
|
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-07 12:48:25 +02:00
|
|
|
std::ostream &operator<<(std::ostream &os, HanabiStateIF const &hanabi_state) {
|
2023-08-06 22:06:58 +02:00
|
|
|
hanabi_state.print(os);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2023-09-26 23:20:49 +02:00
|
|
|
std::string to_string(const Hanabi::Card &card) {
|
|
|
|
if (card == Hanabi::Cards::trash) {
|
|
|
|
return "kt";
|
|
|
|
} else {
|
|
|
|
return Hanabi::suit_initials[card.suit] + std::to_string(5 - card.rank);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:54:11 +02:00
|
|
|
std::ostream &operator<<(std::ostream &os, Action const& action) {
|
|
|
|
switch(action.type) {
|
|
|
|
case ActionType::play:
|
2023-09-26 23:20:49 +02:00
|
|
|
os << "play " + to_string(action.card);
|
2023-08-11 16:54:11 +02:00
|
|
|
break;
|
|
|
|
case ActionType::discard:
|
|
|
|
os << "discard";
|
|
|
|
break;
|
|
|
|
case ActionType::clue:
|
|
|
|
os << "clue";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2023-08-12 08:50:28 +02:00
|
|
|
bool Card::operator==(const Card &other) const {
|
|
|
|
return suit == other.suit and rank == other.rank;
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
std::ostream &operator<<(std::ostream &os, const Card &card) {
|
2023-09-26 23:20:49 +02:00
|
|
|
os << to_string(card);
|
2023-08-06 23:12:44 +02:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2023-08-07 10:45:11 +02:00
|
|
|
template<size_t num_suits>
|
2023-08-07 12:48:25 +02:00
|
|
|
std::ostream &operator<<(std::ostream &os, const Stacks<num_suits> &stacks) {
|
2023-08-12 11:09:06 +02:00
|
|
|
for (size_t i = 0; i < stacks.size(); i++) {
|
|
|
|
os << suit_initials[i] << starting_card_rank - stacks[i];
|
2023-08-12 10:19:04 +02:00
|
|
|
if(i < stacks.size() - 1) {
|
|
|
|
os << ", ";
|
|
|
|
}
|
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-07 12:48:25 +02:00
|
|
|
const T &CardArray<num_suits, T>::operator[](const Card &card) const {
|
2023-08-06 22:20:20 +02:00
|
|
|
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-07 12:48:25 +02:00
|
|
|
T &CardArray<num_suits, T>::operator[](const Card &card) {
|
2023-08-06 22:20:20 +02:00
|
|
|
return _array[card.suit][card.rank];
|
2023-08-05 11:55:46 +02:00
|
|
|
};
|
|
|
|
|
2023-08-11 11:43:05 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
HanabiState<num_suits, num_players, hand_size>::BacktrackAction::BacktrackAction(
|
2023-08-10 12:44:09 +02:00
|
|
|
Hanabi::ActionType action_type, Hanabi::Card discarded_or_played, Hanabi::hand_index_t index,
|
|
|
|
bool was_on_8_clues
|
2023-08-10 12:06:13 +02:00
|
|
|
):
|
|
|
|
action_type(action_type),
|
|
|
|
discarded(discarded_or_played),
|
2023-08-10 12:44:09 +02:00
|
|
|
index(index),
|
|
|
|
was_on_8_clues(was_on_8_clues) {
|
2023-08-10 12:06:13 +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-07 12:48:25 +02:00
|
|
|
_turn(0),
|
|
|
|
_num_clues(max_num_clues),
|
|
|
|
_weighted_draw_pile_size(deck.size()),
|
|
|
|
_stacks(),
|
|
|
|
_hands(),
|
|
|
|
_draw_pile(),
|
|
|
|
_endgame_turns_left(no_endgame),
|
|
|
|
_pace(deck.size() - 5 * num_suits - num_players * (hand_size - 1)),
|
|
|
|
_score(0),
|
2023-08-11 13:47:57 +02:00
|
|
|
_actions_log(),
|
|
|
|
_relative_representation(),
|
|
|
|
_position_tablebase(),
|
2023-08-07 12:48:25 +02:00
|
|
|
_enumerated_states(0) {
|
2023-08-05 12:19:34 +02:00
|
|
|
std::ranges::fill(_stacks, starting_card_rank);
|
2023-08-07 12:48:25 +02:00
|
|
|
for (const Card &card: deck) {
|
2023-08-05 12:19:34 +02:00
|
|
|
_draw_pile.push_back({card, 1});
|
|
|
|
}
|
2023-08-07 12:48:25 +02:00
|
|
|
for (player_t player = 0; player < num_players; player++) {
|
|
|
|
for (std::uint8_t index = 0; index < hand_size; index++) {
|
2023-08-11 14:24:17 +02:00
|
|
|
draw(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-12 08:50:28 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::give_clue() {
|
2023-08-06 15:02:50 +02:00
|
|
|
ASSERT(_num_clues > 0);
|
2023-08-05 11:55:46 +02:00
|
|
|
--_num_clues;
|
|
|
|
|
2023-08-12 08:50:28 +02:00
|
|
|
_actions_log.emplace(ActionType::clue, Cards::unknown, 0);
|
2023-08-05 11:55:46 +02:00
|
|
|
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-07 12:48:25 +02:00
|
|
|
if (_endgame_turns_left != no_endgame) {
|
2023-08-06 15:02:50 +02:00
|
|
|
_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-10 12:06:13 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::play(Hanabi::hand_index_t index) {
|
2023-08-11 14:24:17 +02:00
|
|
|
play_and_potentially_update(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-10 12:06:13 +02:00
|
|
|
unsigned long 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-10 12:06:13 +02:00
|
|
|
const Card played_card = _hands[_turn][index];
|
2023-08-11 12:12:09 +02:00
|
|
|
if (!is_playable(played_card)) {
|
2023-08-11 14:24:17 +02:00
|
|
|
const unsigned long multiplicity = draw(index);
|
2023-08-11 12:12:09 +02:00
|
|
|
incr_turn();
|
|
|
|
return multiplicity;
|
|
|
|
}
|
2023-08-10 12:06:13 +02:00
|
|
|
ASSERT(is_playable(played_card));
|
2023-08-05 11:55:46 +02:00
|
|
|
|
2023-08-10 12:44:09 +02:00
|
|
|
_actions_log.emplace(ActionType::play, played_card, index, _num_clues == 8);
|
|
|
|
|
2023-08-10 12:06:13 +02:00
|
|
|
--_stacks[played_card.suit];
|
2023-08-06 11:54:57 +02:00
|
|
|
_score++;
|
2023-08-05 11:55:46 +02:00
|
|
|
|
2023-08-10 12:06:13 +02:00
|
|
|
if (played_card.rank == 0 and _num_clues < max_num_clues) {
|
|
|
|
// update clues if we played the last played_card of a stack
|
2023-08-05 11:55:46 +02:00
|
|
|
_num_clues++;
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:24:17 +02:00
|
|
|
const unsigned long multiplicity = draw(index);
|
2023-08-05 11:55:46 +02:00
|
|
|
|
2023-08-06 22:15:09 +02:00
|
|
|
incr_turn();
|
2023-08-10 12:06:13 +02:00
|
|
|
return multiplicity;
|
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-11 14:24:17 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::discard(hand_index_t index) {
|
|
|
|
discard_and_potentially_update(index);
|
2023-08-07 10:36:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-10 12:06:13 +02:00
|
|
|
unsigned long 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-10 12:06:13 +02:00
|
|
|
const Card discarded_card = _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-11 14:24:17 +02:00
|
|
|
unsigned long multiplicity = draw(index);
|
2023-08-10 12:06:13 +02:00
|
|
|
_actions_log.emplace(ActionType::discard, discarded_card, index);
|
2023-08-05 11:55:46 +02:00
|
|
|
|
|
|
|
incr_turn();
|
2023-08-10 12:06:13 +02:00
|
|
|
return multiplicity;
|
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
|
|
|
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 {
|
2023-08-07 12:48:25 +02:00
|
|
|
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-05 13:51:55 +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 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-12 11:09:06 +02:00
|
|
|
os << ", clues: " << +_num_clues << ", turn: " << +_turn;
|
|
|
|
if (_endgame_turns_left != no_endgame) {
|
|
|
|
os << ", " << +_endgame_turns_left << " turns left";
|
|
|
|
}
|
|
|
|
os << std::endl;
|
2023-08-05 11:55:46 +02:00
|
|
|
os << "Draw pile: ";
|
2023-08-12 11:09:06 +02:00
|
|
|
unsigned num_trash = 0;
|
2023-08-06 22:06:58 +02:00
|
|
|
for (const auto &[card, mul]: _draw_pile) {
|
2023-08-12 11:09:06 +02:00
|
|
|
if (is_trash(card)) {
|
|
|
|
num_trash += mul;
|
|
|
|
continue;
|
|
|
|
}
|
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-12 11:09:06 +02:00
|
|
|
if (num_trash > 0) {
|
|
|
|
os << Cards::trash << " (" << num_trash << ") ";
|
|
|
|
}
|
|
|
|
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-12 11:09:06 +02:00
|
|
|
os << "[";
|
|
|
|
for(hand_index_t index = 0; index < hand.size(); index++) {
|
|
|
|
os << hand[index];
|
|
|
|
if (index < hand.size() - 1) {
|
|
|
|
os << " ";
|
|
|
|
}
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
2023-08-12 11:09:06 +02:00
|
|
|
os << "] ";
|
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-12 00:04:02 +02:00
|
|
|
unsigned 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
|
2023-08-11 14:24:17 +02:00
|
|
|
if (_relative_representation.initialized) {
|
2023-08-07 11:04:53 +02:00
|
|
|
const Card discarded = _hands[_turn][index];
|
|
|
|
if (!discarded.initial_trash) {
|
2023-08-07 12:04:56 +02:00
|
|
|
if (discarded.in_starting_hand) {
|
2023-08-11 13:47:57 +02:00
|
|
|
ASSERT(_relative_representation.card_positions_hands[discarded.local_index] == true);
|
|
|
|
_relative_representation.card_positions_hands[discarded.local_index] = false;
|
2023-08-07 11:04:53 +02:00
|
|
|
} else {
|
2023-08-11 13:47:57 +02:00
|
|
|
auto replaced_card_it = std::ranges::find(_relative_representation.card_positions_draw[discarded.local_index], _turn);
|
|
|
|
ASSERT(replaced_card_it != _relative_representation.card_positions_draw[discarded.local_index].end());
|
2023-08-07 11:04:53 +02:00
|
|
|
*replaced_card_it = trash_or_play_stack;
|
2023-08-12 14:00:54 +02:00
|
|
|
std::ranges::sort(_relative_representation.card_positions_draw[discarded.local_index]);
|
2023-08-07 11:04:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11 14:24:17 +02:00
|
|
|
if (_relative_representation.initialized) {
|
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) {
|
2023-08-07 12:04:56 +02:00
|
|
|
ASSERT(draw.card.in_starting_hand == false);
|
2023-08-11 13:47:57 +02:00
|
|
|
auto new_card_it = std::ranges::find(_relative_representation.card_positions_draw[draw.card.local_index], draw_pile);
|
|
|
|
ASSERT(new_card_it != _relative_representation.card_positions_draw[draw.card.local_index].end());
|
2023-08-07 10:36:03 +02:00
|
|
|
*new_card_it = _turn;
|
2023-08-12 14:00:54 +02:00
|
|
|
std::ranges::sort(_relative_representation.card_positions_draw[draw.card.local_index]);
|
2023-08-07 10:36:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 22:20:20 +02:00
|
|
|
_hands[_turn][index] = draw.card;
|
2023-08-06 11:54:57 +02:00
|
|
|
|
2023-08-07 12:48:25 +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-10 18:23:33 +02:00
|
|
|
return 1;
|
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
|
|
|
|
2023-08-11 14:39:55 +02:00
|
|
|
if (_relative_representation.initialized && !drawn.initial_trash) {
|
2023-08-07 12:04:56 +02:00
|
|
|
ASSERT(drawn.in_starting_hand == false);
|
2023-08-11 13:47:57 +02:00
|
|
|
auto drawn_card_it = std::ranges::find(_relative_representation.card_positions_draw[drawn.local_index], _turn);
|
|
|
|
ASSERT(drawn_card_it != _relative_representation.card_positions_draw[drawn.local_index].end());
|
2023-08-07 10:36:03 +02:00
|
|
|
*drawn_card_it = draw_pile;
|
2023-08-12 14:00:54 +02:00
|
|
|
std::ranges::sort(_relative_representation.card_positions_draw[drawn.local_index]);
|
2023-08-07 10:36:03 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-08-11 14:39:55 +02:00
|
|
|
if (_relative_representation.initialized && !discarded_card.initial_trash) {
|
2023-08-07 12:04:56 +02:00
|
|
|
if (discarded_card.in_starting_hand) {
|
2023-08-11 13:47:57 +02:00
|
|
|
ASSERT(_relative_representation.card_positions_hands[discarded_card.local_index] == false);
|
|
|
|
_relative_representation.card_positions_hands[discarded_card.local_index] = true;
|
2023-08-07 10:36:03 +02:00
|
|
|
} else {
|
2023-08-11 13:47:57 +02:00
|
|
|
auto hand_card_it = std::ranges::find(_relative_representation.card_positions_draw[discarded_card.local_index],
|
2023-08-07 12:48:25 +02:00
|
|
|
trash_or_play_stack);
|
2023-08-11 13:47:57 +02:00
|
|
|
ASSERT(hand_card_it != _relative_representation.card_positions_draw[discarded_card.local_index].end());
|
2023-08-07 11:04:53 +02:00
|
|
|
*hand_card_it = _turn;
|
2023-08-12 14:00:54 +02:00
|
|
|
std::ranges::sort(_relative_representation.card_positions_draw[discarded_card.local_index]);
|
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-11 14:39:55 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::init_backtracking_information() {
|
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 {
|
2023-08-07 12:48:25 +02:00
|
|
|
for (suit_t suit = 0; suit < num_suits; suit++) {
|
|
|
|
if (_stacks[suit] < starting_card_rank) {
|
2023-08-07 12:04:56 +02:00
|
|
|
return {suit, starting_card_rank - 1, 0, false, true};
|
2023-08-06 10:23:29 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 12:48:25 +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-07 12:48:25 +02:00
|
|
|
for (const auto [card, multiplicity]: _draw_pile) {
|
2023-08-06 10:23:29 +02:00
|
|
|
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();
|
2023-08-07 12:48:25 +02:00
|
|
|
for (suit_t suit = 0; suit < num_suits; suit++) {
|
|
|
|
for (rank_t rank = 0; rank < starting_card_rank; rank++) {
|
2023-08-11 13:47:57 +02:00
|
|
|
Card card{suit, rank, static_cast<uint8_t>(_relative_representation.card_positions_draw.size()), false, is_trash(card)};
|
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 12:48:25 +02:00
|
|
|
if (!is_trash(card)) {
|
2023-08-11 16:54:11 +02:00
|
|
|
_relative_representation.card_positions_draw.push_back({});
|
|
|
|
_relative_representation.card_positions_draw.back().resize(nums_in_draw_pile[card], draw_pile);
|
2023-08-11 13:47:57 +02:00
|
|
|
_relative_representation.good_cards_draw.push_back(card);
|
2023-08-07 01:51:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 13:47:57 +02:00
|
|
|
_relative_representation.initial_draw_pile_size = _weighted_draw_pile_size;
|
2023-08-07 01:51:24 +02:00
|
|
|
|
|
|
|
// Prepare cards in hands
|
2023-08-07 12:48:25 +02:00
|
|
|
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 12:04:56 +02:00
|
|
|
card.in_starting_hand = true;
|
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 12:48:25 +02:00
|
|
|
if (!is_trash(card)) {
|
|
|
|
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-11 13:47:57 +02:00
|
|
|
card.local_index = _relative_representation.num_useful_cards_in_starting_hands;
|
|
|
|
_relative_representation.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-11 13:47:57 +02:00
|
|
|
_relative_representation.card_positions_hands.reset();
|
|
|
|
for (size_t i = 0; i < _relative_representation.num_useful_cards_in_starting_hands; i++) {
|
|
|
|
_relative_representation.card_positions_hands[i] = true;
|
2023-08-07 11:48:39 +02:00
|
|
|
}
|
2023-08-11 14:24:17 +02:00
|
|
|
_relative_representation.initialized = 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-07 12:48:25 +02:00
|
|
|
void
|
2023-08-10 12:44:09 +02:00
|
|
|
HanabiState<num_suits, num_players, hand_size>::revert_play() {
|
2023-08-10 12:06:13 +02:00
|
|
|
const BacktrackAction last_action = _actions_log.top();
|
|
|
|
_actions_log.pop();
|
|
|
|
ASSERT(last_action.action_type == ActionType::play);
|
2023-08-10 12:44:09 +02:00
|
|
|
ASSERT(!last_action.was_on_8_clues or _num_clues == 8);
|
2023-08-10 12:06:13 +02:00
|
|
|
|
2023-08-05 11:55:46 +02:00
|
|
|
decr_turn();
|
2023-08-10 12:44:09 +02:00
|
|
|
if (last_action.discarded.rank == 0 and not last_action.was_on_8_clues) {
|
2023-08-06 16:44:21 +02:00
|
|
|
_num_clues--;
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
2023-08-10 12:06:13 +02:00
|
|
|
revert_draw(last_action.index, last_action.discarded);
|
2023-08-11 12:12:09 +02:00
|
|
|
if(_stacks[last_action.discarded.suit] == last_action.discarded.rank) {
|
|
|
|
_stacks[last_action.discarded.suit]++;
|
|
|
|
}
|
2023-08-06 16:44:21 +02:00
|
|
|
_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-10 12:06:13 +02:00
|
|
|
void HanabiState<num_suits, num_players, hand_size>::revert_discard() {
|
|
|
|
const BacktrackAction last_action = _actions_log.top();
|
|
|
|
_actions_log.pop();
|
|
|
|
|
|
|
|
ASSERT(last_action.action_type == ActionType::discard);
|
|
|
|
|
2023-08-06 16:44:21 +02:00
|
|
|
decr_turn();
|
|
|
|
ASSERT(_num_clues > 0);
|
2023-08-10 12:06:13 +02:00
|
|
|
|
2023-08-06 16:44:21 +02:00
|
|
|
_num_clues--;
|
|
|
|
_pace++;
|
2023-08-10 12:06:13 +02:00
|
|
|
|
|
|
|
revert_draw(last_action.index, last_action.discarded);
|
2023-08-06 16:44:21 +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>::revert_clue() {
|
2023-08-10 12:06:13 +02:00
|
|
|
const BacktrackAction last_action = _actions_log.top();
|
|
|
|
_actions_log.pop();
|
|
|
|
|
|
|
|
ASSERT(last_action.action_type == ActionType::clue);
|
|
|
|
|
2023-08-06 16:44:21 +02:00
|
|
|
decr_turn();
|
|
|
|
ASSERT(_num_clues < max_num_clues);
|
2023-08-10 12:06:13 +02:00
|
|
|
|
2023-08-06 16:44:21 +02:00
|
|
|
_num_clues++;
|
2023-08-05 11:55:46 +02:00
|
|
|
}
|
|
|
|
|
2023-08-11 14:24:17 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
void HanabiState<num_suits, num_players, hand_size>::revert() {
|
|
|
|
switch(_actions_log.top().action_type) {
|
|
|
|
case ActionType::clue:
|
|
|
|
revert_clue();
|
|
|
|
break;
|
|
|
|
case ActionType::discard:
|
|
|
|
revert_discard();
|
|
|
|
break;
|
|
|
|
case ActionType::play:
|
|
|
|
revert_play();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 08:50:28 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
player_t HanabiState<num_suits, num_players, hand_size>::turn() const {
|
|
|
|
return _turn;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
clue_t HanabiState<num_suits, num_players, hand_size>::num_clues() const {
|
|
|
|
return _num_clues;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
std::vector<std::vector<Card>> HanabiState<num_suits, num_players, hand_size>::hands() const {
|
|
|
|
std::vector<std::vector<Card>> hands;
|
|
|
|
for(player_t player = 0; player < num_players; player++) {
|
|
|
|
hands.push_back({});
|
|
|
|
for(const Card& card: _hands[player]) {
|
|
|
|
hands.back().push_back(card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hands;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
std::vector<Card> HanabiState<num_suits, num_players, hand_size>::cur_hand() const {
|
|
|
|
std::vector<Card> hand;
|
|
|
|
for(const Card& card: _hands[_turn]) {
|
|
|
|
hand.push_back(card);
|
|
|
|
}
|
|
|
|
return hand;
|
|
|
|
}
|
|
|
|
|
2023-08-11 18:28:12 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-12 00:04:02 +02:00
|
|
|
std::vector<std::pair<CardMultiplicity, std::optional<probability_t>>> HanabiState<num_suits, num_players, hand_size>::possible_next_states(hand_index_t index, bool play) {
|
|
|
|
std::vector<std::pair<CardMultiplicity, std::optional<probability_t>>> next_states;
|
|
|
|
do_for_each_potential_draw(index, play, [this, &next_states, &index](unsigned multiplicity){
|
2023-08-11 18:28:12 +02:00
|
|
|
auto prob = lookup();
|
|
|
|
|
|
|
|
// bit hacky to get drawn card here
|
|
|
|
decr_turn();
|
2023-08-12 00:04:02 +02:00
|
|
|
const CardMultiplicity drawn_card = {_hands[_turn][index], multiplicity};
|
2023-08-11 18:28:12 +02:00
|
|
|
incr_turn();
|
|
|
|
|
2023-08-12 00:04:02 +02:00
|
|
|
next_states.emplace_back(drawn_card, prob);
|
2023-08-11 18:28:12 +02:00
|
|
|
});
|
|
|
|
return next_states;
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:41:03 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
std::vector<std::pair<Action, std::optional<probability_t>>> HanabiState<num_suits, num_players, hand_size>::get_reasonable_actions() {
|
|
|
|
std::vector<std::pair<Action, std::optional<probability_t>>> reasonable_actions {};
|
|
|
|
|
|
|
|
if(_score == 5 * num_suits or _pace < 0 or _endgame_turns_left == 0) {
|
|
|
|
return reasonable_actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::array<Card, hand_size>& hand = _hands[_turn];
|
|
|
|
// First, check for playable cards
|
|
|
|
for(std::uint8_t index = 0; index < hand_size; index++) {
|
|
|
|
if(is_playable(hand[index])) {
|
|
|
|
const Action action = {ActionType::play, hand[index]};
|
|
|
|
bool known = true;
|
|
|
|
probability_t sum_of_probabilities = 0;
|
|
|
|
|
2023-08-11 18:28:12 +02:00
|
|
|
do_for_each_potential_draw(index, true, [this, &sum_of_probabilities, &known](const unsigned long multiplicity){
|
2023-08-11 15:41:03 +02:00
|
|
|
const std::optional<probability_t> prob = lookup();
|
|
|
|
if (prob.has_value()) {
|
|
|
|
sum_of_probabilities += prob.value() * multiplicity;
|
|
|
|
} else {
|
|
|
|
known = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (known) {
|
|
|
|
const unsigned long total_weight = std::max(static_cast<unsigned long>(_weighted_draw_pile_size), 1ul);
|
|
|
|
const probability_t probability_play = sum_of_probabilities / total_weight;
|
|
|
|
reasonable_actions.emplace_back(action, probability_play);
|
|
|
|
} else {
|
|
|
|
reasonable_actions.emplace_back(action, std::nullopt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_pace > 0 and _num_clues < max_num_clues) {
|
|
|
|
for(std::uint8_t index = 0; index < hand_size; index++) {
|
|
|
|
if (is_trash(hand[index])) {
|
|
|
|
const Action action = {ActionType::discard, hand[index]};
|
|
|
|
bool known = true;
|
|
|
|
probability_t sum_of_probabilities = 0;
|
|
|
|
|
2023-08-11 18:28:12 +02:00
|
|
|
do_for_each_potential_draw(index, false, [this, &sum_of_probabilities, &known](const unsigned long multiplicity){
|
2023-08-11 15:41:03 +02:00
|
|
|
const std::optional<probability_t> prob = lookup();
|
|
|
|
if (prob.has_value()) {
|
|
|
|
sum_of_probabilities += prob.value() * multiplicity;
|
|
|
|
} else {
|
|
|
|
known = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (known) {
|
|
|
|
const unsigned long total_weight = std::max(static_cast<unsigned long>(_weighted_draw_pile_size), 1ul);
|
|
|
|
const probability_t probability_discard = sum_of_probabilities / total_weight;
|
|
|
|
reasonable_actions.emplace_back(action, probability_discard);
|
|
|
|
} else {
|
|
|
|
reasonable_actions.emplace_back(action, std::nullopt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// All discards are equivalent, do not continue searching for different trash
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_num_clues > 0) {
|
2023-08-12 08:50:28 +02:00
|
|
|
give_clue();
|
2023-08-11 15:41:03 +02:00
|
|
|
const std::optional<probability_t> prob = lookup();
|
2023-08-12 08:50:28 +02:00
|
|
|
const Action action = {ActionType::clue, Cards::unknown};
|
2023-08-11 15:41:03 +02:00
|
|
|
reasonable_actions.emplace_back(action, prob);
|
|
|
|
revert_clue();
|
|
|
|
}
|
2023-08-11 16:54:11 +02:00
|
|
|
return reasonable_actions;
|
2023-08-11 15:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
std::optional<probability_t> HanabiState<num_suits, num_players, hand_size>::lookup() const {
|
2023-08-12 10:31:32 +02:00
|
|
|
if (_score == 5 * num_suits) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (_pace < 0 or _endgame_turns_left == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-11 15:41:03 +02:00
|
|
|
const auto id = unique_id();
|
|
|
|
if(_position_tablebase.contains(id)) {
|
2023-08-11 16:54:11 +02:00
|
|
|
return _position_tablebase.at(id);
|
2023-08-11 15:41:03 +02:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
2023-08-06 11:54:57 +02:00
|
|
|
|
2023-08-11 18:28:12 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
void HanabiState<num_suits, num_players, hand_size>::rotate_next_draw(const Card& card) {
|
|
|
|
auto card_it = std::find_if(_draw_pile.begin(), _draw_pile.end(), [&card](const CardMultiplicity& card_multiplicity){
|
|
|
|
return card_multiplicity.card.rank == card.rank and card_multiplicity.card.suit == card.suit;
|
|
|
|
});
|
|
|
|
ASSERT(card_it != _draw_pile.end());
|
|
|
|
std::swap(*card_it, _draw_pile.front());
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-10 18:27:25 +02:00
|
|
|
probability_t HanabiState<num_suits, num_players, hand_size>::evaluate_state() {
|
2023-08-11 14:39:55 +02:00
|
|
|
ASSERT(_relative_representation.initialized);
|
2023-08-06 15:05:34 +02:00
|
|
|
_enumerated_states++;
|
2023-08-07 12:48:25 +02:00
|
|
|
const unsigned long id_of_state = unique_id();
|
|
|
|
|
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;
|
|
|
|
}
|
2023-08-07 13:34:27 +02:00
|
|
|
if (_position_tablebase.contains(id_of_state)) {
|
|
|
|
return _position_tablebase[id_of_state];
|
|
|
|
}
|
2023-08-06 11:54:57 +02:00
|
|
|
|
|
|
|
// TODO: Have some endgame analysis here?
|
|
|
|
|
2023-08-10 11:14:15 +02:00
|
|
|
probability_t best_probability = 0;
|
2023-08-10 18:23:33 +02:00
|
|
|
const std::array<Card, hand_size>& hand = _hands[_turn];
|
2023-08-06 11:54:57 +02:00
|
|
|
|
|
|
|
// First, check for playables
|
|
|
|
for(std::uint8_t index = 0; index < hand_size; index++) {
|
|
|
|
if(is_playable(hand[index])) {
|
2023-08-10 18:23:33 +02:00
|
|
|
probability_t sum_of_probabilities = 0;
|
|
|
|
|
2023-08-11 18:28:12 +02:00
|
|
|
do_for_each_potential_draw(index, true, [this, &sum_of_probabilities](const unsigned long multiplicity){
|
2023-08-10 18:27:25 +02:00
|
|
|
sum_of_probabilities += evaluate_state() * multiplicity;
|
2023-08-10 18:23:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const unsigned long total_weight = std::max(static_cast<unsigned long>(_weighted_draw_pile_size), 1ul);
|
|
|
|
const probability_t probability_play = sum_of_probabilities / total_weight;
|
|
|
|
|
|
|
|
best_probability = std::max(best_probability, probability_play);
|
|
|
|
if (best_probability == 1) {
|
|
|
|
update_tablebase(id_of_state, best_probability);
|
|
|
|
return best_probability;
|
|
|
|
};
|
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])) {
|
2023-08-10 11:14:15 +02:00
|
|
|
probability_t sum_of_probabilities = 0;
|
2023-08-10 18:23:33 +02:00
|
|
|
|
2023-08-11 18:28:12 +02:00
|
|
|
do_for_each_potential_draw(index, false, [this, &sum_of_probabilities](const unsigned long multiplicity){
|
2023-08-10 18:27:25 +02:00
|
|
|
sum_of_probabilities += evaluate_state() * multiplicity;
|
2023-08-10 18:23:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const unsigned long total_weight = std::max(static_cast<unsigned long>(_weighted_draw_pile_size), 1ul);
|
|
|
|
const probability_t probability_discard = sum_of_probabilities / total_weight;
|
|
|
|
best_probability = std::max(best_probability, probability_discard);
|
|
|
|
|
|
|
|
best_probability = std::max(best_probability, probability_discard);
|
|
|
|
if (best_probability == 1) {
|
|
|
|
update_tablebase(id_of_state, best_probability);
|
|
|
|
return best_probability;
|
|
|
|
};
|
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-12 08:50:28 +02:00
|
|
|
give_clue();
|
2023-08-10 18:27:25 +02:00
|
|
|
const probability_t probability_stall = evaluate_state();
|
2023-08-06 16:44:21 +02:00
|
|
|
revert_clue();
|
2023-08-10 18:23:33 +02:00
|
|
|
best_probability = std::max(best_probability, probability_stall);
|
|
|
|
if (best_probability == 1) {
|
|
|
|
update_tablebase(id_of_state, best_probability);
|
|
|
|
return best_probability;
|
|
|
|
};
|
2023-08-06 11:54:57 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 18:23:33 +02:00
|
|
|
update_tablebase(id_of_state, best_probability);
|
|
|
|
return best_probability;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-11 18:28:12 +02:00
|
|
|
template<class Function>
|
|
|
|
void HanabiState<num_suits, num_players, hand_size>::do_for_each_potential_draw(hand_index_t index, bool play, Function f) {
|
|
|
|
auto do_action = [this, index, play](){
|
|
|
|
if (play) {
|
2023-08-11 14:24:17 +02:00
|
|
|
return play_and_potentially_update(index);
|
2023-08-10 18:23:33 +02:00
|
|
|
} else {
|
2023-08-11 14:24:17 +02:00
|
|
|
return discard_and_potentially_update(index);
|
2023-08-10 18:23:33 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-11 18:28:12 +02:00
|
|
|
auto revert_action = [this, play](){
|
|
|
|
if (play) {
|
2023-08-10 18:23:33 +02:00
|
|
|
revert_play();
|
|
|
|
} else {
|
|
|
|
revert_discard();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(_draw_pile.empty()) {
|
|
|
|
do_action();
|
|
|
|
f(1);
|
|
|
|
revert_action();
|
|
|
|
} else {
|
2023-08-11 16:54:11 +02:00
|
|
|
unsigned sum_of_multiplicities = 0;
|
2023-08-10 18:23:33 +02:00
|
|
|
for(size_t i = 0; i < _draw_pile.size(); i++) {
|
|
|
|
const unsigned long multiplicity = do_action();
|
|
|
|
sum_of_multiplicities += multiplicity;
|
|
|
|
f(multiplicity);
|
|
|
|
revert_action();
|
|
|
|
}
|
|
|
|
ASSERT(sum_of_multiplicities == _weighted_draw_pile_size);
|
|
|
|
}
|
2023-08-06 11:54:57 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-08-07 12:48:25 +02:00
|
|
|
unsigned long id = 0;
|
|
|
|
|
|
|
|
// encode all positions of cards that started in draw pile
|
2023-08-11 13:47:57 +02:00
|
|
|
ASSERT(_relative_representation.card_positions_draw.size() == _relative_representation.good_cards_draw.size());
|
|
|
|
for(size_t i = 0; i < _relative_representation.card_positions_draw.size(); i++) {
|
|
|
|
for(player_t player : _relative_representation.card_positions_draw[i]) {
|
2023-08-07 12:48:25 +02:00
|
|
|
id *= num_players + 2;
|
2023-08-08 16:27:25 +02:00
|
|
|
// We normalize here: If a card is already played, then the positions of its other copies
|
|
|
|
// do not matter, so we can just pretend that they are all in the trash already.
|
|
|
|
// The resulting states will be equivalent.
|
2023-08-11 13:47:57 +02:00
|
|
|
if (!is_trash(_relative_representation.good_cards_draw[i])) {
|
2023-08-08 16:27:25 +02:00
|
|
|
id += player;
|
|
|
|
} else {
|
|
|
|
id += trash_or_play_stack;
|
|
|
|
}
|
2023-08-07 12:48:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode number of clues
|
|
|
|
id *= max_num_clues + 1;
|
|
|
|
id += _num_clues;
|
|
|
|
|
2023-08-07 13:34:27 +02:00
|
|
|
// we can encode draw pile size and extra turn in one metric, since we only have extra turns if draw pile is empty
|
|
|
|
const std::uint8_t draw_pile_size_and_extra_turns = [this]() -> uint8_t {
|
|
|
|
if(_endgame_turns_left == no_endgame) {
|
|
|
|
return _weighted_draw_pile_size + num_players;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return _endgame_turns_left;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2023-08-11 13:47:57 +02:00
|
|
|
id *= _relative_representation.initial_draw_pile_size + num_players;
|
2023-08-07 13:34:27 +02:00
|
|
|
id += draw_pile_size_and_extra_turns;
|
2023-08-07 12:48:25 +02:00
|
|
|
|
|
|
|
// encode positions of cards that started in hands
|
2023-08-11 13:47:57 +02:00
|
|
|
id = id << _relative_representation.num_useful_cards_in_starting_hands;
|
|
|
|
id += _relative_representation.card_positions_hands.to_ulong();
|
2023-08-07 12:48:25 +02:00
|
|
|
|
|
|
|
id *= num_players;
|
|
|
|
id += _turn;
|
|
|
|
|
|
|
|
// The id is unique now, since for all relevant cards, we know their position (including if they are played),
|
|
|
|
// the number of clues, the draw pile size and whose turn it is.
|
|
|
|
// This already uniquely determines the current players position, assuming that we never discard good cards
|
|
|
|
// (and only play them)
|
|
|
|
|
2023-08-07 11:48:39 +02:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2023-08-12 18:48:01 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
std::pair<std::vector<std::uint64_t>, std::vector<Card>> HanabiState<num_suits, num_players, hand_size>::dump_unique_id_parts() const {
|
|
|
|
std::vector<std::uint64_t> ret;
|
|
|
|
std::vector<Card> cards;
|
|
|
|
|
|
|
|
// encode all positions of cards that started in draw pile
|
|
|
|
ASSERT(_relative_representation.card_positions_draw.size() == _relative_representation.good_cards_draw.size());
|
|
|
|
for(size_t i = 0; i < _relative_representation.card_positions_draw.size(); i++) {
|
|
|
|
for(player_t player : _relative_representation.card_positions_draw[i]) {
|
|
|
|
// We normalize here: If a card is already played, then the positions of its other copies
|
|
|
|
// do not matter, so we can just pretend that they are all in the trash already.
|
|
|
|
// The resulting states will be equivalent.
|
|
|
|
if (!is_trash(_relative_representation.good_cards_draw[i])) {
|
|
|
|
ret.push_back(player);
|
|
|
|
} else {
|
|
|
|
ret.push_back(trash_or_play_stack);
|
|
|
|
}
|
|
|
|
cards.push_back(_relative_representation.good_cards_draw[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode number of clues
|
|
|
|
ret.push_back(_num_clues);
|
|
|
|
|
|
|
|
// we can encode draw pile size and extra turn in one metric, since we only have extra turns if draw pile is empty
|
|
|
|
const std::uint8_t draw_pile_size_and_extra_turns = [this]() -> uint8_t {
|
|
|
|
if(_endgame_turns_left == no_endgame) {
|
|
|
|
return _weighted_draw_pile_size + num_players;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return _endgame_turns_left;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
ret.push_back(draw_pile_size_and_extra_turns);
|
|
|
|
|
|
|
|
// encode positions of cards that started in hands
|
|
|
|
ret.push_back(_relative_representation.card_positions_hands.to_ulong());
|
|
|
|
|
|
|
|
ret.push_back(_turn);
|
|
|
|
|
|
|
|
// The id is unique now, since for all relevant cards, we know their position (including if they are played),
|
|
|
|
// the number of clues, the draw pile size and whose turn it is.
|
|
|
|
// This already uniquely determines the current players position, assuming that we never discard good cards
|
|
|
|
// (and only play them)
|
|
|
|
|
|
|
|
return {ret, cards};
|
|
|
|
}
|
|
|
|
|
2023-08-07 12:48:25 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
2023-08-11 11:43:05 +02:00
|
|
|
const std::unordered_map<unsigned long, probability_t>& HanabiState<num_suits, num_players, hand_size>::position_tablebase() const {
|
2023-08-07 12:48:25 +02:00
|
|
|
return _position_tablebase;
|
|
|
|
}
|
|
|
|
|
2023-08-08 00:31:16 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
size_t HanabiState<num_suits, num_players, hand_size>::draw_pile_size() const {
|
|
|
|
return _weighted_draw_pile_size;
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:24:17 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
bool HanabiState<num_suits, num_players, hand_size>::is_relative_state_initialized() const {
|
|
|
|
return _relative_representation.initialized;
|
|
|
|
}
|
|
|
|
|
2023-08-10 18:23:33 +02:00
|
|
|
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
|
|
|
|
void HanabiState<num_suits, num_players, hand_size>::update_tablebase(
|
|
|
|
unsigned long id,
|
|
|
|
Hanabi::probability_t probability) {
|
|
|
|
if (_position_tablebase.contains(id)) {
|
|
|
|
ASSERT(_position_tablebase[id] == probability);
|
|
|
|
}
|
|
|
|
_position_tablebase[id] = probability;
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:55:46 +02:00
|
|
|
} // namespace Hanabi
|