remove unneeded template parameter. implement game getter and normalization

This commit is contained in:
Maximilian Keßler 2023-08-06 10:23:29 +02:00
parent 38e4101402
commit b690f43a73
Signed by: max
GPG key ID: BCC5A619923C0BA5
4 changed files with 153 additions and 64 deletions

View file

@ -102,13 +102,13 @@ namespace Download {
return boost::json::parse(game_json).as_object(); return boost::json::parse(game_json).as_object();
} }
template<std::size_t num_suits, Hanabi::player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size> template<std::size_t num_suits, Hanabi::player_t num_players, std::size_t hand_size>
Hanabi::HanabiState<num_suits, num_players, hand_size, max_draw_pile_size> produce_state( Hanabi::HanabiState<num_suits, num_players, hand_size> produce_state(
const std::vector<Hanabi::Card>& deck, const std::vector<Hanabi::Card>& deck,
const std::vector<Action>& actions, const std::vector<Action>& actions,
size_t num_turns_to_replicate size_t num_turns_to_replicate
) { ) {
Hanabi::HanabiState<num_suits, num_players, hand_size, max_draw_pile_size> game(deck); Hanabi::HanabiState<num_suits, num_players, hand_size> game(deck);
std::uint8_t index; std::uint8_t index;
for (size_t i = 0; i < num_turns_to_replicate; i++) { for (size_t i = 0; i < num_turns_to_replicate; i++) {
switch(actions[i].type) { switch(actions[i].type) {
@ -134,7 +134,8 @@ namespace Download {
return game; return game;
} }
void get_game(std::variant<int, const char *> game_spec) { template <std::size_t num_suits, Hanabi::player_t num_players, std::size_t hand_size>
void get_game(std::variant<int, const char *> game_spec, unsigned turn) {
const boost::json::object game_json = [&game_spec]() { const boost::json::object game_json = [&game_spec]() {
if (game_spec.index() == 0) { if (game_spec.index() == 0) {
return download_game_json(std::get<int>(game_spec)); return download_game_json(std::get<int>(game_spec));
@ -144,11 +145,11 @@ namespace Download {
}(); }();
const std::vector<Hanabi::Card> deck = parse_deck(game_json.at("deck")); const std::vector<Hanabi::Card> deck = parse_deck(game_json.at("deck"));
const std::vector<Action> actions = parse_actions(game_json.at("actions")); const std::vector<Action> actions = parse_actions(game_json.at("actions"));
const unsigned num_players = game_json.at("players").as_array().size(); const size_t num_players_js = game_json.at("players").as_array().size();
std::cout << deck.size() << std::endl; assert (num_players_js == num_players);
std::cout << num_players;
auto game = produce_state<6, 3, 5, 0>(deck, actions, 40); auto game = produce_state<num_suits, num_players, hand_size>(deck, actions, turn);
game.normalize_draw_and_positions();
std::cout << game << std::endl; std::cout << game << std::endl;
} }

View file

@ -87,18 +87,37 @@ struct CardMultiplicity {
auto operator<=>(const CardMultiplicity &) const = default; auto operator<=>(const CardMultiplicity &) const = default;
}; };
template <std::size_t num_suits> struct CardPositions { template <std::size_t num_suits, typename T, bool respect_card_duplicity>
struct CardArrayMember {
};
CardPositions(); template <std::size_t num_suits, typename T>
struct CardArrayMember<num_suits, T, true> {
auto operator<=>(const CardArrayMember &) const = default;
std::array<std::array<std::array<T , max_card_duplicity>, starting_card_rank>, num_suits> array {};
};
const player_t &operator[](const Card &card) const; template <std::size_t num_suits, typename T>
struct CardArrayMember<num_suits, T, false> {
auto operator<=>(const CardArrayMember &) const = default;
std::array<std::array<T, starting_card_rank>, num_suits> array {};
};
player_t &operator[](const Card &card); template <std::size_t num_suits, typename T, bool respect_card_duplicity = true> struct CardArray {
auto operator<=>(const CardPositions &) const = default; using value_type = T;
CardArray() = default;
explicit CardArray(value_type default_val);
const value_type &operator[](const Card &card) const;
value_type &operator[](const Card &card);
auto operator<=>(const CardArray &) const = default;
private: private:
std::array<std::array<std::array<player_t, max_card_duplicity>, starting_card_rank>, num_suits> _card_positions; CardArrayMember<num_suits, T, respect_card_duplicity> _vals;
}; };
enum class ActionType { enum class ActionType {
@ -117,7 +136,7 @@ struct BacktrackAction {
std::uint8_t index{}; std::uint8_t index{};
}; };
template <std::size_t num_suits, player_t num_players, std::size_t hand_size, std::uint8_t max_draw_pile_size> template <std::size_t num_suits, player_t num_players, std::size_t hand_size>
class HanabiState { class HanabiState {
public: public:
HanabiState() = default; HanabiState() = default;
@ -135,6 +154,8 @@ public:
std::uint8_t find_card_in_hand(const Card& card) const; std::uint8_t find_card_in_hand(const Card& card) const;
void normalize_draw_and_positions();
void revert(const BacktrackAction &action); void revert(const BacktrackAction &action);
void draw(std::uint8_t index); void draw(std::uint8_t index);
@ -150,7 +171,7 @@ public:
std::uint8_t _draw_pile_size{}; std::uint8_t _draw_pile_size{};
Stacks<num_suits> _stacks{}; Stacks<num_suits> _stacks{};
std::array<std::array<Card, hand_size>, num_players> _hands{}; std::array<std::array<Card, hand_size>, num_players> _hands{};
CardPositions<num_suits> _card_positions{}; CardArray<num_suits, player_t> _card_positions{};
std::list<CardMultiplicity> _draw_pile{}; std::list<CardMultiplicity> _draw_pile{};
// further statistics that we might want to keep track of // further statistics that we might want to keep track of
@ -159,10 +180,10 @@ public:
auto operator<=>(const HanabiState &) const = default; auto operator<=>(const HanabiState &) const = default;
}; };
template <std::size_t num_suits, player_t num_players, std::size_t hand_size, std::uint8_t max_draw_pile_size> template <std::size_t num_suits, player_t num_players, std::size_t hand_size>
std::ostream & operator<<(std::ostream &os, HanabiState<num_suits, num_players, hand_size, max_draw_pile_size> hanabi_state); std::ostream & operator<<(std::ostream &os, HanabiState<num_suits, num_players, hand_size> hanabi_state);
template class HanabiState<5, 3, 4, 20>; template class HanabiState<5, 3, 4>;
} }

View file

@ -20,39 +20,51 @@ namespace Hanabi {
template<std::size_t num_suits> template<std::size_t num_suits>
std::ostream &operator<<(std::ostream &os, const Stacks<num_suits> &stacks) { std::ostream &operator<<(std::ostream &os, const Stacks<num_suits> &stacks) {
for (size_t i = 0; i < stacks.size() - 1; i++) { for (size_t i = 0; i < stacks.size() - 1; i++) {
os << +stacks[i] << ", "; os << starting_card_rank - stacks[i] << ", ";
} }
os << +stacks.back(); os << starting_card_rank - stacks.back();
return os; return os;
} }
template<std::size_t num_suits> template<std::size_t num_suits, typename T, bool respect_card_duplicity>
CardPositions<num_suits>::CardPositions() { CardArray<num_suits, T, respect_card_duplicity>::CardArray(T default_val) {
for(size_t suit = 0; suit < num_suits; suit++) { for(size_t suit = 0; suit < num_suits; suit++) {
for(rank_t rank = 0; rank < starting_card_rank; rank++) { for (rank_t rank = 0; rank < starting_card_rank; rank++) {
std::ranges::fill(_card_positions[suit][rank], draw_pile); 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> template<std::size_t num_suits, typename T, bool respect_card_duplicity>
const player_t &CardPositions<num_suits>::operator[](const Card &card) const { const T& CardArray<num_suits, T, respect_card_duplicity>::operator[](const Card &card) const {
return _card_positions[card.suit][card.rank][card.copy]; 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> template<std::size_t num_suits, typename T, bool respect_card_duplicity>
player_t &CardPositions<num_suits>::operator[](const Card &card) { T& CardArray<num_suits, T, respect_card_duplicity>::operator[](const Card &card) {
return _card_positions[card.suit][card.rank][card.copy]; 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, uint8_t max_draw_pile_size> template<size_t num_suits, player_t num_players, size_t hand_size>
HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::HanabiState(const std::vector<Card> &deck): HanabiState<num_suits, num_players, hand_size>::HanabiState(const std::vector<Card> &deck):
_turn(0), _turn(0),
_num_clues(max_num_clues), _num_clues(max_num_clues),
_draw_pile_size(deck.size() - num_players * hand_size), _draw_pile_size(deck.size() - num_players * hand_size),
_stacks(), _stacks(),
_hands(), _hands(),
_card_positions(), _card_positions(draw_pile),
_draw_pile() { _draw_pile() {
std::ranges::fill(_stacks, starting_card_rank); std::ranges::fill(_stacks, starting_card_rank);
for(const Card& card: deck) { for(const Card& card: deck) {
@ -67,8 +79,8 @@ namespace Hanabi {
assert(_turn == 0); assert(_turn == 0);
} }
template<size_t num_suits, player_t num_players, size_t hand_size, uint8_t max_draw_pile_size> template<size_t num_suits, player_t num_players, size_t hand_size>
BacktrackAction HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::clue() { BacktrackAction HanabiState<num_suits, num_players, hand_size>::clue() {
assert(_num_clues > 0); assert(_num_clues > 0);
--_num_clues; --_num_clues;
@ -77,20 +89,18 @@ namespace Hanabi {
return BacktrackAction{ActionType::clue, {}, {}}; return BacktrackAction{ActionType::clue, {}, {}};
} }
template<size_t num_suits, player_t num_players, size_t hand_size, uint8_t max_draw_pile_size> template<size_t num_suits, player_t num_players, size_t hand_size>
void HanabiState<num_suits, num_players, hand_size, void HanabiState<num_suits, num_players, hand_size>::incr_turn() {
max_draw_pile_size>::incr_turn() {
_turn = (_turn + 1) % num_players; _turn = (_turn + 1) % num_players;
} }
template<size_t num_suits, player_t num_players, size_t hand_size, uint8_t max_draw_pile_size> template<size_t num_suits, player_t num_players, size_t hand_size>
void HanabiState<num_suits, num_players, hand_size, void HanabiState<num_suits, num_players, hand_size>::decr_turn() {
max_draw_pile_size>::decr_turn() {
_turn = (_turn + num_players - 1) % num_players; _turn = (_turn + num_players - 1) % num_players;
} }
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size> template<std::size_t num_suits, player_t num_players, std::size_t hand_size>
BacktrackAction HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::play( BacktrackAction HanabiState<num_suits, num_players, hand_size>::play(
std::uint8_t index) { std::uint8_t index) {
assert(index < _hands[_turn].size()); assert(index < _hands[_turn].size());
const Card card = _hands[_turn][index]; const Card card = _hands[_turn][index];
@ -111,8 +121,8 @@ namespace Hanabi {
return ret; return ret;
} }
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size> template<std::size_t num_suits, player_t num_players, std::size_t hand_size>
BacktrackAction HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::discard( BacktrackAction HanabiState<num_suits, num_players, hand_size>::discard(
std::uint8_t index) { std::uint8_t index) {
assert(index < _hands[_turn].size()); assert(index < _hands[_turn].size());
assert(_num_clues != max_num_clues); assert(_num_clues != max_num_clues);
@ -127,8 +137,8 @@ namespace Hanabi {
return ret; return ret;
} }
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size> 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, max_draw_pile_size>::find_card_in_hand( std::uint8_t HanabiState<num_suits, num_players, hand_size>::find_card_in_hand(
const Hanabi::Card &card) const { const Hanabi::Card &card) const {
for(std::uint8_t i = 0; i < hand_size; i++) { for(std::uint8_t i = 0; i < hand_size; i++) {
if(_hands[_turn][i].rank == card.rank && _hands[_turn][i].suit == card.suit) { if(_hands[_turn][i].rank == card.rank && _hands[_turn][i].suit == card.suit) {
@ -138,8 +148,8 @@ namespace Hanabi {
return -1; return -1;
} }
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size> 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, max_draw_pile_size> hanabi_state) { std::ostream &operator<<(std::ostream &os, const HanabiState<num_suits, num_players, hand_size> hanabi_state) {
os << "Stacks: " << hanabi_state._stacks << std::endl; os << "Stacks: " << hanabi_state._stacks << std::endl;
os << "Draw pile: "; os << "Draw pile: ";
for (const auto &[card, mul]: hanabi_state._draw_pile) { for (const auto &[card, mul]: hanabi_state._draw_pile) {
@ -147,6 +157,7 @@ namespace Hanabi {
if (mul > 1) { if (mul > 1) {
os << " (" << +mul << ")"; os << " (" << +mul << ")";
} }
os << ", ";
} }
os << std::endl; os << std::endl;
os << "Hands: "; os << "Hands: ";
@ -159,11 +170,14 @@ namespace Hanabi {
return os; return os;
} }
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_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, max_draw_pile_size>::draw(std::uint8_t index) { void HanabiState<num_suits, num_players, hand_size>::draw(std::uint8_t index) {
assert(index < _hands[_turn].size()); assert(index < _hands[_turn].size());
_card_positions[_hands[_turn][index]] = trash_or_play_stack; 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 // draw a new card if the draw pile is not empty
if (!_draw_pile.empty()) { if (!_draw_pile.empty()) {
@ -175,16 +189,21 @@ namespace Hanabi {
draw.multiplicity--; draw.multiplicity--;
_draw_pile.push_back(draw); _draw_pile.push_back(draw);
} }
draw.card.copy = draw.multiplicity - 1;
_hands[_turn][index] = draw.card; _hands[_turn][index] = draw.card;
_card_positions[draw.card] = _turn; 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, uint8_t max_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, max_draw_pile_size>::revert_draw(std::uint8_t index, Card card) { void HanabiState<num_suits, num_players, hand_size>::revert_draw(std::uint8_t index, Card card) {
assert(index < _hands[_turn].size()); assert(index < _hands[_turn].size());
const Card& discarded = _hands[_turn][index];
_card_positions[_hands[_turn][index]] = draw_pile; if (_stacks[discarded.suit] > discarded.rank) {
_card_positions[discarded] = draw_pile;
}
// put card back into draw pile (at the back) // put card back into draw pile (at the back)
if (!_draw_pile.empty() and _draw_pile.back().card == _hands[_turn][index]) { if (!_draw_pile.empty() and _draw_pile.back().card == _hands[_turn][index]) {
@ -194,12 +213,60 @@ namespace Hanabi {
} }
_hands[_turn][index] = card; _hands[_turn][index] = card;
_card_positions[card] = _turn; if (_stacks[card.suit] > card.rank) {
_card_positions[card] = _turn;
}
_draw_pile_size++; _draw_pile_size++;
} }
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_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, max_draw_pile_size>::revert( 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) { const BacktrackAction &action) {
decr_turn(); decr_turn();
switch (action.type) { switch (action.type) {

View file

@ -14,7 +14,7 @@
namespace Hanabi { namespace Hanabi {
void test_game() { void test_game() {
HanabiState<2, 2, 5, 10> state; HanabiState<2, 2, 5> state;
state._stacks[0] = 2; state._stacks[0] = 2;
state._stacks[1] = 3; state._stacks[1] = 3;
Card r41 = {0, 4, 1}; Card r41 = {0, 4, 1};
@ -38,10 +38,10 @@ void test_game() {
assert(state == state2); assert(state == state2);
} }
void download() { Download::get_game("1004116.json"); } void download() { Download::get_game<6,3,5>("1004116.json", 40); }
void print_sizes() { void print_sizes() {
std::cout << "size of card -> hand map: " << sizeof(HanabiState<5, 3, 4, 5>) std::cout << "size of card -> hand map: " << sizeof(HanabiState<5, 3, 4>)
<< std::endl; << std::endl;
unsigned exp = 32; unsigned exp = 32;