linked lists
Mar 31, 2013 at 9:56am UTC
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include "Card.h"
using namespace std;
class Deck
{
private :
Card c[52];
public :
Deck ()
{
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();
}
}
};
#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 "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 ;
}
Now the problem is that i can not access the functions of the class card as i have made an object of the class card in the class Node . please help me with this ..
Topic archived. No new replies allowed.