#include "card.h"
// Create a single card using its suit and rank
Card c("hearts","A");
// Print Card object
cout << c;
// Create a full ordered deck of cards
auto deck = full_deck();
for (int i=0; i < deck.size(); i++) {
cout << deck[i] << " ";
}
// Shuffle deck and see the result
shuffle_deck(deck);
for (int i=0; i < deck.size(); i++) {
cout << deck[i] << " ";
}
deck = small_random_deck(20);
for (int i=0; i < deck.size(); i++) {
cout << deck[i] << " ";
}
// Create a deck from the cards you have in your hand
// ** REPLACE WITH YOUR OWN DECK **
auto deck2 = deck_from_tuples({
make_tuple("diamonds", "A"),
make_tuple("hearts", "4")
});
for (int i=0; i < deck2.size(); i++) {
cout << deck2[i] << " ";
}
/**
* Finds the highest ranked hearts card in the deck
*
* @param deck Vector of Card objects to be searched in
* @return Card of hearts suit with the highest value in the deck
*/
Card find_highest_heart(vector<Card> deck){
// prepare variable to store intermediate answers
int max_heart_index = -1;
// loop over all the cards in you deck ( HINT: get size of the vector with .size() )
// ** ADD CODE HERE **
// check the suit
if (deck[i].suit=="hearts"){
// the first heart is automatically the highest
if (max_heart_index==-1)
max_heart_index = i;
// otherwise, need to check if it's bigger than the previously seen heart
else if (deck[i] > deck[max_heart_index])
max_heart_index = i;
}
// Handle case when there are no hearts in the deck
if (max_heart_index==-1){
throw std::range_error("There are no hearts in the deck");
}
return deck[max_heart_index];
}
Check your solution:
deck = small_random_deck(10);
cout << "Deck: ";
for (int i=0; i < deck.size(); i++) {cout << deck[i] << " ";}
cout << "\nHighest heart in the deck: " << find_highest_heart(deck);
The first card at the top is at location/index 0.
/**
* Finds the highest ranked diamonds card in the deck that is in an even position
*
* @param deck Vector of Card objects to be searched in
* @return Card of diamonds suit with the highest value among even positions in the deck
*/
Card find_highest_even_diamond(vector<Card> deck){
// prepare variable to store intermediate answers
int highest_even_diamond_index = -1;
// loop over all the cards in you deck
// ** ADD CODE HERE **
// check if index is even
if (i%2==0){
// the rest is the same as the hearts example above (but with diamonds)
if (deck[i].suit=="diamonds"){
if (highest_even_diamond_index==-1)
highest_even_diamond_index = i;
else if (deck[i] > deck[highest_even_diamond_index])
highest_even_diamond_index = i;
}
}
// Handle case when there are no diamonds in the even positions of the deck
if (highest_even_diamond_index==-1){
throw std::range_error("No diamonds in even positions");
}
return deck[highest_even_diamond_index];
}
Check your solution:
deck = small_random_deck(10);
cout << "Deck: ";
for (int i=0; i < deck.size(); i++) {cout << deck[i] << " ";}
cout << "\nHighest diamonds card in the deck in even position: " << find_highest_even_diamond(deck);
/**
* Finds the highest value card in the first seven of the deck
*
* @param deck Vector of Card objects to be searched in
* @return Card with the highest value among first seven cards in the deck
*/
Card find_highest_in_seven(vector<Card> deck){
// prepare variable to store intermediate answers
int highest_index = -1;
// loop over 7 indices
// ** ADD CODE HERE **
// grab the card in that location
Card card = // ** ADD CODE HERE **
// first card is automatically highest
if (highest_index==-1)
highest_index = i;
// otherwise, needs to be bigger than previous highest card
else if (card > deck[highest_index])
highest_index = i;
return deck[highest_index];
}
Check your solution:
deck = small_random_deck(20);
cout << "Deck: ";
for (int i=0; i < deck.size(); i++) {cout << deck[i] << " ";}
cout << "\nHighest diamonds card in the deck in even position: " << find_highest_in_seven(deck);
// Hold piles of cards in the vector of vectors of Cards
vector<vector<Card> > piles{};
// Create 3 piles
piles.push_back(small_random_deck(1));
piles.push_back(small_random_deck(5));
piles.push_back(small_random_deck(6));
/**
* Counts number of cards in the piles of cards
*
* @param piles Vector of vectors of Card objects to be searched in
* @return Vector of integers containing counts of the card
*/
std::vector<int> count_three_piles(vector<vector<Card> > piles){
// prepare to collect counts
vector<int> counts(3);
// loop over piles
// ** ADD CODE HERE **
// set up counter for this pile
counts[pile] = 0;
// loop over cards in this pile.
// ** ADD CODE HERE **
// HINT: get size of the vector with .size()
// count each card
counts[pile]++;
return counts;
}
Check your solution:
// Expected output: 1 5 6
auto result = count_three_piles(piles);
cout << result[0] << " " << result[1] << " " << result[2];
void infinite_loop(vector<Card> deck){
// the check will always be True
// ** ADD CODE HERE **
// grab the first card
auto first_card = deck[0];
pop_front(deck);
// put it on the bottom
deck.push_back(first_card);
// print deck so you can see what is happening
for (int i=0; i < deck.size(); i++) {
cout << deck[i] << " " << std::flush;
}
cout << "\n";
// include sleep timer so that iteration goes slower
usleep(500000); //time in microseconds
}
Check your solution:
deck = small_random_deck(10);
cout << "Deck: ";
for (int i=0; i < deck.size(); i++) {cout << deck[i] << " ";}
infinite_loop(deck);
int at_least_21(vector<Card> deck){
// set up counter
int counter = 0;
int sum = 0;
// keep going if 21 has not been reached
// ** ADD CODE HERE **
// grab the card in that location
Card card = //** ADD CODE HERE **
// add its value to your sum (HINT: use card.value)
// ** ADD CODE HERE **
// update counter
// ** ADD CODE HERE **
return counter;
}
cout << at_least_21(small_random_deck(10));
Two players each get a deck of card. They both flip the top card in their deck. Whosever card has the highest value (assume A=1, J=11, Q=12, K=13) adds both cards to the bottom of their deck. If the values are the same, set the cards aside. The game ends when one person is out of cards.
auto d1 = small_random_deck(4);
auto d2 = small_random_deck(4);
int game_of_war(vector<Card> deck1, vector<Card> deck2){
// how do you know whether to keep going?
// what do you do at each step?
return winner
}
Check your solution:
cout << "Player 1 deck: ";
for (int i=0; i < d1.size(); i++) {cout << d1[i] << " ";}
cout << "\nPlayer 2 deck: ";
for (int i=0; i < d2.size(); i++) {cout << d2[i] << " ";}
cout << "\n\nGAME OF WAR\n";
cout << game_of_war(d1,d2);