hey guys ! i have a problem relating to linked lists ..
Make a class called Card. Each card is represented by a suit and a rank. You can keep both as strings. Implement getters and setters for the Card class as well as a print function. A 7 of spades should be printed as 7S or 7♠.
Create a class Deck which contains 52 cards. Implement a print function which prints outs all cards in the deck. Also implement a shuffle function. You can use any algorithm to shuffle a deck. The simplest one is dividing the deck into two parts of random size and then placing the lower part on top of the upper part. However, you must implement the deck as a linked list of cards. Each node in this list should have a pointer to a unique Card object and a pointer to the next node. This means that there should never be more (or less) than 52 Card objects in the whole program at any time.
I have printed all the cards and now i want to shuffle them :
My code is :
#ifndef CARD_H
#define CARD_H
#include <string>
#include<iomanip>
using namespace std;
class Card
{
private :
string suit ;
string rank ;
public :
void Setsuit(string Suit)
{
suit = Suit ;
}
string GetSuit ()
{
return suit ;
}
void SetRank( string Rank)
{
rank = Rank ;
}
string GetRank ()
{
return rank ;
}
void Print_Cards()
{
cout << rank << setw(1) <<suit << endl;
}
};
#endif
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include "Card.h"
using namespace std;
class Deck
{
private:
Card c[52];
public :
Deck () // constructor for a deck which initializes every rank
{
int index = 0 ;
for (int i = 2 ; i<=9 ; i++ )
{
c[index].Setsuit("H");
string rank;
ostringstream convert;
convert << i;
rank = convert.str();
c[index].SetRank(rank);
index++;
}
c[index].Setsuit("H");
c[index].SetRank("A");
index++ ;
c[index].Setsuit("H");
c[index].SetRank("k");
index++ ;
c[index].Setsuit("H");
c[index].SetRank("Q");
index++ ;
c[index].Setsuit("H");
c[index].SetRank("J");
index++ ;
c[index].Setsuit("H");
c[index].SetRank("10");
for (int i = 2 ; i<=9 ; i++ )
{
c[index].Setsuit("C");
string rank;
ostringstream convert;
convert << i;
rank = convert.str();
c[index].SetRank(rank);
index++;
}
c[index].Setsuit("C");
c[index].SetRank("A");
index++ ;
c[index].Setsuit("C");
c[index].SetRank("k");
index++ ;
c[index].Setsuit("C");
c[index].SetRank("Q");
index++ ;
c[index].Setsuit("C");
c[index].SetRank("J");
index++ ;
c[index].Setsuit("C");
c[index].SetRank("10");
index++ ;
for (int i = 2 ; i<=9 ; i++ )
{
c[index].Setsuit("S");
string rank;
ostringstream convert;
convert << i;
rank = convert.str();
c[index].SetRank(rank);
index++;
}
c[index].Setsuit("S");
c[index].SetRank("A");
index++ ;
c[index].Setsuit("S");
c[index].SetRank("k");
index++ ;
c[index].Setsuit("S");
c[index].SetRank("Q");
index++ ;
c[index].Setsuit("S");
c[index].SetRank("J");
index++ ;
c[index].Setsuit("S");
c[index].SetRank("10");
index++ ;
for (int i = 2 ; i<=9 ; i++ )
{
c[index].Setsuit("D");
string rank;
ostringstream convert;
convert << i;
rank = convert.str();
c[index].SetRank(rank);
index++;
}
c[index].Setsuit("D");
c[index].SetRank("A");
index++ ;
c[index].Setsuit("D");
c[index].SetRank("k");
index++ ;
c[index].Setsuit("D");
c[index].SetRank("Q");
index++ ;
c[index].Setsuit("D");
c[index].SetRank("J");
index++ ;
c[index].Setsuit("D");
c[index].SetRank("10");
index++ ;
}
void Print_cards_in_a_Deck()
{
for (int i = 0 ; i<52 ; i++ )
{
c[i].Print_Cards();
}
}
};
#include <iostream>
#include "Card.h"
#include "Deck.h"
using namespace std;
void main ()
{
Card c;
Deck d ;
c.SetRank("A");
c.Setsuit("Hearts");
d.Print_cards_in_a_Deck();
system("pause");
}
#include <iostream>
#include "Card.h"
#include"Deck.h"
using namespace std;
class Node
{
public :
Card c ;
Node *next ;
};
class List
{
private :
Node* head ;
Node* tail ;
public :
Now i can not access the functions of the class card in the class Node .. please help me with this ..