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,17 +90,16 @@ struct CardMultiplicity {
template <std::size_t num_suits> struct CardPositions {
const player_t &operator[](const Card &card) const;
CardPositions();
player_t &operator[](const Card &card);
const player_t &operator[](const Card &card) const;
auto operator<=>(const CardPositions &) const = default;
player_t &operator[](const Card &card);
auto operator<=>(const CardPositions &) const = default;
private:
std::array<
std::array<std::array<player_t, max_card_duplicity>, starting_card_rank>,
num_suits>
_card_positions;
std::array<std::array<std::array<player_t, max_card_duplicity>, starting_card_rank>, num_suits> _card_positions;
};
enum class ActionType { discard, clue, play };
@ -111,48 +110,48 @@ struct Action {
std::uint8_t index{};
};
template <std::size_t num_suits, std::size_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::uint8_t max_draw_pile_size>
class HanabiState {
public:
Action clue();
/**
* 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
*/
Action play(std::uint8_t index);
HanabiState() = default;
explicit HanabiState(const std::vector<Card>& deck);
Action discard(std::uint8_t index);
Action clue();
void revert(const Action &action);
/**
* 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
*/
Action play(std::uint8_t index);
void draw(std::uint8_t index);
void revert_draw(std::uint8_t index, Card card);
void incr_turn();
void decr_turn();
Action discard(std::uint8_t index);
player_t _turn{};
clue_t _num_clues{};
std::uint8_t _draw_pile_size{};
Stacks<num_suits> _stacks{};
std::array<boost::container::static_vector<Card, hand_size>, num_players>
_hands{};
CardPositions<num_suits> _card_positions{};
std::list<CardMultiplicity> _draw_pile{};
void revert(const Action &action);
// further statistics that we might want to keep track of
uint8_t _pace{};
void draw(std::uint8_t index);
auto operator<=>(const HanabiState &) const = default;
void revert_draw(std::uint8_t index, Card card);
void incr_turn();
void decr_turn();
player_t _turn{};
clue_t _num_clues{};
std::uint8_t _draw_pile_size{};
Stacks<num_suits> _stacks{};
std::array<boost::container::static_vector<Card, hand_size>, num_players> _hands{};
CardPositions<num_suits> _card_positions{};
std::list<CardMultiplicity> _draw_pile{};
// further statistics that we might want to keep track of
uint8_t _pace{};
auto operator<=>(const HanabiState &) const = default;
};
template <std::size_t num_suits, std::size_t num_players, std::size_t hand_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);
template <std::size_t num_suits, player_t num_players, std::size_t hand_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);
template class HanabiState<5, 3, 4, 20>;

View file

@ -26,6 +26,15 @@ namespace Hanabi {
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>
const player_t &CardPositions<num_suits>::operator[](const Card &card) const {
return _card_positions[card.suit][card.rank][card.copy];
@ -36,7 +45,29 @@ namespace Hanabi {
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() {
assert(_num_clues > 0);
--_num_clues;
@ -46,19 +77,19 @@ namespace Hanabi {
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,
max_draw_pile_size>::incr_turn() {
_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,
max_draw_pile_size>::decr_turn() {
_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(
std::uint8_t index) {
assert(index < _hands[_turn].size());
@ -80,7 +111,7 @@ namespace Hanabi {
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(
std::uint8_t index) {
assert(index < _hands[_turn].size());
@ -96,7 +127,7 @@ namespace Hanabi {
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) {
os << "Stacks: " << hanabi_state._stacks << std::endl;
os << "Draw pile: ";
@ -117,7 +148,7 @@ namespace Hanabi {
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) {
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) {
assert(index < _hands[_turn].size());
@ -156,7 +187,7 @@ namespace Hanabi {
_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(
const Action &action) {
decr_turn();

View file

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