{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Teaching Loops With Cards and C++\n", "\n", "\"Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create decks of cards" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \"card.h\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Create a single card using its suit and rank\n", "Card c(\"hearts\",\"A\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Print Card object\n", "cout << c;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Create a full ordered deck of cards\n", "auto deck = full_deck();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for (int i=0; i < deck.size(); i++) {\n", " cout << deck[i] << \" \";\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Shuffle deck and see the result\n", "shuffle_deck(deck);\n", "for (int i=0; i < deck.size(); i++) {\n", " cout << deck[i] << \" \";\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "deck = small_random_deck(20);\n", "for (int i=0; i < deck.size(); i++) {\n", " cout << deck[i] << \" \";\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Create a deck from the cards you have in your hand\n", "// ** REPLACE WITH YOUR OWN DECK **\n", "auto deck2 = deck_from_tuples({\n", " make_tuple(\"diamonds\", \"A\"),\n", " make_tuple(\"hearts\", \"4\")\n", "});\n", "for (int i=0; i < deck2.size(); i++) {\n", " cout << deck2[i] << \" \";\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Find the highest value Heart in your deck" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "/**\n", " * Finds the highest ranked hearts card in the deck\n", " *\n", " * @param deck Vector of Card objects to be searched in\n", " * @return Card of hearts suit with the highest value in the deck\n", " */\n", "Card find_highest_heart(vector deck){\n", " // prepare variable to store intermediate answers\n", " int max_heart_index = -1;\n", " \n", " // loop over all the cards in you deck ( HINT: get size of the vector with .size() )\n", " // ** ADD CODE HERE **\n", "\n", " // check the suit\n", " if (deck[i].suit==\"hearts\"){\n", " // the first heart is automatically the highest\n", " if (max_heart_index==-1)\n", " max_heart_index = i;\n", " // otherwise, need to check if it's bigger than the previously seen heart\n", " else if (deck[i] > deck[max_heart_index])\n", " max_heart_index = i;\n", " }\n", "\n", " \n", " // Handle case when there are no hearts in the deck\n", " if (max_heart_index==-1){\n", " throw std::range_error(\"There are no hearts in the deck\");\n", " }\n", " \n", " return deck[max_heart_index];\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check your solution:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "deck = small_random_deck(10);\n", "cout << \"Deck: \";\n", "for (int i=0; i < deck.size(); i++) {cout << deck[i] << \" \";}\n", "cout << \"\\nHighest heart in the deck: \" << find_highest_heart(deck);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Find the highest value Diamond in your deck that is in an even location (index)\n", "\n", "The first card at the top is at location/index 0." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "/**\n", " * Finds the highest ranked diamonds card in the deck that is in an even position\n", " *\n", " * @param deck Vector of Card objects to be searched in\n", " * @return Card of diamonds suit with the highest value among even positions in the deck\n", " */\n", "Card find_highest_even_diamond(vector deck){\n", " // prepare variable to store intermediate answers\n", " int highest_even_diamond_index = -1;\n", " \n", " // loop over all the cards in you deck\n", " // ** ADD CODE HERE **\n", "\n", " // check if index is even\n", " if (i%2==0){\n", " // the rest is the same as the hearts example above (but with diamonds)\n", " if (deck[i].suit==\"diamonds\"){\n", " if (highest_even_diamond_index==-1)\n", " highest_even_diamond_index = i;\n", " else if (deck[i] > deck[highest_even_diamond_index])\n", " highest_even_diamond_index = i;\n", " }\n", " }\n", "\n", " \n", " // Handle case when there are no diamonds in the even positions of the deck\n", " if (highest_even_diamond_index==-1){\n", " throw std::range_error(\"No diamonds in even positions\");\n", " }\n", " return deck[highest_even_diamond_index];\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check your solution:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "deck = small_random_deck(10);\n", "cout << \"Deck: \";\n", "for (int i=0; i < deck.size(); i++) {cout << deck[i] << \" \";}\n", "cout << \"\\nHighest diamonds card in the deck in even position: \" << find_highest_even_diamond(deck);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Find the highest value card in the first seven of your deck." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "/**\n", " * Finds the highest value card in the first seven of the deck\n", " *\n", " * @param deck Vector of Card objects to be searched in\n", " * @return Card with the highest value among first seven cards in the deck\n", " */\n", "Card find_highest_in_seven(vector deck){\n", " // prepare variable to store intermediate answers\n", " int highest_index = -1;\n", " \n", " // loop over 7 indices \n", " // ** ADD CODE HERE **\n", "\n", " // grab the card in that location\n", " Card card = // ** ADD CODE HERE **\n", " \n", " // first card is automatically highest\n", " if (highest_index==-1)\n", " highest_index = i;\n", " // otherwise, needs to be bigger than previous highest card\n", " else if (card > deck[highest_index])\n", " highest_index = i;\n", "\n", " \n", " return deck[highest_index];\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check your solution:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "deck = small_random_deck(20);\n", "cout << \"Deck: \";\n", "for (int i=0; i < deck.size(); i++) {cout << deck[i] << \" \";}\n", "cout << \"\\nHighest diamonds card in the deck in even position: \" << find_highest_in_seven(deck);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Separate your cards into 3 piles. Count the number of cards in each pile." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Hold piles of cards in the vector of vectors of Cards\n", "vector > piles{};\n", "\n", "// Create 3 piles\n", "piles.push_back(small_random_deck(1));\n", "piles.push_back(small_random_deck(5));\n", "piles.push_back(small_random_deck(6));" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "/**\n", " * Counts number of cards in the piles of cards\n", " *\n", " * @param piles Vector of vectors of Card objects to be searched in\n", " * @return Vector of integers containing counts of the card\n", " */\n", "std::vector count_three_piles(vector > piles){\n", " // prepare to collect counts\n", " vector counts(3);\n", " \n", " // loop over piles\n", " // ** ADD CODE HERE **\n", "\n", " \n", " // set up counter for this pile\n", " counts[pile] = 0;\n", " \n", " // loop over cards in this pile. \n", " // ** ADD CODE HERE **\n", " // HINT: get size of the vector with .size()\n", "\n", " // count each card\n", " counts[pile]++;\n", " \n", " return counts;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check your solution:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Expected output: 1 5 6 \n", "auto result = count_three_piles(piles);\n", "cout << result[0] << \" \" << result[1] << \" \" << result[2];" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Look at the value of the top card in your deck. Put it on the bottom. Forever." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "void infinite_loop(vector deck){\n", " \n", " // the check will always be True\n", " // ** ADD CODE HERE **\n", "\n", " // grab the first card\n", " auto first_card = deck[0];\n", " pop_front(deck);\n", " \n", " // put it on the bottom\n", " deck.push_back(first_card);\n", " \n", " // print deck so you can see what is happening\n", " for (int i=0; i < deck.size(); i++) {\n", " cout << deck[i] << \" \" << std::flush;\n", " }\n", " cout << \"\\n\";\n", " \n", " // include sleep timer so that iteration goes slower\n", " usleep(500000); //time in microseconds\n", "\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check your solution:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "deck = small_random_deck(10);\n", "cout << \"Deck: \";\n", "for (int i=0; i < deck.size(); i++) {cout << deck[i] << \" \";}\n", "infinite_loop(deck);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Draw cards until the sum of their values reaches (or passes) 21. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "int at_least_21(vector deck){\n", " // set up counter\n", " int counter = 0;\n", " int sum = 0;\n", " \n", " // keep going if 21 has not been reached\n", " // ** ADD CODE HERE **\n", "\n", " // grab the card in that location\n", " Card card = //** ADD CODE HERE **\n", " \n", " // add its value to your sum (HINT: use card.value)\n", " // ** ADD CODE HERE **\n", " \n", " // update counter\n", " // ** ADD CODE HERE **\n", " \n", " return counter;\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cout << at_least_21(small_random_deck(10));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7. CHALLENGE: The Game of War\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto d1 = small_random_deck(4);\n", "auto d2 = small_random_deck(4);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "int game_of_war(vector deck1, vector deck2){\n", " \n", " // how do you know whether to keep going?\n", " // what do you do at each step?\n", "\n", " return winner\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check your solution:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cout << \"Player 1 deck: \";\n", "for (int i=0; i < d1.size(); i++) {cout << d1[i] << \" \";}\n", "cout << \"\\nPlayer 2 deck: \";\n", "for (int i=0; i < d2.size(); i++) {cout << d2[i] << \" \";}\n", "cout << \"\\n\\nGAME OF WAR\\n\";\n", "cout << game_of_war(d1,d2);" ] } ], "metadata": { "kernelspec": { "display_name": "C++17", "language": "C++17", "name": "xcpp17" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "-std=c++17" } }, "nbformat": 4, "nbformat_minor": 2 }