Im having some problem in shuffling the deck. There are some repetition after it shuffle. Any solution for it>?
#include <iostream>
#include <windows.h>
#include "stdlib.h"
#include <cstdlib>
#include <ctime>
using namespace std;
const static int BLACK = 0;
const static int BLUE = 9;
const static int RED = 12;
const static int WHITE = 15;
class Card{
public :
Card();
Card(char cardRank,char cardSuit);
~Card();
char getCardSuit();
char getCardRank();
int getSolitaireValue();
friend std::ostream & operator<< (std::ostream & os, Card& c);
private :
bool showCard;
char cardRank, cardSuit;
};
Card::Card(){}
Card::Card(char r, char s) : cardRank(r), cardSuit(s), showCard(false){}
Card::~Card(){}
char Card::getCardSuit()
{
return cardSuit;
}
char Card::getCardRank()
{
return cardRank;
}
int Card::getSolitaireValue()
{
if(cardRank == 'A')
return 1;
else if(cardRank == 'T')
return 10;
else if(cardRank == 'J')
return 11;
else if(cardRank == 'Q')
return 12;
else if(cardRank == 'K')
return 13;
}
std::ostream & operator<< (std::ostream & os, Card& c)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
//make the suits colour blue and red
if(c.cardSuit == char(5) || c.cardSuit == char(6))
SetConsoleTextAttribute(handle, BLUE);
else
SetConsoleTextAttribute(handle,RED);
// make frame white colour
os<< c.cardSuit << c.cardRank;
SetConsoleTextAttribute(handle,WHITE);
return os;
}
const static int RANK_SIZE = 13; // declare cardRank size to 13 (A-K)
const static int SUIT_SIZE = 4; // declare cardSuit size to 4 (H,D,S,C)
const static char RANKS[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'};
const static char SUITS[] = {char(3), char(4), char(5), char(6)}; //{'h','d','s','c'};
class Deck{
private :
Card cardDeck[52];
int currentIndex;
public :
Deck();
~Deck();
void populate();
void shuffle();
void printDeck();
};
Deck::Deck() : currentIndex(0)
{
populate();
}
Deck::~Deck(){}
void Deck::populate() //Populate column with cards
{
int index = 0;
for(int i=0; i<SUIT_SIZE; i++)
{
for(int j=0; j<RANK_SIZE;j++)
{
cardDeck[index] = Card(RANKS[j],SUITS[i]);
index++;
}
}
}
void Deck::shuffle()
{
int max = SUIT_SIZE * RANK_SIZE;
for(int i=0; i<max; i++)
{
int randNum = rand() % 52;
swap(cardDeck[i],cardDeck[randNum]);
cout<<cardDeck[i]<<" ";
}
}
int main()
{
Deck deck = Deck();
deck.shuffle();
}