Last active
August 29, 2015 14:02
-
-
Save dryya/0c7c6048e08a7efbd068 to your computer and use it in GitHub Desktop.
for dailyprogrammer challenge 161 easy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <numeric> | |
#include <algorithm> | |
#include <string> | |
/* The following function will create a vector made up of a certain number | |
of individual decks. The structure of a deck is all integers from | |
zero to fifty-one inclusive.*/ | |
std::vector<int> createDeck(int amount) { | |
std::vector<int> singleDeck(52); | |
std::iota(std::begin(singleDeck), std::end(singleDeck), 0); | |
std::vector<int> x; | |
for (int a = 0; a < amount; a++) { //put in a given amount of decks into the big deck | |
x.reserve(singleDeck.size()); | |
x.insert(x.end(), singleDeck.begin(), singleDeck.end()); | |
} | |
return x; | |
} | |
/* This function is for the output later on. Given a card from the vector, it matches it to a suit. | |
First 13 are diamonds, and so on.*/ | |
std::string getSuit(int index) { | |
if (index / 13 == 0) { | |
return "Diamond"; | |
} | |
else if (index / 13 == 1) { | |
return "Club"; | |
} | |
else if (index / 13 == 2) { | |
return "Heart"; | |
} | |
else if (index / 13 == 3) { | |
return "Spade"; | |
} | |
} | |
/* This function will fix the values of face cards so that they are all equal to 10 when | |
we sum up the values to see if we got blackjack. */ | |
int valueFixer(int value) { | |
if (value == 0) { | |
value = 11; | |
return value; | |
} | |
else if (value == 10) { | |
value = 10; | |
return value; | |
} | |
else if (value == 11) { | |
value = 10; | |
return value; | |
} | |
else if (value == 12) { | |
value = 10; | |
return value; | |
} | |
else { | |
return value; | |
} | |
} | |
// This function will match face cards to values when we output all of the combinations. | |
std::string faceCardHandler(int value) { | |
if (1 <= value <= 9) { | |
return std::to_string(value); | |
} | |
else if (value == 0) { | |
value = 11; | |
return "Ace"; | |
} | |
else if (value == 10) { | |
value = 10; | |
return "Jack"; | |
} | |
else if (value == 11) { | |
value = 10; | |
return "Queen"; | |
} | |
else if (value == 12) { | |
value = 10; | |
return "King"; | |
} | |
} | |
int main() { | |
int numberOfDecks, handsToPlay; | |
std::vector<int> deck; | |
std::cout << "Input the number of decks to shuffle together" | |
<< "followed by the number of two card hands to deal. If a" | |
<< "hand has a value of 11 or less, another card will be dealt." << std::endl; | |
std::cin >> numberOfDecks >> handsToPlay; | |
deck = createDeck(numberOfDecks); | |
std::random_shuffle(deck.begin(), deck.end()); | |
int counter = 0, blackJackCounter = 0; | |
while (counter < handsToPlay) { | |
int total = 0; //Pop out 2 cards and add them total. | |
std::string output; | |
total += valueFixer(deck.back()); | |
output += (' ' + getSuit(deck.back()) + ' ' + faceCardHandler(deck.back())); | |
deck.pop_back(); | |
total += valueFixer(deck.back()); | |
output += (' ' + getSuit(deck.back()) + ' ' + faceCardHandler(deck.back())); | |
deck.pop_back(); | |
if (total <= 11) { //If total < 11, pull out a third card. | |
total += valueFixer(deck.back()); | |
output += (' ' + getSuit(deck.back()) + ' ' + faceCardHandler(deck.back())); | |
deck.pop_back(); | |
} | |
else if (total == 21) { | |
std::cout << "Blackjack!"; | |
blackJackCounter += 1; | |
} | |
std::cout << output << std::endl; | |
counter++; | |
} | |
float blackJackCounterFloat = blackJackCounter, handsToPlayFloat = handsToPlay; //Needed for percent calc. | |
std::cout << "There were " << blackJackCounter << " blackjacks. This was an incidence of " << blackJackCounter | |
<< " out of " << handsToPlay << " or " << (blackJackCounterFloat/handsToPlayFloat)*100 << " percent." << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment