Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can somebody please help me write a program that plays the card game of Hearts. I really need help on this because it's been driving

Can somebody please help me write a program that plays the card game of Hearts. I really need help on this because it's been driving me crazy for the past 4 days! Any help at all will be greatly appreciated! I have my program posted below. Here are the instructions:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------PART 1:

Write a class Card with these member attributes: Suit (use enumerated data type for spades, hearts, diamonds, clubs) Number (1 through 13 jack is 11, queen is 12, king is 13) Description (string which displays name of card, such as Ace of Hearts) Card has an overloaded < operator, which returns true or false based on these rules: Regardless of card number, spades > hearts > diamonds > clubs Within a suit, the numeric value determines >, except for aces (value 1) which are > all numeric values Create additional member functions as needed.

2) Write a class Deck with just one member attribute, and array of 52 cards. Deck has these member functions: Initialize initializes values for deck of cards Shuffle shuffles deck Deal deals cards to four hands Sort sorts each hand into order Display displays four hands

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PART 2:

Four hands only

At least three classes Card, Deck, and Player.

Use the try-throw-catch statements when a player tries to play an incorrect card, such as not following suit on a trick (see rules of the game).

Keep your main program simple. Most of your logic should be in member functions for classes.

Your display should look something like this:

HAND 1 HAND 2 HAND 3 HAND 4

---------- ---------- ----------- -----------

3 points 11 points 2 points 0 points

----------- ----------- ----------- -----------

Spade Ace Spade 5 Heart 7 Spade 10

Spade 2 Heart Jack Diamond Ace Spade 4

Heart Queen Club 10 Diamond King Heart 2

Heart 4 Club 3 Diamond Queen Diamond Jack

Diamond Jack Club 2 Club 9 Diamond 8

CURRENT PLAY:

Club 4 Club Queen enter card:

PROGRAM:

// Card.h

#ifndef Card_h

#define Card_h

#include

#include

#include

#include

#include

using namespace std;

//The enumerated data types

enum Suit

{

Clubs = 1,

Diamonds = 2,

Hearts = 3,

Spades = 4

};

//The Card class

class Card

{

private:

string description;

Suit suit;

int number;

public:

// default constructor

Card()

{

number = 0;

description = "";

}

// get function for suit

Suit getCardSuit()

{

return suit;

}

// set function for suit

void setCardSuit(Suit m_suit)

{

suit = m_suit;

}

int getCardNumber()

{

return number;

}

// set function for card

void setCardNumber(int m_num)

{

number = m_num;

}

// get function for description

string getCardDescription()

{

return description;

}

// set the card description

void setDescription(string m_description)

{

description = m_description;

}

// Overloaded operator < function

bool operator <(const Card& card2)

{

if (suit < card2.suit)

return true;

else if (suit > card2.suit)

return false;

else

{

if (number == 1)

return false;

else if (card2.number == 1)

return true;

else if (card2.number < number)

return true;

}

return false; // may be incorrect

}

};

#endif /* Card_h */

// Deck.h

#ifndef Deck_h

#define Deck_h

#include "Card.h"

#include

#include

#include

#include

#include

using namespace std;

const int SIZE = 52;

Card card[52]; // card array

//Class Deck definition

class Deck

