Nearly done

master
Øyvind Skaaden 2019-02-07 13:20:06 +01:00
parent e5301efdfd
commit b1fef061e4
9 changed files with 122 additions and 6 deletions

View File

@ -5,14 +5,11 @@ map<Suit, string> suitNames = { {Suit::clubs, "Clubs" },
{Suit::hearts, "Hearts"},
{Suit::spades, "Spades"} };
string suitToString(Suit s)
{
return suitNames[s];
}
map<Rank, string> rankNames = { {Rank::two, "Two" },
{Rank::three, "Three"},
{Rank::four, "Four"},
@ -32,7 +29,6 @@ string rankToString(Rank r)
return rankNames[r];
}
string toString(CardStruct card)
{
return rankToString(card.rank) + " of " + suitToString(card.suit);
@ -46,6 +42,8 @@ string toStringShort(CardStruct card)
}
#pragma region Card - Class
Card::Card()
{
valid = false;
@ -77,3 +75,5 @@ string Card::toStringShort()
else
return "Invalid card";
}
#pragma endregion

View File

@ -0,0 +1,53 @@
#include "CardDeck.h"
#include "utilities.h"
CardDeck::CardDeck()
{
currentCardIndex = 0;
for (int s = 0; s < 4; s++)
{
for (int r = 2; r < 2 + 13; r++)
{
cards.push_back(Card((Suit)s, (Rank)r));
}
}
}
void CardDeck::swap(int fromIndex, int toIndex)
{
Card c = cards[fromIndex];
cards[fromIndex] = cards[toIndex];
cards[toIndex] = c;
}
void CardDeck::print()
{
for (Card c : cards)
{
cout << c.toString() << endl;
}
}
void CardDeck::printShort()
{
for (Card c : cards)
{
cout << c.toStringShort() << endl;
}
}
void CardDeck::shuffle(int depth)
{
srand(static_cast<unsigned int>(time(nullptr)));
for (int i = 0; i < depth; i++)
{
swap(randomWithLimits(0, cards.size() - 1), randomWithLimits(0, cards.size() - 1));
}
}
const Card& CardDeck::drawCard()
{
++currentCardIndex;
return cards[currentCardIndex - 1];
}

View File

@ -0,0 +1,18 @@
#pragma once
#include "Card.h"
class CardDeck
{
public:
CardDeck();
void print();
void printShort();
void shuffle(int depth = 10000);
const Card& drawCard();
private:
vector<Card> cards;
int currentCardIndex;
void swap(int fromIndex, int toIndex);
};

View File

@ -135,10 +135,14 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Card.cpp" />
<ClCompile Include="CardDeck.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="utilities.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Card.h" />
<ClInclude Include="CardDeck.h" />
<ClInclude Include="utilities.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -21,10 +21,22 @@
<ClCompile Include="Card.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="CardDeck.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="utilities.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Card.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="CardDeck.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="utilities.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,5 +1,5 @@
#include "std_lib_facilities.h"
#include "Card.h"
#include "CardDeck.h"
int main(){
@ -26,10 +26,25 @@ int main(){
#pragma endregion
/*
Card c = Card(Suit::clubs, Rank::two);
cout << c.toString() << endl;
*/
CardDeck cD = CardDeck();
cD.print();
cD.shuffle();
cD.printShort();
for (int i = 0; i < 52; i++)
{
Card c = cD.drawCard();
cout << c.toString() << endl;
}
keep_window_open();
return 0;
}

View File

@ -0,0 +1,8 @@
#include "utilities.h"
#include "std_lib_facilities.h"
int randomWithLimits(int min, int max)
{
return rand() % (max - min + 1) + min;;
}

View File

@ -0,0 +1,6 @@
#pragma once
int randomWithLimits(int min, int max);

Binary file not shown.