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 ..

Can you please put up the code in which you are trying to access the member functions of class "card" via a "Node" object? I believe that would help a bit in answering your question.

Edited 3 Years Ago by deltascrow: forgot to type a few words

Here's some code that should help. I revised a few things and added a simple shuffle routine(basically pick 2 random cards and swap them then repeat 1000 times). I also included a sample of a node class that inherits Card and uses Card's functions. The routines here should make it relatively easy to implement a linked list.

#include <string>
#include <iostream>
#include <ctime>
using namespace std;
class Card
{
private :
    string _suit ;
    string _rank ;
public :
    Card()
    {
        _suit = "";
        _rank = "";
    }
    void SetSuit(string suit)
    {
        _suit = suit ;
    }
    string GetSuit ()
    {
        return _suit ;
    }
    void SetRank( string rank)
    {
        _rank = rank ;
    }
    string GetRank ()
    {
        return _rank ;
    }
    void Print_Cards()
    {
        cout << GetRank() << GetSuit() << endl;
    }
};
class Deck
{
public :
    Card c[52];
    Deck () // constructor for a deck which initializes every rank
    {
        string Rank[] = {"A","2","3","4","5","6","7","8","9","T","J","Q","K"};
        string Suit[] = {"C","D","H","S"};
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<13; j++)
            {
                c[j+i+(i*12)].SetRank(Rank[j]);
                c[j+i+(i*12)].SetSuit(Suit[i]);
            }
        }
    }
    void Shuffle()
    {
        int firstcard;
        int nextcard;
        srand( (unsigned)time( NULL ) );
        for(int i=0; i<1000; i++)
        {

            firstcard = rand()%52;
            nextcard = rand()%52;
            Card tempcard;
            tempcard = c[firstcard];
            c[firstcard] = c[nextcard];
            c[nextcard] = tempcard;
        }           
    }
    void Print_cards_in_a_Deck()
    {
        for (int i = 0 ; i<52 ; i++ )
        {
            c[i].Print_Cards();
        }
    }
};
class CardNode : public Card
{
public:
    CardNode * next;
};
int main ()
{
    Card c;
    c.SetRank("A");
    c.SetSuit("H");
    c.Print_Cards();
    Deck d;
    d.Print_cards_in_a_Deck();
    cin.get();
    cout <<endl;
    d.Shuffle();
    d.Print_cards_in_a_Deck();
    cin.get();
    CardNode cn;
    cn.SetRank("2");
    cn.SetSuit("D");
    cn.Print_Cards();
    cin.get();
    return 0;
}

Edited 3 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.