Question: Please use c++ language and please use the code at the end to buid this homework6, I will post homewrk 5 at the end. thank
Please use c++ language and please use the code at the end to buid this homework6, I will post homewrk 5 at the end. thank you .
Homework 6 Game of Hearts
Before writing any code, review the rules of the game, page 2, and play a few games at playhearts-online.com Write a program that plays the card game of Hearts. Your program should build on your code for homework 5, including your overloaded < operator, sort function, and shuffle function. Your program must have these features: 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:
Object of the Game The player with the lowest score wins.
Scoring At the end of each game, players count the number of hearts they have taken as well as the queen of spades. A total of 26 points are awarded: Each heart counts as 1 point Queen of spades counts as 13 points When one player takes all 13 hearts and the queen of spades, instead of losing 26 points, that player scores zero and each opponent scores 26 points. This is called shooting the moon. The Deal There are four players, each with 13 cards initially. Players sit around a table. To deal, start with any player and deal cards one at time clockwise. The Play Play proceeds in rounds, in which each player plays one card, in clockwise order around the table. Playing the first card of a round is called leading. The player holding the 2 of clubs leads the first round. Subsequently, the winner of one round leads the next round. Following the first card of a round, the remaining players must play a card in the same suit. A player with no cards in this suit may play a card in any other suit. The highest card of the suit that was led wins the round. The winner of a round and accumulates points for the queen of spades or any hearts. Rounds are sometimes called tricks.
Challenge 1 (optional - 1 bonus point) Enable games with three or five players. Challenge 2 (optional 1 bonus point) Implement these three special rules of play: After cards are dealt but before the first trick, each player selects three cards and passes them to the player on the right (clockwise). If a player has no clubs when the first trick is led, a heart or the queen of spades cannot be discarded. Hearts may not be led until a heart or the queen of spades has been discarded. (The queen of spades can be led at any time.)
Homework 5 code -----------------
=============================
Code:
card.h
//Include libraries
#ifndef CARD_H
#define CARD_H
//Define suits
enum Suits {Hearts = 3, Diamonds, Clubs, Spades};
//Define values
enum Values {Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace};
//Define array
const int NmCrds(52);
// Create structure
struct Card
{
//Create instance
Suits Suit;
//Create instance
Values Value;
};
//Define method
void lDisply (const Card &);
//Define method
void lIntDck (Card []);
//Define method
void lShwHnd (Card []);
//Define method
void lShffle (Card []);
//Define method
void lDel (Card []);
//End
#endif
card.cpp
//Include libraries
#include
#include
#include
#include
#include "card.h"
//Use namespace
using namespace std;
//Define array
string ValueNames [13] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
//Define method
void lDisply (const Card &C)
{
//Declare variable
string suit;
//Declare variable
int value;
//If value is 3
if(C.Suit == 3)
//Set value
suit = "Hearts";
//Set value
else if(C.Suit == 4)
// Set value
suit = "Diamonds";
// Otherwise
else if(C.Suit == 5)
// Set value
suit = "Clubs";
//Otherwise
else
// Set value
suit = "Spades";
//Switch
switch(C.Value)
{
//Define case 0
case 0:
//Set value
value = 2;
//Break
break;
//Define case
case 1:
//Set value
value = 3;
//Break
break;
//Define case
case 2:
//Set value
value = 4;
//Break
break;
//Define case
case 3:
//Set value
value = 5;
//Break
break;
//Define case
case 4:
//Set value
value = 6;
//Break
break;
//Define case
case 5:
//Set value
value = 7;
//Break
break;
//Define case
case 6:
//Set value
value = 8;
//Break
break;
//Define case
case 7:
//Set value
value = 9;
//Break
break;
//Define case
case 8:
//Set value
value = 10;
//Break
break;
}
//If condition satisfies
if(C.Value > 8)
//lDisply
std::cout << suit << ' ' << ValueNames [C.Value]<<"\t\t";
// Otherwise
else
//lDisply
cout << suit << ' ' << value<<"\t\t";
}
//Define method
void lIntDck (Card Deck [])
{
//Declare variable
int li;
//Create instance
Suits S;
//Create instance
Values V;
// Initialize
srand (time (0));
//Set value
li = 0;
// Loop
for (S = Hearts; S <= Spades; S = (Suits) (S + 1))
{
// Loop
for (V = Two; V <= Ace; V = (Values) (V + 1))
{
// Set suit
Deck [li].Suit = S;
// Set deck
Deck [li].Value = V;
// Increment
li++;
}
}
}
//Define method
void lShwHnd (Card Deck [])
{
//Declare variable
int li;
// Loop
for (li = 0; li < 5; li++)
//Call method
lDisply (Deck [li]);
}
//Define method
void lShffle (Card Deck [])
{
//Declare variable
int CardOne;
//Declare variable
int CardTwo;
//Create instance
Card Temp;
// Loop
for (CardOne = 0; CardOne < NmCrds; CardOne++)
{
//Generate value
CardTwo = rand () % NmCrds;
// Swap cards
Temp = Deck [CardOne];
//Create instance
Deck [CardOne] = Deck [CardTwo];
//Create instance
Deck [CardTwo] = Temp;
}
}
//Define method
void lDel (Card Deck [])
{
//Declare variable
int CardOne;
//Declare variable
int CardTwo;
//Create instance
Card Temp;
// Loop
for (CardOne = 0; CardOne < 5; CardOne++)
{
// Generate random number
CardTwo = rand () % 5;
// Swap card
Temp = Deck [CardOne];
//Create instance
Deck [CardOne] = Deck [CardTwo];
//Create instance
Deck [CardTwo] = Temp;
}
}
Main.cpp
//Include libraries
#include
#include "card.h"
//Use namespace
using namespace std;
//Define method
int menu()
{
//Declare variable
int choice;
// lDisply menu
cout<<" 1 Shuffle Deck 2 Deal Deck 3 Exit";
//lDisply message
cout<<" Enter your choice: ";
//Store value
cin>>choice;
// Return
return choice;
}
//Define main
int main()
{
// Create instance
Card Deck[NmCrds];
// Create instance
Card Hands[4][5];
//Declare variables
int choice, i, j;
// Call method
lIntDck(Deck);
// Loop
do
{
// Call method
choice = menu();
//Switch
switch(choice)
{
//Define case
case 1:
//Call method
lShffle(Deck);
//Break
break;
//Define case
case 2:
//Call method
lDel(Deck);
//Assign value
j = 0;
//Loop
for (i = 0; i < 5; i++)
{
//Store value
Hands[0][i] = Deck[j];
// Increment value
j++;
}
//lDisply message
cout << " \t\t\t\tHand 1 is: " << endl;
//Loop
for (i = 0; i < 5; i++)
{
//Call method
lDisply(Hands[0][i]);
}
//Loop
for (i = 0; i < 5; i++)
{
//Store
Hands[1][i] = Deck[j];
//Increment
j++;
}
//Display message
cout << " \t\t\t\tHand 2 is: " << endl;
// Loop
for (i = 0; i < 5; i++)
{
//Call method
lDisply(Hands[1][i]);
}
//Loop
for (i = 0; i < 5; i++)
{
//Assign value
Hands[2][i] = Deck[j];
// Increment
j++;
}
//Display message
cout << " \t\t\t\tHand 3 is: " << endl;
// Loop
for (i = 0; i < 5; i++)
{
//Call method
lDisply(Hands[2][i]);
}
//Loop
for (i = 0; i < 5; i++)
{
//Assign value
Hands[3][i] = Deck[j];
// Increment
j++;
}
//Display message
cout << " \t\t\t\tHand 4 is: " << endl;
// Loop
for (i = 0; i < 5; i++)
{
// Call method
lDisply(Hands[3][i]);
}
//Break
break;
//Define case
case 3:
//Pause console window
system("pause");
//Exit
exit(0);
//Default
default:
//Display message
cout<<" Invalid choice!";
}
}
//Loop
while(1);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
