code cleanup

This commit is contained in:
Maximilian Keßler 2023-08-06 15:04:31 +02:00
parent daa19e408f
commit 5ab7538506
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -337,7 +337,6 @@ namespace Hanabi {
template<std::size_t num_suits, player_t num_players, std::size_t hand_size>
double HanabiState<num_suits, num_players, hand_size>::backtrack(size_t depth) {
// std::cout << *this << std::endl;
if (_score == 5 * num_suits) {
return 1;
}
@ -354,32 +353,20 @@ namespace Hanabi {
// First, check for playables
for(std::uint8_t index = 0; index < hand_size; index++) {
if(is_playable(hand[index])) {
// std::cout << std::string("---------------------", depth) << "playing " << hand[index] << std::endl;
if (_draw_pile.empty()) {
auto copy = *this;
BacktrackAction action = play(index);
const double probability_for_this_play = backtrack(depth + 1);
revert(action);
if (!same_up_to_discard_permutation(*this, copy)) {
std::cout << "detected faulty backtrack" << std::endl;
std::cout << *this << std::endl;
std::cout << copy << std::endl;
ASSERT(this->_hands == copy._hands);
ASSERT(this->_draw_pile == copy._draw_pile);
ASSERT(this->_endgame_turns_left == copy._endgame_turns_left);
ASSERT(false);
}
UPDATE_PROBABILITY(probability_for_this_play);
} else {
double sum_of_probabilities = 0;
uint8_t sum_of_mults = 0;
for (size_t i = 0; i < _draw_pile.size(); i++) {
auto copy = *this;
BacktrackAction action = play(index);
sum_of_probabilities += backtrack(depth + 1) * action.multiplicity;
sum_of_mults += action.multiplicity;
revert(action);
ASSERT(same_up_to_discard_permutation(*this, copy));
ASSERT(sum_of_mults <= _weighted_draw_pile_size);
}
ASSERT(sum_of_mults == _weighted_draw_pile_size);
@ -393,40 +380,19 @@ namespace Hanabi {
if(_pace > 0) {
for(std::uint8_t index = 0; index < hand_size; index++) {
if (is_trash(hand[index])) {
// std::cout << std::string("---------------------------", depth) << "discarding " << hand[index] << std::endl;
double sum_of_probabilities = 0;
if (_draw_pile.empty()) {
auto copy = *this;
BacktrackAction action = discard(index);
const double probability_for_this_discard = backtrack(depth + 1);
revert(action);
if (!same_up_to_discard_permutation(*this, copy)) {
std::cout << "detected faulty backtrack" << std::endl;
std::cout << *this << std::endl;
std::cout << copy << std::endl;
ASSERT(this->_hands == copy._hands);
ASSERT(this->_draw_pile == copy._draw_pile);
ASSERT(this->_endgame_turns_left == copy._endgame_turns_left);
ASSERT(false);
}
UPDATE_PROBABILITY(probability_for_this_discard);
} else {
uint8_t sum_of_mults = 0;
for (size_t i = 0; i < _draw_pile.size(); i++) {
auto copy = *this;
BacktrackAction action = discard(index);
sum_of_probabilities += backtrack(depth + 1) * action.multiplicity;
sum_of_mults += action.multiplicity;
revert(action);
if (!same_up_to_discard_permutation(*this, copy)) {
std::cout << "detected faulty backtrack" << std::endl;
std::cout << *this << std::endl;
std::cout << copy << std::endl;
ASSERT(this->_hands == copy._hands);
ASSERT(this->_draw_pile == copy._draw_pile);
ASSERT(this->_endgame_turns_left == copy._endgame_turns_left);
ASSERT(false);
}
}
ASSERT(sum_of_mults == _weighted_draw_pile_size);
const double probability_discard = sum_of_probabilities / _weighted_draw_pile_size;
@ -441,12 +407,9 @@ namespace Hanabi {
// Last option is to stall
if(_num_clues > 0) {
// std::cout << std::string("--------------------", depth) << "stalling " << std::endl;
auto copy = *this;
BacktrackAction action = clue();
const double probability_stall = backtrack(depth + 1);
revert(action);
ASSERT(same_up_to_discard_permutation(*this, copy));
UPDATE_PROBABILITY(probability_stall);
}