fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. // ANSI color codes
  8. #define RED "\033[31m"
  9. #define GREEN "\033[32m"
  10. #define YELLOW "\033[33m"
  11. #define BLUE "\033[34m"
  12. #define MAGENTA "\033[35m"
  13. #define RESET "\033[0m"
  14.  
  15. vector<string> generateDeck()
  16. {
  17. vector<string> deck;
  18. char types[] = {'A', 'B', 'C', 'D', 'E'};
  19. for (char type : types)
  20. {
  21. for (int i = 1; i <= 12; i++)
  22. {
  23. deck.push_back(string(1, type) + to_string(i));
  24. }
  25. }
  26. random_shuffle(deck.begin(), deck.end());
  27. return deck;
  28. }
  29.  
  30. int getCardValue(string card)
  31. {
  32. return stoi(card.substr(1));
  33. }
  34.  
  35. char getCardType(string card)
  36. {
  37. return card[0];
  38. }
  39.  
  40. string getColoredCard(string card)
  41. {
  42. char type = getCardType(card);
  43. string color;
  44. switch (type)
  45. {
  46. case 'A':
  47. color = RED;
  48. break;
  49. case 'B':
  50. color = GREEN;
  51. break;
  52. case 'C':
  53. color = YELLOW;
  54. break;
  55. case 'D':
  56. color = BLUE;
  57. break;
  58. case 'E':
  59. color = MAGENTA;
  60. break;
  61. default:
  62. color = RESET;
  63. }
  64. return color + card + RESET;
  65. }
  66.  
  67. bool hasType(const vector<string> &player, char type)
  68. {
  69. for (const auto &card : player)
  70. {
  71. if (getCardType(card) == type)
  72. return true;
  73. }
  74. return false;
  75. }
  76.  
  77. int main()
  78. {
  79. srand(time(0));
  80. vector<string> deck = generateDeck();
  81.  
  82. vector<string> player(deck.begin(), deck.begin() + 6);
  83. vector<string> ai(deck.begin() + 6, deck.begin() + 12);
  84.  
  85. int playerPoints = 0, aiPoints = 0;
  86.  
  87. for (int i = 0; i < 6; i++)
  88. {
  89. cout << "\nYour remaining cards: ";
  90. for (auto card : player)
  91. cout << getColoredCard(card) << " ";
  92. cout << "\n";
  93.  
  94. cout << "\nRound " << i + 1 << "\n";
  95. cout << "AI plays: " << getColoredCard(ai[i]) << "\n";
  96.  
  97. char aiType = getCardType(ai[i]);
  98. int aiValue = getCardValue(ai[i]);
  99.  
  100. cout << "Choose a card to play";
  101. if (hasType(player, aiType))
  102. cout << " of type " << aiType;
  103. cout << ": ";
  104.  
  105. string chosenCard;
  106. bool validChoice = false;
  107.  
  108. while (!validChoice)
  109. {
  110. cin >> chosenCard;
  111. auto it = find(player.begin(), player.end(), chosenCard);
  112.  
  113. if (it != player.end())
  114. {
  115. if (getCardType(chosenCard) == aiType || !hasType(player, aiType))
  116. {
  117. validChoice = true;
  118. if (getCardValue(chosenCard) > aiValue)
  119. {
  120. cout << "You win this round!\n";
  121. playerPoints++;
  122. }
  123. else
  124. {
  125. cout << "AI wins this round!\n";
  126. aiPoints++;
  127. }
  128. player.erase(it);
  129. }
  130. else
  131. {
  132. cout << "Invalid choice. Pick a valid " << aiType << " card: ";
  133. }
  134. }
  135. else
  136. {
  137. cout << "Invalid choice. Choose a card from your deck: ";
  138. }
  139. }
  140. }
  141.  
  142. cout << "\nGame Over!\n";
  143. cout << "Your Points: " << playerPoints << "\n";
  144. cout << "AI Points: " << aiPoints << "\n";
  145.  
  146. if (playerPoints > aiPoints)
  147. cout << "You Win!\n";
  148. else if (playerPoints < aiPoints)
  149. cout << "AI Wins!\n";
  150. else
  151. cout << "It's a Tie!\n";
  152.  
  153. return 0;
  154. }
  155.  
Success #stdin #stdout 0.03s 25640KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

// ANSI color codes
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define RESET "\033[0m"

vector<string> generateDeck()
{
    vector<string> deck;
    char types[] = {'A', 'B', 'C', 'D', 'E'};
    for (char type : types)
    {
        for (int i = 1; i <= 12; i++)
        {
            deck.push_back(string(1, type) + to_string(i));
        }
    }
    random_shuffle(deck.begin(), deck.end());
    return deck;
}

int getCardValue(string card)
{
    return stoi(card.substr(1));
}

char getCardType(string card)
{
    return card[0];
}

string getColoredCard(string card)
{
    char type = getCardType(card);
    string color;
    switch (type)
    {
    case 'A':
        color = RED;
        break;
    case 'B':
        color = GREEN;
        break;
    case 'C':
        color = YELLOW;
        break;
    case 'D':
        color = BLUE;
        break;
    case 'E':
        color = MAGENTA;
        break;
    default:
        color = RESET;
    }
    return color + card + RESET;
}

bool hasType(const vector<string> &player, char type)
{
    for (const auto &card : player)
    {
        if (getCardType(card) == type)
            return true;
    }
    return false;
}

int main()
{
    srand(time(0));
    vector<string> deck = generateDeck();

    vector<string> player(deck.begin(), deck.begin() + 6);
    vector<string> ai(deck.begin() + 6, deck.begin() + 12);

    int playerPoints = 0, aiPoints = 0;

    for (int i = 0; i < 6; i++)
    {
        cout << "\nYour remaining cards: ";
        for (auto card : player)
            cout << getColoredCard(card) << " ";
        cout << "\n";

        cout << "\nRound " << i + 1 << "\n";
        cout << "AI plays: " << getColoredCard(ai[i]) << "\n";

        char aiType = getCardType(ai[i]);
        int aiValue = getCardValue(ai[i]);

        cout << "Choose a card to play";
        if (hasType(player, aiType))
            cout << " of type " << aiType;
        cout << ": ";

        string chosenCard;
        bool validChoice = false;

        while (!validChoice)
        {
            cin >> chosenCard;
            auto it = find(player.begin(), player.end(), chosenCard);

            if (it != player.end())
            {
                if (getCardType(chosenCard) == aiType || !hasType(player, aiType))
                {
                    validChoice = true;
                    if (getCardValue(chosenCard) > aiValue)
                    {
                        cout << "You win this round!\n";
                        playerPoints++;
                    }
                    else
                    {
                        cout << "AI wins this round!\n";
                        aiPoints++;
                    }
                    player.erase(it);
                }
                else
                {
                    cout << "Invalid choice. Pick a valid " << aiType << " card: ";
                }
            }
            else
            {
                cout << "Invalid choice. Choose a card from your deck: ";
            }
        }
    }

    cout << "\nGame Over!\n";
    cout << "Your Points: " << playerPoints << "\n";
    cout << "AI Points: " << aiPoints << "\n";

    if (playerPoints > aiPoints)
        cout << "You Win!\n";
    else if (playerPoints < aiPoints)
        cout << "AI Wins!\n";
    else
        cout << "It's a Tie!\n";

    return 0;
}