// This file is part of Timmi. // // Timmi is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. #include #include #include #include #include Card string_to_card(std::string description) { Card result = {NOJOKER, NOSUIT, NORANK}; std::istringstream stream(description); char jokerchar = 0; stream >> jokerchar; switch (std::tolower(jokerchar)) { case 'j': result.joker = JOKER; break; case 'r': result.joker = REDJOKER; break; case 'b': result.joker = BLACKJOKER; break; } char suitchar = jokerchar; if (result.joker != NOJOKER) { stream >> suitchar; } switch (std::tolower(suitchar)) { case 'c': result.suit = CLUBS; break; case 's': result.suit = SPADES; break; case 'h': result.suit = HEARTS; break; case 'd': result.suit = DIAMONDS; break; default: if (result.joker != NOJOKER) { return result; // Unassigned joker } debug(CRITICAL, "Invalid suit character in string->card"); } int rankno = 0; stream >> rankno; if (rankno >= 1 && rankno <= 13) { result.rank = static_cast(rankno); } return result; } std::string card_to_string(Card card) { std::ostringstream description(""); switch (card.joker) { case JOKER: description << "J"; break; case REDJOKER: description << "R"; break; case BLACKJOKER: description << "B"; break; default: // No joker-char break; } if (!is_card(card) && is_joker(card)) { return description.str(); } switch (card.suit) { case HEARTS: description << "H"; break; case SPADES: description << "S"; break; case DIAMONDS: description << "D"; break; case CLUBS: description << "C"; break; default: debug(CRITICAL, "Invalid conversion from card to string"); } description << card.rank; return description.str(); } std::string table_to_string(const std::vector &table) { std::ostringstream result(""); int i; for (i = 0; i < table.size(); i++) { result << " " << card_to_string(table.at(i).tobeat) << ":"; if (is_card(table.at(i).beater)) { result << card_to_string(table.at(i).beater); } } if (result.str() != "") { return result.str().substr(1); // Skip initial space } else { return ""; } } std::string hand_to_string(const std::vector &hand) { std::ostringstream result(""); int i; for (i = 0; i < hand.size(); i++) { result << " " << card_to_string(hand.at(i)); } if (result.str() != "") { return result.str().substr(1); // Skip initial space } else { return ""; } } std::vector string_to_table(std::string line) { std::istringstream stream(line); std::string part(""); std::vector result; TableCard tablecard = {{NOJOKER, NOSUIT, NORANK}, {NOJOKER, NOSUIT, NORANK}}; int pos = 0; while (stream >> part) { if (part == "table") { continue; } pos = part.find(':'); tablecard.tobeat = string_to_card(part.substr(0, pos)); if (pos + 1 < part.size()) { tablecard.beater = string_to_card(part.substr(pos + 1)); } else { tablecard.beater.suit = NOSUIT; tablecard.beater.rank = NORANK; } result.push_back(tablecard); } return result; } std::vector string_to_hand(std::string line) { std::istringstream stream(line); std::string part(""); std::vector result; Card card = {NOJOKER, NOSUIT, NORANK}; while (stream >> part) { if (part == "hand") { continue; } card = string_to_card(part); result.push_back(card); } return result; }