proper constructor for hanabi state

This commit is contained in:
Maximilian Keßler 2023-08-05 12:19:34 +02:00
parent babc3f5085
commit 452c028f72
Signed by: max
GPG key ID: BCC5A619923C0BA5
3 changed files with 79 additions and 49 deletions

View file

@ -90,6 +90,8 @@ struct CardMultiplicity {
template <std::size_t num_suits> struct CardPositions { template <std::size_t num_suits> struct CardPositions {
CardPositions();
const player_t &operator[](const Card &card) const; const player_t &operator[](const Card &card) const;
player_t &operator[](const Card &card); player_t &operator[](const Card &card);
@ -97,10 +99,7 @@ template <std::size_t num_suits> struct CardPositions {
auto operator<=>(const CardPositions &) const = default; auto operator<=>(const CardPositions &) const = default;
private: private:
std::array< std::array<std::array<std::array<player_t, max_card_duplicity>, starting_card_rank>, num_suits> _card_positions;
std::array<std::array<player_t, max_card_duplicity>, starting_card_rank>,
num_suits>
_card_positions;
}; };
enum class ActionType { discard, clue, play }; enum class ActionType { discard, clue, play };
@ -111,11 +110,14 @@ struct Action {
std::uint8_t index{}; std::uint8_t index{};
}; };
template <std::size_t num_suits, std::size_t num_players, std::size_t hand_size, template <std::size_t num_suits, player_t num_players, std::size_t hand_size, std::uint8_t max_draw_pile_size>
std::uint8_t max_draw_pile_size>
class HanabiState { class HanabiState {
public: public:
HanabiState() = default;
explicit HanabiState(const std::vector<Card>& deck);
Action clue(); Action clue();
/** /**
* Plays a card from current hand, drawing top card of draw pile and rotating draw pile * Plays a card from current hand, drawing top card of draw pile and rotating draw pile
* @param index of card in hand to be played * @param index of card in hand to be played
@ -127,16 +129,18 @@ public:
void revert(const Action &action); void revert(const Action &action);
void draw(std::uint8_t index); void draw(std::uint8_t index);
void revert_draw(std::uint8_t index, Card card); void revert_draw(std::uint8_t index, Card card);
void incr_turn(); void incr_turn();
void decr_turn(); void decr_turn();
player_t _turn{}; player_t _turn{};
clue_t _num_clues{}; clue_t _num_clues{};
std::uint8_t _draw_pile_size{}; std::uint8_t _draw_pile_size{};
Stacks<num_suits> _stacks{}; Stacks<num_suits> _stacks{};
std::array<boost::container::static_vector<Card, hand_size>, num_players> std::array<boost::container::static_vector<Card, hand_size>, num_players> _hands{};
_hands{};
CardPositions<num_suits> _card_positions{}; CardPositions<num_suits> _card_positions{};
std::list<CardMultiplicity> _draw_pile{}; std::list<CardMultiplicity> _draw_pile{};
@ -146,13 +150,8 @@ public:
auto operator<=>(const HanabiState &) const = default; auto operator<=>(const HanabiState &) const = default;
}; };
template <std::size_t num_suits, std::size_t num_players, std::size_t hand_size, template <std::size_t num_suits, player_t num_players, std::size_t hand_size, std::uint8_t max_draw_pile_size>
std::uint8_t max_draw_pile_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, max_draw_pile_size>
hanabi_state);
template class HanabiState<5, 3, 4, 20>; template class HanabiState<5, 3, 4, 20>;

View file

@ -26,6 +26,15 @@ namespace Hanabi {
return os; return os;
} }
template<std::size_t num_suits>
CardPositions<num_suits>::CardPositions() {
for(size_t suit = 0; suit < num_suits; suit++) {
for(rank_t rank = 0; rank < starting_card_rank; rank++) {
std::ranges::fill(_card_positions[suit][rank], draw_pile);
}
}
}
template<std::size_t num_suits> template<std::size_t num_suits>
const player_t &CardPositions<num_suits>::operator[](const Card &card) const { const player_t &CardPositions<num_suits>::operator[](const Card &card) const {
return _card_positions[card.suit][card.rank][card.copy]; return _card_positions[card.suit][card.rank][card.copy];
@ -36,7 +45,29 @@ namespace Hanabi {
return _card_positions[card.suit][card.rank][card.copy]; return _card_positions[card.suit][card.rank][card.copy];
}; };
template<size_t num_suits, size_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, uint8_t max_draw_pile_size>
HanabiState<num_suits, num_players, hand_size, max_draw_pile_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() {
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, uint8_t max_draw_pile_size>
Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::clue() { Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::clue() {
assert(_num_clues > 0); assert(_num_clues > 0);
--_num_clues; --_num_clues;
@ -46,19 +77,19 @@ namespace Hanabi {
return Action{ActionType::clue, {}, {}}; return Action{ActionType::clue, {}, {}};
} }
template<size_t num_suits, size_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, uint8_t max_draw_pile_size>
void HanabiState<num_suits, num_players, hand_size, void HanabiState<num_suits, num_players, hand_size,
max_draw_pile_size>::incr_turn() { max_draw_pile_size>::incr_turn() {
_turn = (_turn + 1) % num_players; _turn = (_turn + 1) % num_players;
} }
template<size_t num_suits, size_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, uint8_t max_draw_pile_size>
void HanabiState<num_suits, num_players, hand_size, void HanabiState<num_suits, num_players, hand_size,
max_draw_pile_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, std::size_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, uint8_t max_draw_pile_size>
Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::play( Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::play(
std::uint8_t index) { std::uint8_t index) {
assert(index < _hands[_turn].size()); assert(index < _hands[_turn].size());
@ -80,7 +111,7 @@ namespace Hanabi {
return ret; return ret;
} }
template<std::size_t num_suits, std::size_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, uint8_t max_draw_pile_size>
Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::discard( Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::discard(
std::uint8_t index) { std::uint8_t index) {
assert(index < _hands[_turn].size()); assert(index < _hands[_turn].size());
@ -96,7 +127,7 @@ namespace Hanabi {
return ret; return ret;
} }
template<std::size_t num_suits, std::size_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, uint8_t max_draw_pile_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, max_draw_pile_size> hanabi_state) {
os << "Stacks: " << hanabi_state._stacks << std::endl; os << "Stacks: " << hanabi_state._stacks << std::endl;
os << "Draw pile: "; os << "Draw pile: ";
@ -117,7 +148,7 @@ namespace Hanabi {
return os; return os;
} }
template<std::size_t num_suits, std::size_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, uint8_t max_draw_pile_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, max_draw_pile_size>::draw(std::uint8_t index) {
assert(index < _hands[_turn].size()); assert(index < _hands[_turn].size());
@ -138,7 +169,7 @@ namespace Hanabi {
} }
} }
template<std::size_t num_suits, std::size_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, uint8_t max_draw_pile_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, max_draw_pile_size>::revert_draw(std::uint8_t index, Card card) {
assert(index < _hands[_turn].size()); assert(index < _hands[_turn].size());
@ -156,7 +187,7 @@ namespace Hanabi {
_draw_pile_size++; _draw_pile_size++;
} }
template<std::size_t num_suits, std::size_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, uint8_t max_draw_pile_size>
void HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::revert( void HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::revert(
const Action &action) { const Action &action) {
decr_turn(); decr_turn();

View file

@ -21,8 +21,8 @@ void test_game() {
state._draw_pile.push_back({r41, 1}); state._draw_pile.push_back({r41, 1});
state._hands[0] = {y0, y1, y2, r0, r1}; state._hands[0] = {y0, y1, y2, r0, r1};
state._hands[1] = {r1, r1, y1, r3, r2}; state._hands[1] = {r1, r1, y1, r3, r2};
state._card_positions[r1] = 0;
state._draw_pile_size = 1; state._draw_pile_size = 1;
state._card_positions[r41] = draw_pile;
auto state2 = state; auto state2 = state;