CLI: align action probabilities in output

This commit is contained in:
Maximilian Keßler 2023-09-26 23:20:49 +02:00
parent 3fd64bc70f
commit 0cc069485d
Signed by: max
GPG Key ID: BCC5A619923C0BA5
3 changed files with 25 additions and 7 deletions

View File

@ -108,6 +108,7 @@ namespace std {
namespace Hanabi {
inline std::string to_string(const Hanabi::Card &card);
inline std::ostream &operator<<(std::ostream &os, const Card &card);

View File

@ -10,10 +10,18 @@ namespace Hanabi {
return os;
}
std::string to_string(const Hanabi::Card &card) {
if (card == Hanabi::Cards::trash) {
return "kt";
} else {
return Hanabi::suit_initials[card.suit] + std::to_string(5 - card.rank);
}
}
std::ostream &operator<<(std::ostream &os, Action const& action) {
switch(action.type) {
case ActionType::play:
os << "play " << action.card;
os << "play " + to_string(action.card);
break;
case ActionType::discard:
os << "discard";
@ -32,11 +40,7 @@ namespace Hanabi {
}
std::ostream &operator<<(std::ostream &os, const Card &card) {
if (card == Cards::trash) {
os << "kt";
} else {
os << suit_initials[card.suit] << 5 - card.rank;
}
os << to_string(card);
return os;
}

View File

@ -1,4 +1,6 @@
#include <cstdio>
#include <numeric>
#include <ios>
#include <cmath>
#include <csignal>
#include <iomanip>
@ -335,8 +337,19 @@ namespace Hanabi {
if (prompt.starts_with("actions")) {
auto reasonable_actions = game->get_reasonable_actions();
int max_rational_digit_len = std::accumulate(
reasonable_actions.begin(),
reasonable_actions.end(),
0,
[](int old, const std::pair<Action, std::optional<probability_t>>& pair){
return std::max(old, representation_length(pair.second.value_or(0)));
}
);
for (const auto &[action, probability] : reasonable_actions) {
std::cout << action << ": " << probability << std::endl;
std::cout.setf(std::ios_base::left, std::ios_base::adjustfield);
std::cout << std::setw(7) << action << ": ";
std::cout.setf(std::ios_base::right, std::ios_base::adjustfield);
std::cout << std::setw(max_rational_digit_len) << probability << std::endl;
}
if(reasonable_actions.empty()) {
std::cout << "Game is over, no actions to take." << std::endl;