Nearly done
parent
b1fef061e4
commit
5ad204a8de
|
@ -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;
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "Card.h"
|
#include "Card.h"
|
||||||
|
|
||||||
|
|
||||||
map<Suit, string> suitNames = { {Suit::clubs, "Clubs" },
|
map<Suit, string> suitNames = { {Suit::clubs, "Clubs" },
|
||||||
{Suit::diamonds, "Diamonds"},
|
{Suit::diamonds, "Diamonds"},
|
||||||
{Suit::hearts, "Hearts"},
|
{Suit::hearts, "Hearts"},
|
||||||
|
@ -58,6 +59,14 @@ Card::Card(Suit suit, Rank rank)
|
||||||
|
|
||||||
inline Suit Card::suit() { return s; }
|
inline Suit Card::suit() { return s; }
|
||||||
inline Rank Card::rank() { return r; }
|
inline Rank Card::rank() { return r; }
|
||||||
|
int Card::suitVal()
|
||||||
|
{
|
||||||
|
return int(s);
|
||||||
|
}
|
||||||
|
int Card::rankVal()
|
||||||
|
{
|
||||||
|
return int(r);
|
||||||
|
}
|
||||||
inline bool Card::isValid() { return valid; }
|
inline bool Card::isValid() { return valid; }
|
||||||
|
|
||||||
string Card::toString()
|
string Card::toString()
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "std_lib_facilities.h"
|
#include "std_lib_facilities.h"
|
||||||
|
|
||||||
|
|
||||||
enum class Suit { clubs, diamonds, hearts, spades };
|
enum class Suit { clubs, diamonds, hearts, spades };
|
||||||
|
|
||||||
enum class Rank { two = 2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace };
|
enum class Rank { two = 2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace };
|
||||||
|
@ -31,6 +30,8 @@ public:
|
||||||
|
|
||||||
Suit suit();
|
Suit suit();
|
||||||
Rank rank();
|
Rank rank();
|
||||||
|
int suitVal();
|
||||||
|
int rankVal();
|
||||||
bool isValid();
|
bool isValid();
|
||||||
string toString();
|
string toString();
|
||||||
string toStringShort();
|
string toStringShort();
|
||||||
|
|
|
@ -134,12 +134,14 @@
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="Blackjack.cpp" />
|
||||||
<ClCompile Include="Card.cpp" />
|
<ClCompile Include="Card.cpp" />
|
||||||
<ClCompile Include="CardDeck.cpp" />
|
<ClCompile Include="CardDeck.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="utilities.cpp" />
|
<ClCompile Include="utilities.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Blackjack.h" />
|
||||||
<ClInclude Include="Card.h" />
|
<ClInclude Include="Card.h" />
|
||||||
<ClInclude Include="CardDeck.h" />
|
<ClInclude Include="CardDeck.h" />
|
||||||
<ClInclude Include="utilities.h" />
|
<ClInclude Include="utilities.h" />
|
||||||
|
|
|
@ -27,6 +27,9 @@
|
||||||
<ClCompile Include="utilities.cpp">
|
<ClCompile Include="utilities.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Blackjack.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Card.h">
|
<ClInclude Include="Card.h">
|
||||||
|
@ -38,5 +41,8 @@
|
||||||
<ClInclude Include="utilities.h">
|
<ClInclude Include="utilities.h">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Blackjack.h">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -1,13 +1,14 @@
|
||||||
#include "std_lib_facilities.h"
|
#include "std_lib_facilities.h"
|
||||||
#include "CardDeck.h"
|
#include "CardDeck.h"
|
||||||
|
#include "Blackjack.h"
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
#pragma region Task 1
|
#pragma region Task 1
|
||||||
// a - d
|
// a - d
|
||||||
// Testing functions and mapping
|
// Testing functions and mapping
|
||||||
cout << suitToString(Suit::diamonds) << endl;
|
//cout << suitToString(Suit::diamonds) << endl;
|
||||||
cout << rankToString(Rank::ace) << endl;
|
//cout << rankToString(Rank::ace) << endl;
|
||||||
|
|
||||||
// e
|
// e
|
||||||
/*
|
/*
|
||||||
|
@ -19,18 +20,19 @@ int main(){
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
|
||||||
#pragma region Task 2
|
#pragma region Task 2
|
||||||
|
/*
|
||||||
CardStruct card1 = { Suit::spades, Rank::ace };
|
CardStruct card1 = { Suit::spades, Rank::ace };
|
||||||
CardStruct card2 = { Suit::diamonds, Rank::ten };
|
CardStruct card2 = { Suit::diamonds, Rank::ten };
|
||||||
cout << toString(card1) << endl;
|
cout << toString(card1) << endl;
|
||||||
cout << toStringShort(card2) << endl;
|
cout << toStringShort(card2) << endl;
|
||||||
|
|
||||||
|
*/
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
/*
|
|
||||||
Card c = Card(Suit::clubs, Rank::two);
|
Card c = Card(Suit::clubs, Rank::two);
|
||||||
|
|
||||||
cout << c.toString() << endl;
|
cout << c.toString() << endl;
|
||||||
*/
|
|
||||||
|
|
||||||
CardDeck cD = CardDeck();
|
CardDeck cD = CardDeck();
|
||||||
|
|
||||||
|
@ -45,6 +47,11 @@ int main(){
|
||||||
cout << c.toString() << endl;
|
cout << c.toString() << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Blackjack bJack = Blackjack();
|
||||||
|
|
||||||
|
bJack.printCards();
|
||||||
|
|
||||||
keep_window_open();
|
keep_window_open();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue