In my last post in the series I published the card class. This class will now be used in the deck class. Additionally, this class also uses the random class which was part of my first post of this series.
As before, I am going to post the header files used used in all of the classes. However, this time I will include the header files that include the randomNumber.h and card.h files.
#include <cstdlib>
#include <iostream>
#include <cstdlib> //Used for rand() & srand()
#include <string>
#include <fstream>
//Used to just make all kinds of crazy random numbers
#include "randomNumbers.h"
//Used to make an individual card object
#include "card.h"
The deck class is actually a very simple class. It serves two purposes. First, it creates an entire deck of cards using the card class. The other main purpose is to actually shuffle the cards. I would like to emphasize the real lack of thoroughness by using this shuffle method. More about the later. Here is the code, but remember this code requires the RandomGenerator and card class.
class deck
{
private:
card myCard[52];
int cardPosition;
RandomGenerator number;
public:
deck();
void resetDeck();
void printCard(int);
void printDeck();
void shuffleCards();
card dealCard();
};
deck::deck()
{
resetDeck();
}
void deck::resetDeck()
{
cardPosition = 0;
for(int i = 0; i < 52; i++)
{
card newCard(i);
myCard[i] = newCard;
}
}
void deck::printCard(int myIndex)
{
(myCard[myIndex]).printCard();
cout << " ";
}
void deck::printDeck()
{
for(int i = 0; i < 52; i++)
printCard(i);
}
void deck::shuffleCards()
{
//Cycles through entire deck
for (int i = cardPosition; i < 52; i++)
{
//Picks a random card
int r = number.cardIndex();
card temp = (myCard[i]);
//Swaps the cards
myCard[i] = myCard[r];
myCard[r] = temp;
}
}
card deck::dealCard()
{
//Deck is dealt from 0 to 51
if(cardPosition < 52)
{
return myCard[cardPosition++];
}
else
{
//return NULL;
}
}
Looking at this code, it becomes obvious that there are quite a few ways to arrange a deck of playing cards. I believe it is equal to 52! which is a huge number. I had to look it up on a table and found it to be equal to 8065817517094387857166063685640376
6975289505440883277824000000000000 (that is all one number, I just split it in half so it would mess with my blog's formatting.) Not to be totally obvious, but I'm not going to run this many test to see what the results to every possible game of war is (not yet at least, or with this type of implementation).
For my purposes, I'm just going to run a very small sample of games. The code I currently have is not able to predetermine a deck of cards. A future version might, but that would require indexing all the possible combinations and 52! is a ton of possible combinations and indexes. I'll leave that headache for another day. If I experiment with having all possible combinations of War played out, it would most likely be with a smaller deck before I moved up to such large numbers. I can only imagine how long these computations would take.
To above code is very simple. It simply serves to make a deck of cards. Then the cards will be shuffled. Then the cards will be dealt out. This is all the above deck is used for, nothing else. Once the deck is dealt out, it will no longer be used. The cards will be tracked using the code that will be part of my next post.
No comments:
Post a Comment