From 0cc069485df56955d67ffea340dd6165018af474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 26 Sep 2023 23:20:49 +0200 Subject: [PATCH] CLI: align action probabilities in output --- include/game_state.h | 1 + include/game_state.hpp | 16 ++++++++++------ src/cli_interface.cpp | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/include/game_state.h b/include/game_state.h index 01c07bc..d652b95 100644 --- a/include/game_state.h +++ b/include/game_state.h @@ -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); diff --git a/include/game_state.hpp b/include/game_state.hpp index e96f0ed..6b64ac6 100644 --- a/include/game_state.hpp +++ b/include/game_state.hpp @@ -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; } diff --git a/src/cli_interface.cpp b/src/cli_interface.cpp index b975eb3..1dbc690 100644 --- a/src/cli_interface.cpp +++ b/src/cli_interface.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -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>& 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;