{

//private card variable

Card deckOfCards[SIZE];

public:

void initialize()

{

//the string to perform the description

string cardDesc;

char name[10];

int setFlag;

//the for loop to initialize the deck Of Cards

for (int i = 1; i <= SIZE; i++)

{

setFlag = 0;

if (i == 1 || i == 14 || i == 27 || i == 40)

{

cardDesc = "Ace";

setFlag = 1;

}

else if (i == 11 || i == 24 || i == 37 || i == 50)

{

cardDesc = "Jack";

setFlag = 1;

}

else if (i == 12 || i == 25 || i == 38 || i == 51)

{

cardDesc = "Queen";

setFlag = 1;

}

else if (i == 13 || i == 26 || i == 39 || i == 52)

{

cardDesc = "King";

setFlag = 1;

}

if(i <= 13)

{

deckOfCards[i - 1].setCardSuit(Spades);

deckOfCards[i - 1].setCardNumber(i);

if(setFlag == 0)

{

(deckOfCards[i - 1].getCardNumber(), name, 10);

cardDesc = ((string)name);

}

cardDesc = " of Spades";

deckOfCards[i - 1].setDescription(cardDesc);

}

else if (i > 13 && i <= 26)

{

deckOfCards[i - 1].setCardSuit(Hearts);

deckOfCards[i - 1].setCardNumber(i - 13);

if(setFlag == 0)

{

(deckOfCards[i - 1].getCardNumber(), name, 10);

cardDesc = ((string)name);

}

cardDesc = " of Hearts";

deckOfCards[i - 1].setDescription(cardDesc);

}

else if(i > 26 && i <= 39)

{

deckOfCards[i - 1].setCardSuit(Diamonds);

deckOfCards[i - 1].setCardNumber(i - 26);

if(setFlag == 0)

{

(deckOfCards[i - 1].getCardNumber(), name, 10);

cardDesc = ((string)name);

}

cardDesc = " of Diamonds";

deckOfCards[i - 1].setDescription(cardDesc);

}

else

{

deckOfCards[i - 1].setCardSuit(Clubs);

deckOfCards[i - 1].setCardNumber(i - 39);

if(setFlag == 0)

{

(deckOfCards[i - 1].getCardNumber(), name, 10);

cardDesc = ((string)name);

}

cardDesc = (" of Clubs");

deckOfCards[i - 1].setDescription(cardDesc);

}

}

}

void shuffle()

{

Card cardObj;

int numRandm;

srand(time(NULL));

for (int i = 0; i < SIZE; i++)

{

numRandm = rand() % 52;

cardObj = deckOfCards[i];

deckOfCards[i] = deckOfCards[numRandm];

deckOfCards[numRandm] = cardObj;

}

}

void deal()

{

Card hands[13][4];

for(int j = 0 ; j < 13; j++)

{

for(int i = 0; i < 4; i++)

{

hands[j][i] = deckOfCards[i + 4 * j];

}

}

sort(hands);

display(hands);

}

void sort(Card hands[13][4])

{

Card cardObj;

int j;

for (int itr = 0; itr < 4; itr++)

{

for(int i = 1 ; i < 13 ; i++)

{

cardObj = hands[i][itr];

j = i - 1;

while(j >= 0 && hands[j][itr] < cardObj)

{

hands[j + 1][itr] = hands[j][itr];

j--;

}

hands[j + 1][itr] = cardObj;

}

}

}

void display(Card hands[13][4])

{

Suit suit;

int number;

cout << endl << setw(14) << "Player 1" << setw(16) << "Player 2" << setw(16)<< "Player 3" << setw(16) << "Player 4";

cout << endl << setw(14) << "--------" << setw(16) << "--------" << setw(16)<< "--------" << setw(16) << "--------" << endl << endl;

for (int j = 0 ; j < 13 ; j++)

{

for (int i = 0 ; i < 4 ; i++)

{

suit = hands[j][i].getCardSuit();

number = hands[j][i].getCardNumber();

if (suit == 1)

cout << setw(10) << "Club";

else if (suit == 2)

cout << setw(10) << "Diamond";

else if (suit == 3)

cout << setw(10) << "Heart";

else

cout << setw(10) << "Spade";

if (number == 1)

cout << setw(6) << "Ace";

else if(number == 11)

cout << setw(6) << "Jack";

else if(number == 12)

cout << setw(6) << "Queen";

else if(number == 13)

cout << setw(6) << "King";

else

cout << setw(6) << number;

}

cout << endl;

}

}

};

#endif /* Deck_h */

// main.cpp

#include "Card.h"

#include "Deck.h"

#include

using namespace std;

int main()

{

Deck deckOfCards;

deckOfCards.initialize();

int choice = 1;

do

{

cout << " What would you like to do now?" << endl;

cout << "1) Shuffle Cards" << endl;

cout << "2) Deal Cards" << endl;

cout << "3) End Program" << endl;

cout << "Enter your selection (1, 2, or 3): ";

cin >> choice;

switch(choice)

{

case 1:

cout << "Shuffling cards" << endl;

deckOfCards.shuffle();

cout << "Cards are now shuffled" << endl;

break;

case 2:

cout << "Dealing cards" << endl;

deckOfCards.deal();

cout << "Cards are now dealt" << endl;

break;

case 3:

cout<<"Ending the program" << endl;

exit(0);

break;

default:

cout << "Please try again and enter a number from the menu!";

}

} while (choice != 3);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

What are the key tenets of expectancy theory?

Answered: 1 week ago

Question

What are the six steps in applying for a patent?

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago