diff --git a/Oving 5/Oving 5/Blackjack.cpp b/Oving 5/Oving 5/Blackjack.cpp new file mode 100644 index 0000000..cae89be --- /dev/null +++ b/Oving 5/Oving 5/Blackjack.cpp @@ -0,0 +1,107 @@ +#include "Blackjack.h" +#include "Card.h" + +Blackjack::Blackjack() +{ + cards = CardDeck(); + cards.shuffle(); + + + player = Hand(); + dealer = Hand(true); + + for (int i = 0; i < 2; i++) + { + player.newCard(cards); + dealer.newCard(cards); + } + +} + +void Blackjack::printCards() +{ + cout << flush; + dealer.printCards(); + cout << endl; + player.printCards(); +} + +Hand::Hand() +{ + d = false; +} + +Hand::Hand(bool dealer) +{ + d = dealer; +} + +void Hand::newCard(CardDeck & cards) +{ + card.push_back(cards.drawCard()); + addValue(); +} + +/* +void Hand::addValue() +{ + Card c = card.back(); + int value = int(c.rankVal()); +} +*/ + + +void Hand::addValue() +{ + Card c = card.back(); + int valueOfCard = c.rankVal(); + if (valueOfCard >= 2 && valueOfCard <= 10) + total.push_back(valueOfCard); + else if (valueOfCard < 14) + total.push_back(10); + else if (valueOfCard == 14) + { + if (!d) + { + cout << "Do you want the ace to be 1 or 11? "; + int aceVal; + cin >> aceVal; + if (aceVal > 11 || aceVal > 5) + aceVal = 11; + else if (aceVal <= 5) + aceVal = 1; + total.push_back(aceVal); + } + else + total.push_back(11); + } + +} + +void Hand::printCards() +{ + for (int i = 0; i < card.size(); ++i) + { + if (d && i > 0) + cout << "Hidden dealer cards!" << endl; + else + cout << card[i].toString() << " \tValue: " << total[i] << endl; + } +} + +int Hand::sum() +{ + return calculateSum(); +} + + + +int Hand::calculateSum() +{ + int t = 0; + for (int i : total) + { + t += i; + } + return t; +} diff --git a/Oving 5/Oving 5/Blackjack.h b/Oving 5/Oving 5/Blackjack.h new file mode 100644 index 0000000..307c62c --- /dev/null +++ b/Oving 5/Oving 5/Blackjack.h @@ -0,0 +1,34 @@ +#pragma once +#include "CardDeck.h" + +class Hand +{ +public: + Hand(); + Hand(bool dealer); + void newCard(CardDeck& cards); + void printCards(); + int sum(); +private: + void addValue(); + vector card; + vector cardsToShow; + vector total; + bool d; + int calculateSum(); +}; + +class Blackjack +{ +public: + Blackjack(); + void printCards(); +private: + CardDeck cards; + Hand player; + Hand dealer; + +}; + + + diff --git a/Oving 5/Oving 5/Card.cpp b/Oving 5/Oving 5/Card.cpp index 1d8ce5c..9c298a8 100644 --- a/Oving 5/Oving 5/Card.cpp +++ b/Oving 5/Oving 5/Card.cpp @@ -1,5 +1,6 @@ #include "Card.h" + map suitNames = { {Suit::clubs, "Clubs" }, {Suit::diamonds, "Diamonds"}, {Suit::hearts, "Hearts"}, @@ -58,6 +59,14 @@ Card::Card(Suit suit, Rank rank) inline Suit Card::suit() { return s; } inline Rank Card::rank() { return r; } +int Card::suitVal() +{ + return int(s); +} +int Card::rankVal() +{ + return int(r); +} inline bool Card::isValid() { return valid; } string Card::toString() diff --git a/Oving 5/Oving 5/Card.h b/Oving 5/Oving 5/Card.h index 49bf5e1..2503a2c 100644 --- a/Oving 5/Oving 5/Card.h +++ b/Oving 5/Oving 5/Card.h @@ -1,7 +1,6 @@ #pragma once #include "std_lib_facilities.h" - enum class Suit { clubs, diamonds, hearts, spades }; enum class Rank { two = 2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace }; @@ -31,6 +30,8 @@ public: Suit suit(); Rank rank(); + int suitVal(); + int rankVal(); bool isValid(); string toString(); string toStringShort(); diff --git a/Oving 5/Oving 5/Oving 5.vcxproj b/Oving 5/Oving 5/Oving 5.vcxproj index 7e7c1c7..17d6fa8 100644 --- a/Oving 5/Oving 5/Oving 5.vcxproj +++ b/Oving 5/Oving 5/Oving 5.vcxproj @@ -134,12 +134,14 @@ + + diff --git a/Oving 5/Oving 5/Oving 5.vcxproj.filters b/Oving 5/Oving 5/Oving 5.vcxproj.filters index dbbc966..773642d 100644 --- a/Oving 5/Oving 5/Oving 5.vcxproj.filters +++ b/Oving 5/Oving 5/Oving 5.vcxproj.filters @@ -27,6 +27,9 @@ Source Files + + Source Files + @@ -38,5 +41,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/Oving 5/Oving 5/main.cpp b/Oving 5/Oving 5/main.cpp index 4a8faf7..5adaa15 100644 --- a/Oving 5/Oving 5/main.cpp +++ b/Oving 5/Oving 5/main.cpp @@ -1,13 +1,14 @@ #include "std_lib_facilities.h" #include "CardDeck.h" +#include "Blackjack.h" int main(){ #pragma region Task 1 // a - d // Testing functions and mapping - cout << suitToString(Suit::diamonds) << endl; - cout << rankToString(Rank::ace) << endl; + //cout << suitToString(Suit::diamonds) << endl; + //cout << rankToString(Rank::ace) << endl; // e /* @@ -19,19 +20,20 @@ int main(){ #pragma endregion #pragma region Task 2 + /* CardStruct card1 = { Suit::spades, Rank::ace }; CardStruct card2 = { Suit::diamonds, Rank::ten }; cout << toString(card1) << endl; cout << toStringShort(card2) << endl; - + */ #pragma endregion - /* + Card c = Card(Suit::clubs, Rank::two); cout << c.toString() << endl; - */ - + + CardDeck cD = CardDeck(); cD.print(); @@ -44,6 +46,11 @@ int main(){ Card c = cD.drawCard(); cout << c.toString() << endl; } + + + Blackjack bJack = Blackjack(); + + bJack.printCards(); keep_window_open(); return 0;