In computer games and also in simulations of cardplaying sce
In computer games and also in simulations of card-playing scenarios, we some-times need to use a computer to simulate the way that person would shufe a deck of cards. Given two decks of n cards each, the rife shufe algorithm involvesrepeatedly choosing one of the two decks at random and then removing the bottom card from that deck and placing that card on the top of an output deck. Thiscard-choosing step is repeated until all the original cards are placed in the output deck. Dene a recursive-rife algorithm, which cuts a deck of n cards into two decks of n/ 2 each, where n is a power of 2 , and then calls the recursive-rife algorithm on each half. When these recursive calls return, the recursive-rife algorithm then performs a rife shufe of the two decks to produce the shufedresult. Show that every card in the original deck has an equal probability of being the top card after a recursive-rife is performed on the deck, and analyze the running time of the recursive-rife algorithm using a recurrence equation.
Solution
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int SIZE = 52;
class card
{
public:
card();
card(string cardFace, string cardSuit);
string print();
private:
string face;
string suit;
};
class deckOfCards
{
public:
deckOfCards();
void shuffle();
card dealCard();
private:
card deck[SIZE]; // an array of cards of size SIZR
int currentCard;
};
int main()
{
deckOfCards deck;
deck.shuffle();
for( int i = 0; i <= 2; i++)
{
currentCard = deck.dealCard();
//include to actually print out current card in the format we want
cout << currentCard.print() << endl;
}
return 0;
}
card::card()
{
}
card::card(string cardFace, string cardSuit)
{
face = cardFace;
suit = cardSuit;
}
string card::print()
{
return (face + \" of \" + suit);
}
deckOfCards::deckOfCards()
{
string faces[] = {\"Ace\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"Jack\", \"Queen\", \"King\"};
string suits[] = {\"Hearts\", \"Diamonds\", \"Clubs\", \"Spades\"};
deck = new card[SIZE];
currentCard = 0;
for(int count = 0; count < SIZE; count++)
{
deck[count] = card(faces[count % 13], suits[count / 13]);
}
}
void deckOfCards::shuffle()
{
currentCard = 0;
for(int first = 0; first < SIZE; first++)
{
int second = (rand() + time(0)) % SIZE;
card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
}
card deckOfCards::dealCard()
{
if(currentCard > SIZE)
{
shuffle();
}
if( currentCard < SIZE)
{
return (deck[currentCard++]);
}
return (deck[0]);
}

