In: Computer Science
How do I make a template functions for these 2 functions to prevent code duplication
These are the 2 functions
1st:
virtual Card lead_card(const std::string &trump) {
int numOfTrump =
0;
for (int i = 0; i <
int(hand.size()); ++i) {
if (hand[i].is_trump(trump) == true) {
numOfTrump += 1;
}
}
if (numOfTrump !=
int(hand.size())) {
Card c = highestCardNotIncludingTrump(hand, trump);
int indexOfHighest = 0;
for (int i = 0; i < int(hand.size()); ++i) {
if (c == hand[i]) {
indexOfHighest = i;
}
}
hand.erase(hand.begin() + indexOfHighest);
cout << c << " led by " << get_name() <<
endl;
return c;
}
else {
Card c = highestCardIncludingTrump(hand, trump);
int indexOfHighest = 0;
for (int i = 0; i < int(hand.size()); ++i) {
if (c == hand[i]) {
indexOfHighest = i;
}
}
hand.erase(hand.begin() + indexOfHighest);
cout << c << " led by " << get_name() <<
endl;
return c;
}
}
2nd code:
virtual Card play_card(const Card &led_card, const
std::string &trump) {
int numLedSuitCards =
0;
string ledCardSuit =
led_card.get_suit();
if
(led_card.is_trump(trump) || led_card.is_left_bower(trump)) {
for (int i = 0; i < int(hand.size()); ++i) {
if (hand[i].is_trump(trump) == true) {
numLedSuitCards += 1;
}
}
}
else {
for (int i = 0; i < int(hand.size()); ++i) {
if (hand[i].get_suit() == ledCardSuit &&
!(hand[i].is_left_bower(trump))) {
numLedSuitCards += 1;
};
}
}
if (numLedSuitCards >
0) {
Card c = highestCardFollowingSuit(hand, led_card, trump);
int indexOfHighest = 0;
for (int i = 0; i < int(hand.size()); ++i) {
if (c == hand[i]) {
indexOfHighest = i;
}
}
hand.erase(hand.begin() + indexOfHighest);
cout << c << " played by " << get_name() <<
endl;
return c;
}
else {
Card c = lowestCardIncludingTrump(hand, trump);
int indexOfLowest = 0;
for (int i = 0; i < int(hand.size()); ++i) {
if (c == hand[i]) {
indexOfLowest = i;
}
}
hand.erase(hand.begin() + indexOfLowest);
cout << c << " played by " << get_name() <<
endl;
return c;
}
This can be templatised as follows:
template<typename T>
Card lead_card(const T& trump) {
int numOfTrump = 0;
for (int i = 0; i < int(hand.size()); ++i) {
if (hand[i].is_trump(trump) == true) {
numOfTrump += 1;
}
}
if (numOfTrump != int(hand.size())) {
Card c = highestCardNotIncludingTrump(hand, trump);
int indexOfHighest = 0;
for (int i = 0; i < int(hand.size()); ++i) {
if (c == hand[i]) {
indexOfHighest = i;
}
}
hand.erase(hand.begin() + indexOfHighest);
cout << c << " led by " << get_name() <<
endl;
return c;
}
else {
Card c = highestCardIncludingTrump(hand, trump);
int indexOfHighest = 0;
for (int i = 0; i < int(hand.size()); ++i) {
if (c == hand[i]) {
indexOfHighest = i;
}
}
hand.erase(hand.begin() + indexOfHighest);
cout << c << " led by " << get_name() <<
endl;
return c;
}
}
template<typename T>
Card play_card(const Card & led_card, const T& trump)
{
int numLedSuitCards = 0;
string ledCardSuit = led_card.get_suit();
if (led_card.is_trump(trump) || led_card.is_left_bower(trump))
{
for (int i = 0; i < int(hand.size()); ++i) {
if (hand[i].is_trump(trump) == true) {
numLedSuitCards += 1;
}
}
}
else {
for (int i = 0; i < int(hand.size()); ++i) {
if (hand[i].get_suit() == ledCardSuit &&
!(hand[i].is_left_bower(trump))) {
numLedSuitCards += 1;
};
}
}
if (numLedSuitCards > 0) {
Card c = highestCardFollowingSuit(hand, led_card, trump);
int indexOfHighest = 0;
for (int i = 0; i < int(hand.size()); ++i) {
if (c == hand[i]) {
indexOfHighest = i;
}
}
hand.erase(hand.begin() + indexOfHighest);
cout << c << " played by " << get_name() <<
endl;
return c;
}
else {
Card c = lowestCardIncludingTrump(hand, trump);
int indexOfLowest = 0;
for (int i = 0; i < int(hand.size()); ++i) {
if (c == hand[i]) {
indexOfLowest = i;
}
}
hand.erase(hand.begin() + indexOfLowest);
cout << c << " played by " << get_name() <<
endl;
return c;
}
Here the type of the trump can be any thing not needed to be string and corresponding adaptation has to be made in the private functions called within the above functions.