Nearly done

master
Øyvind Skaaden 2019-02-13 11:07:11 +01:00
parent b1fef061e4
commit 5ad204a8de
7 changed files with 173 additions and 7 deletions

View File

@ -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;
}

View File

@ -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> card;
vector<int> cardsToShow;
vector<int> total;
bool d;
int calculateSum();
};
class Blackjack
{
public:
Blackjack();
void printCards();
private:
CardDeck cards;
Hand player;
Hand dealer;
};

View File

@ -1,5 +1,6 @@
#include "Card.h"
map<Suit, string> 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()

View File

@ -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();

View File

@ -134,12 +134,14 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Blackjack.cpp" />
<ClCompile Include="Card.cpp" />
<ClCompile Include="CardDeck.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="utilities.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Blackjack.h" />
<ClInclude Include="Card.h" />
<ClInclude Include="CardDeck.h" />
<ClInclude Include="utilities.h" />

View File

@ -27,6 +27,9 @@
<ClCompile Include="utilities.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Blackjack.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Card.h">
@ -38,5 +41,8 @@
<ClInclude Include="utilities.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="Blackjack.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -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;