/
RedResistance
/
wonders
Обзор
Документация
Войти
/
RedResistance
/
wonders
Код
Запросы
2
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
console_ui.cpp
367 строк
12 KB
Domovoi-Kuzma
T-10 new cards
09 мар 2026, 04:13
09 мар 2026, 04:13
0bd8633
Код
Авторство
О чём код?
#include <iostream> #include <iomanip> #include "console_ui.h" ConsoleUI::ConsoleUI(PlayerIndex pi) :my_pi(pi) {} std::string ConsoleUI::colorToString(Color color) { switch (color) { case Color::Green: return "green"; case Color::Red: return "red"; case Color::Yellow: return "yellow"; case Color::Blue: return "blue"; case Color::Brown: return "brown"; case Color::Grey: return "grey"; case Color::Purple: return "purple"; default: return "unknown_color"; // �������� ���� �� ������ ������������ ��������� } } std::string ConsoleUI::getCardTypeAsString(const CardViewData& cvd) { std::string result; if (cvd.victory_points > 0) return "victory"; if (cvd.military > 0) return "military"; if (cvd.has_science) return "science"; if (cvd.dollars.get() > 0) return "instant pay"; if (cvd.has_production) return "production"; if (cvd.has_discount || cvd.pay.get()) { if (cvd.has_discount) result += "discount "; if (cvd.pay.get()) result += "pay"; return result; } if (cvd.has_referring) return "for colors"; if (cvd.has_untradables) return "bonus res"; throw std::runtime_error("unknown cvd"); } std::string ConsoleUI::formatCost(const Cost& cost) { if (cost.is_zero()) { return " "; } std::string subResult; bool firstItem = true; for (Resource res : globals::allResources) { int amount = cost[res]; if (amount > 0) { if (!firstItem) { subResult += ", "; } firstItem = false; subResult += std::to_string(amount); switch (res) { case Resource::Wood: subResult += " wood"; break; case Resource::Stone: subResult += " stone"; break; case Resource::Ore: subResult += " ore"; break; case Resource::Clay: subResult += " clay"; break; case Resource::Glass: subResult += " glass"; break; case Resource::Paper: subResult += " paper"; break; case Resource::Clothes: subResult += " clothes"; break; default: break; } } } return subResult; } std::string ConsoleUI::formatProduction(const CostVariants& variants) { std::string result; bool firstVariant = true; for (const Cost& variant: variants) { if (!firstVariant) result += ", "; // ��������� �������� ������� firstVariant = false; string subResult = formatCost(variant); // ���������, ����� �� �������������� ������� ������ if (subResult.find(',') != std::string::npos) { result += "(" + subResult + ")"; } else { result += subResult; } } // ��������� ������������� ������� ������ if (result.find(',') != std::string::npos) { result = "[" + result + "]"; } return result; } std::string ConsoleUI::formatMoneyDiff(Money m) { std::string pay_str; if (m.get() > 0) { pay_str = "+" + to_string(m.get()) + "$ "; } else if (m.get() < 0) { pay_str = to_string(m.get()) + "$ "; } return pay_str; } std::string ConsoleUI::formatResourceType(const ResourceType& rt) { switch (rt) { case ResourceType::Plain: return "(wood,stone,ore,clay)"; case ResourceType::Rare: return "(glass,paper,clothes)"; } throw std::runtime_error("unknown ResourceType"); } std::string ConsoleUI::getDiscountLabel(const CardViewData& cvd) { std::string pay_str = formatMoneyDiff(cvd.pay), prefix, suffix, resourceType; switch (cvd.discount_dir) { case Direction::Left: prefix = "<<"; break; case Direction::Right: suffix = ">>"; break; case Direction::Both: prefix = "<<"; suffix = ">>"; break; } return pay_str + prefix + "$1 " + formatResourceType(cvd.resource_type) + suffix; } std::string ConsoleUI::getCardNumericParamAsString(const CardViewData& cvd) { if (cvd.victory_points > 0) return std::to_string(cvd.victory_points) + " VP"; // ����� ����� ������ if (cvd.military > 0) return std::to_string(cvd.military); // ����� ������� ����� if (cvd.has_science) { switch (cvd.science_type) { case Science::Mason: return "mason"; case Science::Script: return "script"; case Science::Gear: return "gear"; case Science::Alternative: return "alternative"; default: return "unknown_science"; } } if (cvd.dollars.get() > 0) return "+"+std::to_string(cvd.dollars.get())+"$"; // ����� ����� if (cvd.has_discount) { return getDiscountLabel(cvd); } if (cvd.has_referring) { string result; switch (cvd.referring) { case ReferringPlayers::Neighbors: result += "<>"; break; case ReferringPlayers::Self: result += "V"; break; case ReferringPlayers::Both: result += "<V>"; break; } result += colorToString(cvd.color_referring); result = "(" + result + ")"; if (cvd.money_per_color_referring.get()) { result += formatMoneyDiff(cvd.money_per_color_referring); } if (cvd.wp_per_color_referring) { result += " "+std::to_string(cvd.wp_per_color_referring)+" VP"; } return result; } if (cvd.has_production) return "adds "+ formatProduction(cvd.production_appended); // ������������ �������� if (cvd.has_untradables) { return formatResourceType(cvd.resource_type); } return "no effect"; // ���������� ������-���� ������� } void ConsoleUI::print_passing_hand(const HandChoiceView& hand) { const int NUM_COLUMNS = 6; // ���������� �������� � ������� std::vector<std::string> table; std::vector<int> colWidths(NUM_COLUMNS, 0); // ������ ��� �������� ������ �������� // ���������� ������ � ��������� ������ �������� // ��������� ������ ������ ������� ������������� ���������� table.push_back("#"); colWidths[0] = std::max(colWidths[0], static_cast<int>(table.back().length())) + 1; table.push_back("name"); colWidths[1] = std::max(colWidths[1], static_cast<int>(table.back().length())) + 1; table.push_back("building cost"); colWidths[2] = std::max(colWidths[2], static_cast<int>(table.back().length())) + 1; table.push_back("color"); colWidths[3] = std::max(colWidths[3], static_cast<int>(table.back().length())) + 1; table.push_back("effect"); colWidths[4] = std::max(colWidths[4], static_cast<int>(table.back().length())) + 1; table.push_back("effect_value"); colWidths[5] = std::max(colWidths[5], static_cast<int>(table.back().length())) + 1; // ��������������� ������ ��� ���������� ������ � ������� ������ �������� for (const auto& cvd : hand.cards) { // ���������� ������ � ��������� ������ �������� table.push_back(std::to_string(table.size() / NUM_COLUMNS - 1)); // ����� ����� colWidths[0] = std::max(colWidths[0], static_cast<int>(table.back().length()) + 1); table.push_back(cvd.name); // �������� ����� colWidths[1] = std::max(colWidths[1], static_cast<int>(table.back().length()) + 1); // ��������� ����� std::string cost_str = cvd.dollars.get() ? std::to_string(cvd.dollars.get()) + "$" : formatCost(cvd.cost); table.push_back(cost_str); colWidths[2] = std::max(colWidths[2], static_cast<int>(table.back().length()) + 1); // ���� ����� table.push_back(colorToString(cvd.color)); colWidths[3] = std::max(colWidths[3], static_cast<int>(table.back().length()) + 1); // ������ ����� table.push_back(getCardTypeAsString(cvd)); colWidths[4] = std::max(colWidths[4], static_cast<int>(table.back().length()) + 1); // �������� �������� ����� table.push_back(getCardNumericParamAsString(cvd)); colWidths[5] = std::max(colWidths[5], static_cast<int>(table.back().length()) + 1); } // ������� �������������� ������ for (size_t row = 0; row < table.size() / NUM_COLUMNS; ++row) { for (size_t col = 0; col < NUM_COLUMNS; ++col) { std::cout << std::setw(colWidths[col]) << table[row * NUM_COLUMNS + col]; } std::cout << std::endl; } } void ConsoleUI::prompt(Msg m) { switch (m) { case Msg::ChooseCard: cout << "Select card(print help to view the table)\n"; break; case Msg::ChooseUsage: cout << "Choose Usage(print help to view the table)\n"; break; case Msg::InvalidChoice: cout << "Wrong input\n"; break; default: throw std::runtime_error("unknown Msg"); } } string ConsoleUI::read_token() { string value; cin >> value; return value; } void ConsoleUI::print_usage_options(const CardUsageView& options) { const int NUM_COLUMNS = 3; std::vector<std::string> table; std::vector<int> colWidths(NUM_COLUMNS, 0); auto push = [&](int col, const std::string& s) { table.push_back(s); colWidths[col] = std::max(colWidths[col], (int)s.size() + 1); }; // header push(0, "#"); push(1, "action"); push(2, "payment"); // rows for (size_t i = 0; i < options.allowed_actions.size(); ++i) { const CardAction& a = options.allowed_actions[i]; push(0, std::to_string(i)); std::string actionStr; std::string paymentStr; switch (a.type) { case CardActionType::Sell: actionStr = "Sell"; paymentStr = "+3$"; break; case CardActionType::BuildBonus: actionStr = "Build (bonus)"; paymentStr = "free"; break; case CardActionType::BuildBuy: actionStr = "Build (buy)"; paymentStr = "1$"; break; case CardActionType::Build: actionStr = "Build"; paymentStr = formatPaymentBatch(a.required_to_pay); break; case CardActionType::BuildFree: actionStr = "BuildFree"; paymentStr = formatPaymentBatch(a.required_to_pay); break; case CardActionType::BuildWonder: actionStr = "Build wonder"; paymentStr = formatPaymentBatch(a.required_to_pay); break; } push(1, actionStr); push(2, paymentStr); } // print for (size_t row = 0; row < table.size() / NUM_COLUMNS; ++row) { for (size_t col = 0; col < NUM_COLUMNS; ++col) std::cout << std::setw(colWidths[col]) << table[row * NUM_COLUMNS + col]; std::cout << '\n'; } } std::string ConsoleUI::formatDirection(const Direction& m) { switch (m) { case Direction::Left: return "<<"; case Direction::Right: return ">>"; case Direction::Self: return "VV"; default: throw std::runtime_error("unknown Direction"); } } std::string ConsoleUI::formatPaymentBatch(const PaymentBatch& pb) { std::string s; for (const auto& tr : pb.all_transactions) { s += formatDirection(tr.whom); if (tr.money.get() > 0) s += formatMoneyDiff(tr.money); if (!tr.goods.is_zero()) s += "("+ formatCost(tr.goods)+")"; } if (s.empty()) s = "free"; return s; } void ConsoleUI::print_player_state(const PlayerStateView& psv) { cout << "player: " << psv.name; cout << endl; cout << "money: $" << psv.money.get() << endl; HandChoiceView tmp; tmp.cards = psv.buildings; print_passing_hand(tmp); } void ConsoleUI::print_game_state(const GameStateView& gsv) { string sep; for (int i=0; i<gsv.players.size(); ++i) { cout << sep; sep = "*****************\n"; if (my_pi == i) cout << "(you)" << endl; print_player_state(gsv.players[i]); } }