Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef CARD_H
- #define CARD_H
- #include <iostream>
- #include <string>
- using namespace std;
- enum Suit { Clubs = 1, Diamonds, Hearts, Spades };
- // Card class
- class Card
- {
- private:
- Suit suit; // use enumerated data type for spades, hearts, diamonds, clubs
- int number; // 1 through 13 – ‘jack’ is 11, ‘queen’ is 12, ‘king’ is 13
- string description; // string which displays name of card, such as ‘Ace of Hearts’
- public:
- Card(Suit s = Clubs, int n = 1, string d = "")
- {
- setSuit(s);
- setNumber(n);
- setDescription(d);
- }
- void setSuit(Suit s) { suit = s; }
- Suit getSuit() { return suit; }
- void setNumber(int n) { number = n; }
- int getNumber() { return number; }
- void setDescription(string d) { description = d; }
- string getDescription() { return description; }
- bool operator<(Card& right)
- {
- if (suit < right.suit)
- return true;
- else if (suit > right.suit)
- return false;
- else
- {
- if (number == 1)
- return false;
- else if (right.number == 1)
- return true;
- else
- return (number < right.number) ? true : false;
- }
- }
- };
- #endif
- #ifndef DECK_H
- #define DECK_H
- #include "card.h"
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <ctime>
- #include <vector>
- using namespace std;
- // Deck class
- class Deck
- {
- public:
- // Member variables
- Card deck[52], h1[13], h2[13], h3[13], h4[13], card;
- vector<Card> d;
- Card getCard() { return card; }
- // initialize function
- void initialize()
- {
- string setN, setS, setD;
- Suit s;
- int count = 0;
- for (int ss = 1; ss <= 4; ss++)
- {
- if (ss == 1) { s = Spades; setS = "Spades"; }
- else if (ss == 2) { s = Hearts; setS = "Hearts"; }
- else if (ss == 3) { s = Diamonds; setS = "Diamonds"; }
- else if (ss == 4) { s = Clubs; setS = "Clubs"; }
- for (int i = 1; i <= 13; i++)
- {
- if (i == 1) { setN = "Ace"; }
- else if (i == 11) { setN = "Jack"; }
- else if (i == 12) { setN = "Queen"; }
- else if (i == 13) { setN = "King"; }
- else { setN = to_string(i); }
- setD = setS + " " + setN;
- deck[count].setSuit(s);
- deck[count].setNumber(i);
- deck[count].setDescription(setD);
- count++;
- }
- }
- }
- // shuffle function
- void shuffle()
- {
- Card temp;
- srand(time(0));
- for (int i = 0; i < 52; i++)
- {
- int n = rand() % 52;
- temp = deck[i];
- deck[i] = deck[n];
- deck[n] = temp;
- }
- }
- // deal function (deals cards to 4 hands)
- void deal()
- {
- for (int i = 0; i < 13; i++)
- {
- h1[i] = deck[i];
- h2[i] = deck[i + 13];
- h3[i] = deck[i + 26];
- h4[i] = deck[i + 39];
- }
- }
- // sort function
- void sort()
- {
- Card temp;
- bool swap;
- do
- {
- swap = false;
- for (int c = 0; c < 12; c++)
- {
- if (h1[c].operator <(h1[c + 1]))
- {
- temp = h1[c];
- h1[c] = h1[c + 1];
- h1[c + 1] = temp;
- swap = true;
- }
- if (h2[c].operator <(h2[c + 1]))
- {
- temp = h2[c];
- h2[c] = h2[c + 1];
- h2[c + 1] = temp;
- swap = true;
- }
- if (h3[c].operator <(h3[c + 1]))
- {
- temp = h3[c];
- h3[c] = h3[c + 1];
- h3[c + 1] = temp;
- swap = true;
- }
- if (h4[c].operator <(h4[c + 1]))
- {
- temp = h4[c];
- h4[c] = h4[c + 1];
- h4[c + 1] = temp;
- swap = true;
- }
- }
- } while (swap);
- }
- // display function (displays 4 hands)
- void display()
- {
- cout << setw(30) << "HAND 1" << right << setw(30) << "HAND 2" << setw(30) << "HAND 3" << setw(30) << "HAND 4\n";
- cout << setw(30) << "------" << right << setw(30) << "------" << setw(30) << "------" << setw(30) << "------\n";
- for (int i = 0; i < 13; i++)
- {
- cout << setw(30) << h1[i].getDescription()
- << right << setw(30) << h2[i].getDescription()
- << right << setw(30) << h3[i].getDescription()
- << setw(30) << h4[i].getDescription() << right;
- }
- cout << endl;
- }
- };
- #endif
- #ifndef PLAYER_H
- #define PLAYER_H
- #include "deck.h"
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- // Player class
- class Player
- {
- public:
- Card hand[13]; // stores cards dealt to player
- bool played[13]; // played[i] is true if hand[i] has been played, otherwise false
- int points; // total number of points player has accumulated so far
- string name; // name of player
- Deck d;
- int j;
- // constructor
- Player(string n = "") : name(n) {
- points = 0;
- j = 0;
- for (int i = 0; i < 13; i++)
- played[i] = false;
- }
- // additional member functions
- void countPoints() {
- for (int i = 0; i < 13; i++) {
- if (hand[i].getSuit() == Spades && hand[i].getNumber() == 12) {
- points += 13;
- }
- if (hand[i].getSuit() == Hearts) {
- points++;
- }
- }
- }
- void resetPoints() { points = 0; }
- // sorts hand into order using the < operator of Card
- void sortHand() {
- int start, minI;
- Card minV;
- for (start = 0; start < 12; start++) {
- minI = start;
- minV = hand[start];
- for (int i = start + 1; i < 13; i++) {
- if (hand[i] < minV) {
- minV = hand[i];
- minI = i;
- }
- }
- hand[minI] = hand[start];
- hand[start] = minV;
- }
- }
- // displays descriptions of cards in hand on console
- void displayHand() {
- cout << endl;
- for (int i = 0; i < 13; i++) {
- cout << (i + 1) << ": ";
- if (played[i])
- cout << endl;
- if (!played[i])
- cout << hand[i].getDescription() << endl;
- }
- }
- // adds a card to hand during deal
- void addCard(Card c) {
- if(j < 13) {
- hand[j] = c;
- j++;
- }
- }
- // returns the value of card played – if leader is false, card must be in the lead suit
- Card playCard(Suit lead, bool leader) {
- Card result;
- result.setDescription("");
- result.setNumber(0);
- result.setSuit(Clubs);
- if (leader) {
- for (int i = 0; i < 13; i++) {
- if (hand[i].getSuit() == Clubs && hand[i].getNumber() == 2) {
- played[i] = true;
- return hand[i];
- }
- }
- }
- if (leader == false) {
- for (int i = 0; i < 13; i++) {
- if (hand[i].getSuit() == Hearts && played[i] == false) {
- played[i] = true;
- return hand[i];
- }
- }
- }
- for (int i = 0; i < 13; i++) {
- if (!played[i]) {
- played[i] = true;
- return hand[i];
- }
- }
- return result;
- }
- Card playCard(int n) {
- played[n - 1] = true;
- return hand[n - 1];
- }
- // returns location of Club Two in hand, or -1 if not found
- int findClubTwo() {
- int location = -1;
- bool found = false;
- for (int i = 0; i < 13 && !found; i++) {
- if (hand[i].getSuit() == Clubs && hand[i].getNumber() == 2) {
- found = true;
- location = i;
- }
- }
- return location;
- }
- };
- #endif
- #include <iostream>
- #include <iomanip>
- #include "card.h"
- #include "deck.h"
- #include "player.h"
- using namespace std;
- /*
- Description: This program will play the card game of hearts and should build on your code for
- homework 3, including your overloaded < operator, sort function, and shuffle function and add
- members to your Card and Deck classes from Homework 3, as needed.
- */
- int main()
- {
- Player p[4] = { Player("Me"),
- Player("Snoop Dogg"),
- Player("Lady Gaga"),
- Player("Elton John") };
- Deck d;
- d.initialize();
- d.shuffle();
- for (int i = 0; i < 13; i++) {
- for (int j = 0; j < 4; j++) {
- p[j].addCard(d.deck[j * 13 + i]);
- }
- }
- cout << "\t\tPoints\tPoints" << endl;
- cout << "Player\t\tRound 1\tTotal" << endl;
- cout << "------\t\t-------\t-----" << endl;
- cout << "Round 1\n-------\n";
- for (int i = 0; i < 4; i++)
- cout << p[i].name << "\t0\t0" << endl;
- cout << endl;
- p[0].sortHand();
- p[0].displayHand();
- int leader = 0;
- int cardChoice = 0;
- if (p[0].findClubTwo() != -1) {
- cout << p[0].name << ": " << p[0].playCard(Clubs, true).getDescription() << endl;
- cout << p[1].name << ": " << p[1].playCard(Clubs, false).getDescription() << endl;
- cout << p[2].name << ": " << p[2].playCard(Clubs, false).getDescription() << endl;
- cout << "Your play? ";
- cin >> cardChoice;
- cout << p[3].name << ": " << p[3].playCard(cardChoice).getDescription() << endl;
- leader = 0;
- }
- if (p[1].findClubTwo() != -1) {
- cout << p[1].name << ": " << p[0].playCard(Clubs, true).getDescription() << endl;
- cout << p[2].name << ": " << p[1].playCard(Clubs, false).getDescription() << endl;
- cout << p[0].name << ": " << p[2].playCard(Clubs, false).getDescription() << endl;
- cout << "Your play? ";
- cin >> cardChoice;
- cout << p[3].name << ": " << p[3].playCard(cardChoice).getDescription() << endl;
- leader = 1;
- }
- if (p[2].findClubTwo() != -1) {
- cout << p[2].name << ": " << p[0].playCard(Clubs, true).getDescription() << endl;
- cout << p[0].name << ": " << p[1].playCard(Clubs, false).getDescription() << endl;
- cout << p[1].name << ": " << p[2].playCard(Clubs, false).getDescription() << endl;
- cout << "Your play? ";
- cin >> cardChoice;
- cout << p[3].name << ": " << p[3].playCard(cardChoice).getDescription() << endl;
- leader = 2;
- }
- if (p[3].findClubTwo() != -1) {
- cout << p[3].name << ": " << p[3].playCard(p[3].findClubTwo()).getDescription() << endl;
- cout << p[2].name << ": " << p[0].playCard(Clubs, false).getDescription() << endl;
- cout << p[0].name << ": " << p[1].playCard(Clubs, false).getDescription() << endl;
- cout << p[1].name << ": " << p[2].playCard(Clubs, false).getDescription() << endl;
- leader = 3;
- }
- for (int j = 2; j <= 13; j++) {
- cout << "\t\tPoints\tPoints" << endl;
- cout << "Player\tRound " << j << "\tTotal" << endl;
- cout << "------\t-------\t-----" << endl;
- for (int i = 0; i < 4; i++)
- cout << p[i].name << "\t" << p[i].points << "\t" << p[i].points << endl;
- cout << endl;
- cout << "My Hand" << endl;
- p[3].displayHand();
- cout << endl;
- cout << "Round " << j << "\n-------\n";
- Card max = p[leader].playCard(Clubs, true);
- cout << p[leader].name + ": " + max.getDescription() << endl;
- Card next = p[(leader + 1) % 4].playCard(Clubs, false);
- cout << p[(leader + 1) % 4].name + ": " + next.getDescription() << endl;
- Card nextPlayer = p[(leader + 2) % 4].playCard(Clubs, false);
- cout << p[(leader + 2) % 4].name + ": " + nextPlayer.getDescription() << endl;
- cout << "Your play? ";
- cin >> cardChoice;
- if (next.getSuit() == max.getSuit()) {
- if (next.getNumber() > max.getNumber()) {
- max = next;
- }
- }
- }
- cin.get();
- cout << "Press any key to continue..." << endl;
- cin.get();
- return 0;
- }
- Output:
- Points Points
- Player Round 1 Total
- ------ ------- -----
- Round 1
- -------
- Me 0 0
- Snoop Dogg 0 0
- Lady Gaga 0 0
- Elton John 0 0
- 1: Clubs 9
- 2: Clubs 10
- 3: Clubs Queen
- 4: Diamonds 6
- 5: Diamonds 10
- 6: Diamonds Jack
- 7: Diamonds Ace
- 8: Hearts 5
- 9: Hearts 6
- 10: Hearts 9
- 11: Hearts 10
- 12: Hearts Ace
- 13: Spades Ace
- Snoop Dogg: Clubs 9
- Lady Gaga: Hearts 8
- Me: Hearts King
- Your play? 4
- Elton John: Hearts 2
- Points Points
- Player Round 2 Total
- ------ ------- -----
- Me 0 0
- Snoop Dogg 0 0
- Lady Gaga 0 0
- Elton John 0 0
- My Hand
- 1: Diamonds 2
- 2: Clubs King
- 3: Clubs Jack
- 4:
- 5: Spades Queen
- 6: Diamonds Queen
- 7: Clubs Ace
- 8: Hearts Queen
- 9: Hearts 3
- 10: Diamonds 9
- 11: Spades Jack
- 12: Hearts 7
- 13: Spades 7
- Round 2
- -------
- Snoop Dogg: Clubs 2
- Lady Gaga: Diamonds 8
- Elton John: Hearts Queen
- Your play? 3
- Points Points
- Player Round 3 Total
- ------ ------- -----
- Me 0 0
- Snoop Dogg 0 0
- Lady Gaga 0 0
- Elton John 0 0
- My Hand
- 1: Diamonds 2
- 2: Clubs King
- 3: Clubs Jack
- 4:
- 5: Spades Queen
- 6: Diamonds Queen
- 7: Clubs Ace
- 8:
- 9: Hearts 3
- 10: Diamonds 9
- 11: Spades Jack
- 12: Hearts 7
- 13: Spades 7
- Round 3
- -------
- Snoop Dogg: Clubs 2
- Lady Gaga: Spades 10
- Elton John: Hearts 3
- Your play?
- ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement