Endgame-Analyzer/game_state.h

178 lines
4.4 KiB
C
Raw Normal View History

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>
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;
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;
uint8_t copy;
uint8_t index; // index of card in the deck
2023-08-04 16:28:41 +02:00
Card &operator++();
2023-08-04 16:55:41 +02:00
Card successor() const;
const Card operator++(int);
2023-08-04 16:28:41 +02:00
auto operator<=>(const Card &) const = default;
2023-08-04 16:28:41 +02:00
};
std::ostream &operator<<(std::ostream &os, const Card &card) {
2023-08-04 16:28:41 +02:00
os << suit_initials[card.suit] << +card.rank;
return os;
}
constexpr Card r0 = {0, 0, 0};
constexpr Card r1 = {0, 1, 0};
constexpr Card r2 = {0, 2, 0};
constexpr Card r3 = {0, 3, 0};
constexpr Card r4 = {0, 4, 0};
constexpr Card y0 = {1, 0, 0};
constexpr Card y1 = {1, 1, 0};
constexpr Card y2 = {1, 2, 0};
constexpr Card y3 = {1, 3, 0};
constexpr Card y4 = {1, 4, 0};
/**
* To store:
* - Draw pile size
* - Distribution of cards
* - Which cards exist?
* - Number of clues
*/
template <std::size_t num_suits> using Stacks = std::array<rank_t, num_suits>;
2023-08-04 16:28:41 +02:00
template <std::size_t num_suits>
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;
auto operator<=>(const CardMultiplicity &) const = default;
2023-08-04 16:28:41 +02:00
};
template <std::size_t num_suits> struct CardPositions {
2023-08-04 16:28:41 +02:00
2023-08-05 12:19:34 +02:00
CardPositions();
2023-08-04 16:28:41 +02:00
2023-08-05 12:19:34 +02:00
const player_t &operator[](const Card &card) const;
2023-08-04 16:28:41 +02:00
2023-08-05 12:19:34 +02:00
player_t &operator[](const Card &card);
auto operator<=>(const CardPositions &) const = default;
2023-08-04 16:28:41 +02:00
private:
2023-08-05 12:19:34 +02:00
std::array<std::array<std::array<player_t, max_card_duplicity>, starting_card_rank>, num_suits> _card_positions;
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
/**
* Action type for replay format of hanab.live
*/
2023-08-04 16:28:41 +02:00
struct Action {
2023-08-05 13:04:51 +02:00
ActionType type{};
uint8_t target;
};
struct BacktrackAction {
ActionType type{};
Card discarded{};
std::uint8_t index{};
2023-08-04 16:28:41 +02:00
};
2023-08-05 12:19:34 +02:00
template <std::size_t num_suits, player_t num_players, std::size_t hand_size, std::uint8_t max_draw_pile_size>
2023-08-04 16:28:41 +02:00
class HanabiState {
public:
2023-08-05 12:19:34 +02:00
HanabiState() = default;
explicit HanabiState(const std::vector<Card>& deck);
2023-08-05 13:04:51 +02:00
BacktrackAction clue();
2023-08-05 12:19:34 +02:00
/**
* 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
*/
2023-08-05 13:04:51 +02:00
BacktrackAction play(std::uint8_t index);
2023-08-05 12:19:34 +02:00
2023-08-05 13:04:51 +02:00
BacktrackAction discard(std::uint8_t index);
2023-08-05 12:19:34 +02:00
2023-08-05 13:04:51 +02:00
void revert(const BacktrackAction &action);
2023-08-05 12:19:34 +02:00
void draw(std::uint8_t index);
2023-08-04 16:28:41 +02:00
2023-08-05 12:19:34 +02:00
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;
};
2023-08-04 16:28:41 +02:00
2023-08-05 12:19:34 +02:00
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);
2023-08-04 16:28:41 +02:00
template class HanabiState<5, 3, 4, 20>;
}
2023-08-05 11:55:46 +02:00
#include "game_state.hpp"
#endif // DYNAMIC_PROGRAM_GAME_STATE_H