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 { namespace Hanabi {
inline std::string to_string(const Hanabi::Card &card);
inline std::ostream &operator<<(std::ostream &os, const Card &card); inline std::ostream &operator<<(std::ostream &os, const Card &card);

View file

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

View file

@ -1,4 +1,6 @@
#include <cstdio> #include <cstdio>
#include <numeric>
#include <ios>
#include <cmath> #include <cmath>
#include <csignal> #include <csignal>
#include <iomanip> #include <iomanip>
@ -335,8 +337,19 @@ namespace Hanabi {
if (prompt.starts_with("actions")) { if (prompt.starts_with("actions")) {
auto reasonable_actions = game->get_reasonable_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) { 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()) { if(reasonable_actions.empty()) {
std::cout << "Game is over, no actions to take." << std::endl; std::cout << "Game is over, no actions to take." << std::endl;