Advertisement
avillareal98

Game Of Hearts (Text-Based)

Apr 5th, 2020
1,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.83 KB | None | 0 0
  1. #ifndef CARD_H
  2. #define CARD_H
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. enum Suit { Clubs = 1, Diamonds, Hearts, Spades };
  8.  
  9. // Card class
  10. class Card
  11. {
  12. private:
  13.     Suit suit;            // use enumerated data type for spades, hearts, diamonds, clubs
  14.     int number;           // 1 through 13 – ‘jack’ is 11, ‘queen’ is 12, ‘king’ is 13
  15.     string description;   // string which displays name of card, such as ‘Ace of Hearts’
  16. public:
  17.     Card(Suit s = Clubs, int n = 1, string d = "")
  18.     {
  19.         setSuit(s);
  20.         setNumber(n);
  21.         setDescription(d);
  22.     }
  23.     void setSuit(Suit s) { suit = s; }
  24.     Suit getSuit() { return suit; }
  25.     void setNumber(int n) { number = n; }
  26.     int getNumber() { return number; }
  27.     void setDescription(string d) { description = d; }
  28.     string getDescription() { return description; }
  29.  
  30.     bool operator<(Card& right)
  31.     {
  32.         if (suit < right.suit)
  33.             return true;
  34.         else if (suit > right.suit)
  35.             return false;
  36.         else
  37.         {
  38.             if (number == 1)
  39.                 return false;
  40.             else if (right.number == 1)
  41.                 return true;
  42.             else
  43.                 return (number < right.number) ? true : false;
  44.         }
  45.     }
  46. };
  47.  
  48. #endif
  49.  
  50.  
  51. #ifndef DECK_H
  52. #define DECK_H
  53. #include "card.h"
  54. #include <iostream>
  55. #include <iomanip>
  56. #include <cstdlib>
  57. #include <ctime>
  58. #include <vector>
  59. using namespace std;
  60.  
  61. // Deck class
  62. class Deck
  63. {
  64. public:
  65.     // Member variables
  66.     Card deck[52], h1[13], h2[13], h3[13], h4[13], card;
  67.     vector<Card> d;
  68.  
  69.     Card getCard() { return card; }
  70.  
  71.     // initialize function
  72.     void initialize()
  73.     {
  74.         string setN, setS, setD;
  75.         Suit s;
  76.  
  77.         int count = 0;
  78.         for (int ss = 1; ss <= 4; ss++)
  79.         {
  80.             if (ss == 1) { s = Spades; setS = "Spades"; }
  81.             else if (ss == 2) { s = Hearts; setS = "Hearts"; }
  82.             else if (ss == 3) { s = Diamonds; setS = "Diamonds"; }
  83.             else if (ss == 4) { s = Clubs; setS = "Clubs"; }
  84.  
  85.             for (int i = 1; i <= 13; i++)
  86.             {
  87.                 if (i == 1) { setN = "Ace"; }
  88.                 else if (i == 11) { setN = "Jack"; }
  89.                 else if (i == 12) { setN = "Queen"; }
  90.                 else if (i == 13) { setN = "King"; }
  91.                 else { setN = to_string(i); }
  92.  
  93.                 setD = setS + " " + setN;
  94.  
  95.                 deck[count].setSuit(s);
  96.                 deck[count].setNumber(i);
  97.                 deck[count].setDescription(setD);
  98.                 count++;
  99.             }
  100.         }
  101.     }
  102.  
  103.     // shuffle function
  104.     void shuffle()
  105.     {
  106.         Card temp;
  107.         srand(time(0));
  108.  
  109.         for (int i = 0; i < 52; i++)
  110.         {
  111.             int n = rand() % 52;
  112.             temp = deck[i];
  113.             deck[i] = deck[n];
  114.             deck[n] = temp;
  115.         }
  116.     }
  117.  
  118.     // deal function (deals cards to 4 hands)
  119.     void deal()
  120.     {
  121.         for (int i = 0; i < 13; i++)
  122.         {
  123.             h1[i] = deck[i];
  124.             h2[i] = deck[i + 13];
  125.             h3[i] = deck[i + 26];
  126.             h4[i] = deck[i + 39];
  127.         }
  128.     }
  129.  
  130.     // sort function
  131.     void sort()
  132.     {
  133.         Card temp;
  134.         bool swap;
  135.  
  136.         do
  137.         {
  138.             swap = false;
  139.             for (int c = 0; c < 12; c++)
  140.             {
  141.                 if (h1[c].operator <(h1[c + 1]))
  142.                 {
  143.                     temp = h1[c];
  144.                     h1[c] = h1[c + 1];
  145.                     h1[c + 1] = temp;
  146.                     swap = true;
  147.                 }
  148.                 if (h2[c].operator <(h2[c + 1]))
  149.                 {
  150.                     temp = h2[c];
  151.                     h2[c] = h2[c + 1];
  152.                     h2[c + 1] = temp;
  153.                     swap = true;
  154.                 }
  155.                 if (h3[c].operator <(h3[c + 1]))
  156.                 {
  157.                     temp = h3[c];
  158.                     h3[c] = h3[c + 1];
  159.                     h3[c + 1] = temp;
  160.                     swap = true;
  161.                 }
  162.                 if (h4[c].operator <(h4[c + 1]))
  163.                 {
  164.                     temp = h4[c];
  165.                     h4[c] = h4[c + 1];
  166.                     h4[c + 1] = temp;
  167.                     swap = true;
  168.                 }
  169.             }
  170.         } while (swap);
  171.     }
  172.  
  173.     // display function (displays 4 hands)
  174.     void display()
  175.     {
  176.         cout << setw(30) << "HAND 1" << right << setw(30) << "HAND 2" << setw(30) << "HAND 3" << setw(30) << "HAND 4\n";
  177.         cout << setw(30) << "------" << right << setw(30) << "------" << setw(30) << "------" << setw(30) << "------\n";
  178.         for (int i = 0; i < 13; i++)
  179.         {
  180.             cout << setw(30) << h1[i].getDescription()
  181.                 << right << setw(30) << h2[i].getDescription()
  182.                 << right << setw(30) << h3[i].getDescription()
  183.                 << setw(30) << h4[i].getDescription() << right;
  184.         }
  185.         cout << endl;
  186.     }
  187. };
  188. #endif
  189.  
  190.  
  191. #ifndef PLAYER_H
  192. #define PLAYER_H
  193.  
  194. #include "deck.h"
  195. #include <iostream>
  196. #include <iomanip>
  197. #include <string>
  198. using namespace std;
  199.  
  200. // Player class
  201. class Player
  202. {
  203. public:
  204.     Card hand[13];    // stores cards dealt to player
  205.     bool played[13];  // played[i] is true if hand[i] has been played, otherwise false
  206.     int points;       // total number of points player has accumulated so far
  207.     string name;      // name of player
  208.  
  209.     Deck d;
  210.     int j;
  211.    
  212.     // constructor
  213.     Player(string n = "") : name(n) {
  214.         points = 0;
  215.         j = 0;
  216.         for (int i = 0; i < 13; i++)
  217.             played[i] = false;
  218.     }
  219.  
  220.     // additional member functions
  221.     void countPoints() {
  222.         for (int i = 0; i < 13; i++) {
  223.             if (hand[i].getSuit() == Spades && hand[i].getNumber() == 12) {
  224.                 points += 13;
  225.             }
  226.             if (hand[i].getSuit() == Hearts) {
  227.                 points++;
  228.             }
  229.         }
  230.     }
  231.     void resetPoints() { points = 0; }
  232.  
  233.     // sorts hand into order using the < operator of Card
  234.     void sortHand() {
  235.         int start, minI;
  236.         Card minV;
  237.         for (start = 0; start < 12; start++) {
  238.             minI = start;
  239.             minV = hand[start];
  240.             for (int i = start + 1; i < 13; i++) {
  241.                 if (hand[i] < minV) {
  242.                     minV = hand[i];
  243.                     minI = i;
  244.                 }
  245.             }
  246.             hand[minI] = hand[start];
  247.             hand[start] = minV;
  248.         }
  249.     }
  250.     // displays descriptions of cards in hand on console
  251.     void displayHand() {
  252.         cout << endl;
  253.         for (int i = 0; i < 13; i++) {
  254.             cout << (i + 1) << ": ";
  255.             if (played[i])
  256.                 cout << endl;
  257.             if (!played[i])
  258.                 cout << hand[i].getDescription() << endl;
  259.         }
  260.     }
  261.     // adds a card to hand during deal
  262.     void addCard(Card c) {
  263.         if(j < 13) {
  264.             hand[j] = c;
  265.             j++;
  266.         }
  267.     }
  268.     // returns the value of card played  – if leader is false, card must be in the lead suit
  269.     Card playCard(Suit lead, bool leader) {
  270.         Card result;
  271.         result.setDescription("");
  272.         result.setNumber(0);
  273.         result.setSuit(Clubs);
  274.  
  275.         if (leader) {
  276.             for (int i = 0; i < 13; i++) {
  277.                 if (hand[i].getSuit() == Clubs && hand[i].getNumber() == 2) {
  278.                     played[i] = true;
  279.                     return hand[i];
  280.                 }
  281.             }
  282.         }
  283.         if (leader == false) {
  284.             for (int i = 0; i < 13; i++) {
  285.                 if (hand[i].getSuit() == Hearts && played[i] == false) {
  286.                     played[i] = true;
  287.                     return hand[i];
  288.                 }
  289.             }
  290.         }
  291.         for (int i = 0; i < 13; i++) {
  292.             if (!played[i]) {
  293.                 played[i] = true;
  294.                 return hand[i];
  295.             }
  296.         }
  297.         return result;
  298.     }
  299.     Card playCard(int n) {
  300.         played[n - 1] = true;
  301.         return hand[n - 1];
  302.     }
  303.  
  304.     // returns location of Club Two in hand, or -1 if not found
  305.     int findClubTwo() {
  306.         int location = -1;
  307.         bool found = false;
  308.         for (int i = 0; i < 13 && !found; i++) {
  309.             if (hand[i].getSuit() == Clubs && hand[i].getNumber() == 2) {
  310.                 found = true;
  311.                 location = i;
  312.             }
  313.         }
  314.         return location;
  315.     }
  316.  
  317. };
  318.  
  319.  
  320.  
  321. #endif
  322.  
  323.  
  324. #include <iostream>
  325. #include <iomanip>
  326. #include "card.h"
  327. #include "deck.h"
  328. #include "player.h"
  329. using namespace std;
  330.  
  331. /*
  332. Description: This program will play the card game of hearts and should build on your code for
  333. homework 3, including your overloaded < operator, sort function, and shuffle function and add
  334. members to your Card and Deck classes from Homework 3, as needed.
  335. */
  336.  
  337. int main()
  338. {
  339.     Player p[4] = { Player("Me"),
  340.                     Player("Snoop Dogg"),
  341.                     Player("Lady Gaga"),
  342.                     Player("Elton John") };
  343.     Deck d;
  344.  
  345.     d.initialize();
  346.     d.shuffle();
  347.  
  348.     for (int i = 0; i < 13; i++) {
  349.         for (int j = 0; j < 4; j++) {
  350.             p[j].addCard(d.deck[j * 13 + i]);
  351.         }
  352.     }
  353.  
  354.     cout << "\t\tPoints\tPoints" << endl;
  355.     cout << "Player\t\tRound 1\tTotal" << endl;
  356.     cout << "------\t\t-------\t-----" << endl;
  357.     cout << "Round 1\n-------\n";
  358.     for (int i = 0; i < 4; i++)
  359.         cout << p[i].name << "\t0\t0" << endl;
  360.     cout << endl;
  361.    
  362.     p[0].sortHand();
  363.     p[0].displayHand();
  364.  
  365.     int leader = 0;
  366.     int cardChoice = 0;
  367.     if (p[0].findClubTwo() != -1) {
  368.         cout << p[0].name << ": " << p[0].playCard(Clubs, true).getDescription() << endl;
  369.         cout << p[1].name << ": " << p[1].playCard(Clubs, false).getDescription() << endl;
  370.         cout << p[2].name << ": " << p[2].playCard(Clubs, false).getDescription() << endl;
  371.         cout << "Your play? ";
  372.         cin >> cardChoice;
  373.         cout << p[3].name << ": " << p[3].playCard(cardChoice).getDescription() << endl;
  374.         leader = 0;
  375.     }
  376.     if (p[1].findClubTwo() != -1) {
  377.         cout << p[1].name << ": " << p[0].playCard(Clubs, true).getDescription() << endl;
  378.         cout << p[2].name << ": " << p[1].playCard(Clubs, false).getDescription() << endl;
  379.         cout << p[0].name << ": " << p[2].playCard(Clubs, false).getDescription() << endl;
  380.         cout << "Your play? ";
  381.         cin >> cardChoice;
  382.         cout << p[3].name << ": " << p[3].playCard(cardChoice).getDescription() << endl;
  383.         leader = 1;
  384.     }
  385.     if (p[2].findClubTwo() != -1) {
  386.         cout << p[2].name << ": " << p[0].playCard(Clubs, true).getDescription() << endl;
  387.         cout << p[0].name << ": " << p[1].playCard(Clubs, false).getDescription() << endl;
  388.         cout << p[1].name << ": " << p[2].playCard(Clubs, false).getDescription() << endl;
  389.         cout << "Your play? ";
  390.         cin >> cardChoice;
  391.         cout << p[3].name << ": " << p[3].playCard(cardChoice).getDescription() << endl;
  392.         leader = 2;
  393.     }
  394.     if (p[3].findClubTwo() != -1) {
  395.         cout << p[3].name << ": " << p[3].playCard(p[3].findClubTwo()).getDescription() << endl;
  396.         cout << p[2].name << ": " << p[0].playCard(Clubs, false).getDescription() << endl;
  397.         cout << p[0].name << ": " << p[1].playCard(Clubs, false).getDescription() << endl;
  398.         cout << p[1].name << ": " << p[2].playCard(Clubs, false).getDescription() << endl;
  399.         leader = 3;
  400.     }
  401.  
  402.     for (int j = 2; j <= 13; j++) {
  403.         cout << "\t\tPoints\tPoints" << endl;
  404.         cout << "Player\tRound " << j << "\tTotal" << endl;
  405.         cout << "------\t-------\t-----" << endl;
  406.         for (int i = 0; i < 4; i++)
  407.             cout << p[i].name << "\t" << p[i].points << "\t" << p[i].points << endl;
  408.         cout << endl;
  409.         cout << "My Hand" << endl;
  410.         p[3].displayHand();
  411.         cout << endl;
  412.  
  413.         cout << "Round " << j << "\n-------\n";
  414.         Card max = p[leader].playCard(Clubs, true);
  415.         cout << p[leader].name + ": " + max.getDescription() << endl;
  416.  
  417.         Card next = p[(leader + 1) % 4].playCard(Clubs, false);
  418.         cout << p[(leader + 1) % 4].name + ": " + next.getDescription() << endl;
  419.  
  420.         Card nextPlayer = p[(leader + 2) % 4].playCard(Clubs, false);
  421.         cout << p[(leader + 2) % 4].name + ": " + nextPlayer.getDescription() << endl;
  422.  
  423.         cout << "Your play? ";
  424.         cin >> cardChoice;
  425.  
  426.         if (next.getSuit() == max.getSuit()) {
  427.             if (next.getNumber() > max.getNumber()) {
  428.                 max = next;
  429.             }
  430.         }
  431.  
  432.     }
  433.  
  434.  
  435.     cin.get();
  436.     cout << "Press any key to continue..." << endl;
  437.     cin.get();
  438.     return 0;
  439. }
  440.  
  441.  
  442. Output:
  443.                 Points  Points
  444. Player          Round 1 Total
  445. ------          ------- -----
  446. Round 1
  447. -------
  448. Me      0       0
  449. Snoop Dogg      0       0
  450. Lady Gaga       0       0
  451. Elton John      0       0
  452.  
  453.  
  454. 1: Clubs 9
  455. 2: Clubs 10
  456. 3: Clubs Queen
  457. 4: Diamonds 6
  458. 5: Diamonds 10
  459. 6: Diamonds Jack
  460. 7: Diamonds Ace
  461. 8: Hearts 5
  462. 9: Hearts 6
  463. 10: Hearts 9
  464. 11: Hearts 10
  465. 12: Hearts Ace
  466. 13: Spades Ace
  467. Snoop Dogg: Clubs 9
  468. Lady Gaga: Hearts 8
  469. Me: Hearts King
  470. Your play? 4
  471. Elton John: Hearts 2
  472.                 Points  Points
  473. Player  Round 2 Total
  474. ------  ------- -----
  475. Me      0       0
  476. Snoop Dogg      0       0
  477. Lady Gaga       0       0
  478. Elton John      0       0
  479.  
  480. My Hand
  481.  
  482. 1: Diamonds 2
  483. 2: Clubs King
  484. 3: Clubs Jack
  485. 4:
  486. 5: Spades Queen
  487. 6: Diamonds Queen
  488. 7: Clubs Ace
  489. 8: Hearts Queen
  490. 9: Hearts 3
  491. 10: Diamonds 9
  492. 11: Spades Jack
  493. 12: Hearts 7
  494. 13: Spades 7
  495.  
  496. Round 2
  497. -------
  498. Snoop Dogg: Clubs 2
  499. Lady Gaga: Diamonds 8
  500. Elton John: Hearts Queen
  501. Your play? 3
  502.                 Points  Points
  503. Player  Round 3 Total
  504. ------  ------- -----
  505. Me      0       0
  506. Snoop Dogg      0       0
  507. Lady Gaga       0       0
  508. Elton John      0       0
  509.  
  510. My Hand
  511.  
  512. 1: Diamonds 2
  513. 2: Clubs King
  514. 3: Clubs Jack
  515. 4:
  516. 5: Spades Queen
  517. 6: Diamonds Queen
  518. 7: Clubs Ace
  519. 8:
  520. 9: Hearts 3
  521. 10: Diamonds 9
  522. 11: Spades Jack
  523. 12: Hearts 7
  524. 13: Spades 7
  525.  
  526. Round 3
  527. -------
  528. Snoop Dogg: Clubs 2
  529. Lady Gaga: Spades 10
  530. Elton John: Hearts 3
  531. Your play?
  532. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement