Endgame-Analyzer/game_state.hpp

215 lines
7.3 KiB
C++
Raw Normal View History

2023-08-04 16:28:41 +02:00
#include <cassert>
#include <algorithm>
#include <iterator>
2023-08-05 11:55:46 +02:00
namespace Hanabi {
2023-08-04 16:28:41 +02:00
2023-08-05 11:55:46 +02:00
Card &Card::operator++() {
rank++;
return *this;
2023-08-04 16:28:41 +02:00
}
2023-08-05 11:55:46 +02:00
Card Card::successor() const { return {suit, static_cast<rank_t>(rank + 1)}; }
const Card Card::operator++(int) {
Card ret = *this;
rank++;
return ret;
}
template<std::size_t num_suits>
std::ostream &operator<<(std::ostream &os, const Stacks<num_suits> &stacks) {
for (size_t i = 0; i < stacks.size() - 1; i++) {
os << +stacks[i] << ", ";
}
os << +stacks.back();
return os;
}
2023-08-05 12:19:34 +02:00
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);
}
}
}
2023-08-05 11:55:46 +02:00
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];
};
template<std::size_t num_suits>
player_t &CardPositions<num_suits>::operator[](const Card &card) {
return _card_positions[card.suit][card.rank][card.copy];
};
2023-08-05 12:19:34 +02:00
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>
2023-08-05 13:04:51 +02:00
BacktrackAction HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::clue() {
2023-08-05 11:55:46 +02:00
assert(_num_clues > 0);
--_num_clues;
incr_turn();
2023-08-05 13:04:51 +02:00
return BacktrackAction{ActionType::clue, {}, {}};
2023-08-05 11:55:46 +02:00
}
2023-08-05 12:19:34 +02:00
template<size_t num_suits, player_t num_players, size_t hand_size, uint8_t max_draw_pile_size>
2023-08-05 11:55:46 +02:00
void HanabiState<num_suits, num_players, hand_size,
max_draw_pile_size>::incr_turn() {
_turn = (_turn + 1) % num_players;
}
2023-08-05 12:19:34 +02:00
template<size_t num_suits, player_t num_players, size_t hand_size, uint8_t max_draw_pile_size>
2023-08-05 11:55:46 +02:00
void HanabiState<num_suits, num_players, hand_size,
max_draw_pile_size>::decr_turn() {
_turn = (_turn + num_players - 1) % num_players;
}
2023-08-05 12:19:34 +02:00
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size>
2023-08-05 13:04:51 +02:00
BacktrackAction HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::play(
2023-08-05 11:55:46 +02:00
std::uint8_t index) {
assert(index < _hands[_turn].size());
const Card card = _hands[_turn][index];
assert(card.rank == _stacks[card.suit] - 1);
--_stacks[card.suit];
2023-08-05 13:04:51 +02:00
BacktrackAction ret{ActionType::play, _hands[_turn][index], index};
2023-08-05 11:55:46 +02:00
if (card.rank == 0) {
// update clues if we played the last card of a stack
_num_clues++;
}
draw(index);
incr_turn();
return ret;
}
2023-08-05 12:19:34 +02:00
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size>
2023-08-05 13:04:51 +02:00
BacktrackAction HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::discard(
2023-08-05 11:55:46 +02:00
std::uint8_t index) {
assert(index < _hands[_turn].size());
assert(_num_clues != max_num_clues);
_num_clues++;
2023-08-05 13:04:51 +02:00
BacktrackAction ret{ActionType::discard, _hands[_turn][index], index};
2023-08-05 11:55:46 +02:00
draw(index);
incr_turn();
return ret;
}
2023-08-05 12:19:34 +02:00
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size>
2023-08-05 11:55:46 +02:00
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: ";
for (const auto &[card, mul]: hanabi_state._draw_pile) {
os << card;
if (mul > 1) {
os << " (" << +mul << ")";
}
}
os << std::endl;
os << "Hands: ";
for (const auto &hand: hanabi_state._hands) {
for (const auto &card: hand) {
os << card << ", ";
}
os << " | ";
}
return os;
}
2023-08-05 12:19:34 +02:00
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size>
2023-08-05 11:55:46 +02:00
void HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::draw(std::uint8_t index) {
assert(index < _hands[_turn].size());
_card_positions[_hands[_turn][index]] = trash_or_play_stack;
// draw a new card if the draw pile is not empty
if (!_draw_pile.empty()) {
--_draw_pile_size;
CardMultiplicity draw = _draw_pile.front();
_draw_pile.pop_front();
assert(draw.multiplicity > 0);
if (draw.multiplicity > 1) {
draw.multiplicity--;
_draw_pile.push_back(draw);
}
_hands[_turn][index] = draw.card;
_card_positions[draw.card] = _turn;
}
}
2023-08-05 12:19:34 +02:00
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size>
2023-08-05 11:55:46 +02:00
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());
_card_positions[_hands[_turn][index]] = draw_pile;
// put card back into draw pile (at the back)
if (!_draw_pile.empty() and _draw_pile.back().card == _hands[_turn][index]) {
2023-08-05 11:55:46 +02:00
_draw_pile.back().multiplicity++;
} else {
_draw_pile.push_back({_hands[_turn][index], 1});
}
_hands[_turn][index] = card;
_card_positions[card] = _turn;
_draw_pile_size++;
}
2023-08-05 12:19:34 +02:00
template<std::size_t num_suits, player_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size>
2023-08-05 11:55:46 +02:00
void HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::revert(
2023-08-05 13:04:51 +02:00
const BacktrackAction &action) {
2023-08-05 11:55:46 +02:00
decr_turn();
switch (action.type) {
case ActionType::clue:
assert(_num_clues < max_num_clues);
_num_clues++;
break;
case ActionType::discard:
assert(_num_clues > 0);
_num_clues--;
revert_draw(action.index, action.discarded);
break;
case ActionType::play:
if (action.discarded.rank == 0) {
_num_clues--;
}
revert_draw(action.index, action.discarded);
_stacks[action.discarded.suit]++;
2023-08-05 13:04:51 +02:00
default:
break;
2023-08-05 11:55:46 +02:00
}
}
} // namespace Hanabi