Advertisement
Kerberos177

Program 2

Sep 27th, 2020
1,331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. using namespace std;
  6.  
  7.  
  8. void DisplayCards(vector<int> &aliveCards, vector<int> &deadCards, int score)
  9. {
  10.     cout << "Current score: " << score << endl;
  11.  
  12.     cout << "Dead cards:";
  13.     for (int i = 0; i < deadCards.size(); i++)
  14.     {
  15.         cout << "   " << deadCards.at(i);
  16.     }
  17.     cout << endl;
  18.  
  19.     cout << "Live cards:";
  20.     for (int i = 0; i < aliveCards.size(); i++)
  21.     {
  22.         cout << "   " << aliveCards.at(i);
  23.     }
  24.     cout << endl;
  25. }
  26.  
  27. void RemoveCard(vector<int> &aliveCards, vector<int> &deadCards, int location)
  28. {
  29.     int smallestCard, temp;
  30.  
  31.     deadCards.push_back(aliveCards.at(location));
  32.     aliveCards.erase(aliveCards.begin() + location);
  33.  
  34.     for (int i = 0; i < deadCards.size(); i++)
  35.     {
  36.         smallestCard = i;
  37.         for (int j = 0; j < deadCards.size(); j++)
  38.         {
  39.             if (deadCards.at(j) < deadCards.at(smallestCard))
  40.             {
  41.                 smallestCard = j;
  42.             }
  43.         }
  44.         temp = deadCards.at(i);
  45.         deadCards.at(i) = deadCards.at(smallestCard);
  46.         deadCards.at(smallestCard) = temp;
  47.     }
  48. }
  49.  
  50. void PickCard(vector<int> &aliveCards, vector<int> &deadCards, int maxCard, int &currentCard)
  51. {
  52.     srand(time(NULL));
  53.  
  54.     if (aliveCards.size() > 1)
  55.     {
  56.         currentCard = rand() % (aliveCards.size() - 1);
  57.     }
  58.     else
  59.     {
  60.         currentCard = 0;
  61.     }
  62.  
  63.     if (aliveCards.at(currentCard) > maxCard)
  64.     {
  65.         cout << "Next card: " << aliveCards.at(currentCard) << endl;
  66.         cout << endl;
  67.     }
  68.     else
  69.     {
  70.         cout << "Next card: " << aliveCards.at(currentCard) << " dead" << endl;
  71.         RemoveCard(aliveCards, deadCards, currentCard);
  72.         cout << endl;
  73.     }
  74. }
  75.  
  76. int main()
  77. {
  78.     int maxCard = 0;
  79.     vector<int> aliveCards{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  80.     vector<int> deadCards;
  81.     int score = 0;
  82.     char choice;
  83.     int currCardE = 0;
  84.  
  85.     while (aliveCards.size() > 0)
  86.     {
  87.         do
  88.         {
  89.             DisplayCards(aliveCards, deadCards, score);
  90.             PickCard(aliveCards, deadCards, maxCard, currCardE);
  91.  
  92.             if (aliveCards.size() == 0)
  93.             {
  94.                 break;
  95.             }
  96.  
  97.         } while (aliveCards.at(currCardE) < maxCard);
  98.  
  99.         cout << endl;
  100.         cout << "Take it [T] or Leave it [L]? ";
  101.         cin >> choice;
  102.  
  103.         cout << endl;
  104.         choice = toupper(choice);
  105.  
  106.         switch (choice)
  107.         {
  108.         case 'T':
  109.             maxCard = aliveCards.at(currCardE);
  110.             score += aliveCards.at(currCardE);
  111.             RemoveCard(aliveCards, deadCards, currCardE);
  112.             break;
  113.  
  114.         case 'L':
  115.             RemoveCard(aliveCards, deadCards, currCardE);
  116.             break;
  117.  
  118.         default:
  119.             break;
  120.         }
  121.  
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement