2023-08-04 16:28:41 +02:00
|
|
|
#ifndef DYNAMIC_PROGRAM_GAME_STATE_H
|
|
|
|
#define DYNAMIC_PROGRAM_GAME_STATE_H
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <bitset>
|
|
|
|
#include <limits>
|
|
|
|
#include <optional>
|
|
|
|
#include <boost/container/static_vector.hpp>
|
|
|
|
#include <list>
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
|
2023-08-05 00:34:31 +02:00
|
|
|
namespace Hanabi {
|
|
|
|
|
2023-08-04 16:28:41 +02:00
|
|
|
using rank_t = std::uint8_t;
|
|
|
|
using suit_t = std::uint8_t;
|
|
|
|
using clue_t = std::uint8_t;
|
|
|
|
using player_t = std::int8_t;
|
2023-08-06 23:12:44 +02:00
|
|
|
using hand_index_t = std::uint8_t;
|
2023-08-04 16:28:41 +02:00
|
|
|
|
|
|
|
using state_t = std::uint32_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We will generally assume that stacks are played from n to 0
|
|
|
|
* Playing a 0 will yield a clue
|
|
|
|
* Therefore, for the default hanabi, we will play 4,3,2,1,0 in that order
|
|
|
|
* on each stack. A stack with no cards played implicitly has value 5 on it
|
|
|
|
* This is just easier to implement, since then the remaining number of cards
|
|
|
|
* to be played is always the current number of the stack
|
|
|
|
*/
|
|
|
|
constexpr rank_t starting_card_rank = 5;
|
|
|
|
constexpr suit_t max_suit_index = 5;
|
|
|
|
constexpr size_t max_card_duplicity = 3;
|
|
|
|
constexpr player_t draw_pile = -1;
|
|
|
|
constexpr player_t trash_or_play_stack = -2;
|
|
|
|
constexpr clue_t max_num_clues = 8;
|
|
|
|
|
2023-08-05 11:55:46 +02:00
|
|
|
constexpr std::array<std::string, 6> suit_initials{"r", "y", "g", "b", "p", "t"};
|
2023-08-04 16:28:41 +02:00
|
|
|
|
|
|
|
struct Card {
|
|
|
|
suit_t suit;
|
|
|
|
rank_t rank;
|
|
|
|
|
2023-08-05 00:34:31 +02:00
|
|
|
Card &operator++();
|
2023-08-04 16:55:41 +02:00
|
|
|
const Card operator++(int);
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-05 00:34:31 +02:00
|
|
|
auto operator<=>(const Card &) const = default;
|
2023-08-04 16:28:41 +02:00
|
|
|
};
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
std::ostream &operator<<(std::ostream &os, const Card &card);
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-06 22:20:20 +02:00
|
|
|
constexpr Card r0 = {0, 0};
|
|
|
|
constexpr Card r1 = {0, 1};
|
|
|
|
constexpr Card r2 = {0, 2};
|
|
|
|
constexpr Card r3 = {0, 3};
|
|
|
|
constexpr Card r4 = {0, 4};
|
|
|
|
constexpr Card y0 = {1, 0};
|
|
|
|
constexpr Card y1 = {1, 1};
|
|
|
|
constexpr Card y2 = {1, 2};
|
|
|
|
constexpr Card y3 = {1, 3};
|
|
|
|
constexpr Card y4 = {1, 4};
|
2023-08-04 16:28:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* To store:
|
|
|
|
* - Draw pile size
|
|
|
|
* - Distribution of cards
|
|
|
|
* - Which cards exist?
|
|
|
|
* - Number of clues
|
|
|
|
*/
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template <suit_t num_suits>
|
|
|
|
using Stacks = std::array<rank_t, num_suits>;
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template <suit_t num_suits>
|
2023-08-05 00:34:31 +02:00
|
|
|
std::ostream &operator<<(std::ostream &os, const Stacks<num_suits> &stacks);
|
2023-08-04 16:28:41 +02:00
|
|
|
|
|
|
|
struct CardMultiplicity {
|
|
|
|
Card card;
|
|
|
|
std::uint8_t multiplicity;
|
|
|
|
|
2023-08-05 00:34:31 +02:00
|
|
|
auto operator<=>(const CardMultiplicity &) const = default;
|
2023-08-04 16:28:41 +02:00
|
|
|
};
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
template <suit_t num_suits, typename T> struct CardArray {
|
2023-08-06 10:23:29 +02:00
|
|
|
using value_type = T;
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-06 10:23:29 +02:00
|
|
|
CardArray() = default;
|
|
|
|
explicit CardArray(value_type default_val);
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-06 10:23:29 +02:00
|
|
|
const value_type &operator[](const Card &card) const;
|
2023-08-05 12:19:34 +02:00
|
|
|
|
2023-08-06 10:23:29 +02:00
|
|
|
value_type &operator[](const Card &card);
|
|
|
|
|
|
|
|
auto operator<=>(const CardArray &) const = default;
|
2023-08-04 16:28:41 +02:00
|
|
|
|
|
|
|
private:
|
2023-08-06 22:20:20 +02:00
|
|
|
std::array<std::array<T, starting_card_rank>, num_suits> _array {};
|
2023-08-04 16:28:41 +02:00
|
|
|
};
|
|
|
|
|
2023-08-05 13:04:51 +02:00
|
|
|
enum class ActionType {
|
|
|
|
play = 0,
|
|
|
|
discard = 1,
|
|
|
|
clue = 2,
|
|
|
|
color_clue = 2,
|
|
|
|
rank_clue = 3,
|
|
|
|
end_game = 4,
|
|
|
|
vote_terminate = 10,
|
|
|
|
};
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-05 13:04:51 +02:00
|
|
|
struct BacktrackAction {
|
2023-08-06 11:54:57 +02:00
|
|
|
// The card that was discarded or played
|
2023-08-05 00:34:31 +02:00
|
|
|
Card discarded{};
|
2023-08-06 11:54:57 +02:00
|
|
|
// Index of card in hand that was discarded or played
|
2023-08-06 23:12:44 +02:00
|
|
|
hand_index_t index{};
|
2023-08-06 11:54:57 +02:00
|
|
|
// Multiplicity of new draw (needed for probability calculations)
|
2023-08-06 23:12:44 +02:00
|
|
|
hand_index_t multiplicity{};
|
2023-08-04 16:28:41 +02:00
|
|
|
};
|
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
class HanabiStateIF {
|
|
|
|
public:
|
|
|
|
virtual double backtrack(size_t depth) = 0;
|
|
|
|
|
|
|
|
virtual void clue() = 0;
|
2023-08-06 23:12:44 +02:00
|
|
|
virtual BacktrackAction discard(hand_index_t index) = 0;
|
|
|
|
virtual BacktrackAction play(hand_index_t index) = 0;
|
2023-08-06 22:06:58 +02:00
|
|
|
|
|
|
|
virtual void revert_clue() = 0;
|
|
|
|
virtual void revert_play(const BacktrackAction &action, bool was_on_8_clues) = 0;
|
|
|
|
virtual void revert_discard(const BacktrackAction &action) = 0;
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
[[nodiscard]] virtual hand_index_t find_card_in_hand(const Card& card) const = 0;
|
2023-08-06 22:06:58 +02:00
|
|
|
[[nodiscard]] virtual bool is_trash(const Card& card) const = 0;
|
|
|
|
[[nodiscard]] virtual bool is_playable(const Card& card) const = 0;
|
|
|
|
|
|
|
|
[[nodiscard]] virtual std::uint64_t enumerated_states() const = 0;
|
|
|
|
|
|
|
|
virtual void normalize_draw_and_positions() = 0;
|
|
|
|
|
|
|
|
virtual ~HanabiStateIF() = default;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void print(std::ostream& os) const = 0;
|
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream&, HanabiStateIF const&);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
class HanabiState : public HanabiStateIF {
|
2023-08-04 16:28:41 +02:00
|
|
|
public:
|
2023-08-05 12:19:34 +02:00
|
|
|
HanabiState() = default;
|
|
|
|
explicit HanabiState(const std::vector<Card>& deck);
|
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
double backtrack(size_t depth) final;
|
2023-08-06 11:54:57 +02:00
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
void clue() final;
|
2023-08-06 23:12:44 +02:00
|
|
|
BacktrackAction play(hand_index_t index) final;
|
|
|
|
BacktrackAction discard(hand_index_t index) final;
|
2023-08-05 12:19:34 +02:00
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
void revert_clue() final;
|
|
|
|
void revert_play(const BacktrackAction &action, bool was_on_8_clues) final;
|
|
|
|
void revert_discard(const BacktrackAction &action) final;
|
2023-08-05 12:19:34 +02:00
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
[[nodiscard]] hand_index_t find_card_in_hand(const Card& card) const final;
|
2023-08-06 22:06:58 +02:00
|
|
|
[[nodiscard]] bool is_trash(const Card& card) const final;
|
|
|
|
[[nodiscard]] bool is_playable(const Card& card) const final;
|
2023-08-05 12:19:34 +02:00
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
[[nodiscard]] std::uint64_t enumerated_states() const final;
|
2023-08-05 13:51:55 +02:00
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
void normalize_draw_and_positions() final;
|
2023-08-06 10:23:29 +02:00
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
auto operator<=>(const HanabiState &) const = default;
|
2023-08-05 12:19:34 +02:00
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
protected:
|
|
|
|
void print(std::ostream& os) const final;
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
private:
|
2023-08-07 00:06:50 +02:00
|
|
|
BacktrackAction play_no_strike(hand_index_t index);
|
|
|
|
|
2023-08-06 23:12:44 +02:00
|
|
|
hand_index_t draw(hand_index_t index);
|
|
|
|
void revert_draw(hand_index_t index, Card discarded_card);
|
2023-08-05 12:19:34 +02:00
|
|
|
|
|
|
|
void incr_turn();
|
|
|
|
void decr_turn();
|
|
|
|
|
|
|
|
player_t _turn{};
|
|
|
|
clue_t _num_clues{};
|
2023-08-06 11:54:57 +02:00
|
|
|
std::uint8_t _weighted_draw_pile_size{};
|
2023-08-05 12:19:34 +02:00
|
|
|
Stacks<num_suits> _stacks{};
|
2023-08-05 13:51:55 +02:00
|
|
|
std::array<std::array<Card, hand_size>, num_players> _hands{};
|
2023-08-05 12:19:34 +02:00
|
|
|
std::list<CardMultiplicity> _draw_pile{};
|
2023-08-06 22:06:58 +02:00
|
|
|
std::uint8_t _endgame_turns_left{};
|
2023-08-05 12:19:34 +02:00
|
|
|
|
2023-08-06 22:06:58 +02:00
|
|
|
static constexpr uint8_t no_endgame = std::numeric_limits<uint8_t>::max();
|
2023-08-06 13:53:18 +02:00
|
|
|
|
2023-08-05 12:19:34 +02:00
|
|
|
// further statistics that we might want to keep track of
|
|
|
|
uint8_t _pace{};
|
2023-08-06 11:54:57 +02:00
|
|
|
uint8_t _score{};
|
2023-08-05 12:19:34 +02:00
|
|
|
|
2023-08-06 15:02:50 +02:00
|
|
|
std::uint64_t _enumerated_states {};
|
2023-08-05 12:19:34 +02:00
|
|
|
};
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-06 14:06:41 +02:00
|
|
|
template <std::size_t num_suits, player_t num_players, std::size_t hand_size>
|
|
|
|
bool same_up_to_discard_permutation(HanabiState<num_suits, num_players, hand_size> state1, HanabiState<num_suits, num_players, hand_size> state2) {
|
|
|
|
auto comp = [](CardMultiplicity &m1, CardMultiplicity &m2) -> bool {
|
|
|
|
return m1.card.suit < m2.card.suit || (m1.card.suit == m2.card.suit and m1.card.rank < m2.card.rank) ||
|
|
|
|
(m1.card.suit == m2.card.suit and m1.card.rank == m2.card.rank and m1.multiplicity < m2.multiplicity);
|
|
|
|
};
|
|
|
|
state1._draw_pile.sort(comp);
|
|
|
|
state2._draw_pile.sort(comp);
|
|
|
|
return state1 == state2;
|
|
|
|
}
|
|
|
|
|
2023-08-04 16:28:41 +02:00
|
|
|
|
2023-08-05 00:34:31 +02:00
|
|
|
}
|
|
|
|
|
2023-08-05 11:55:46 +02:00
|
|
|
#include "game_state.hpp"
|
|
|
|
|
2023-08-05 00:34:31 +02:00
|
|
|
#endif // DYNAMIC_PROGRAM_GAME_STATE_H
|