Remove and return card from deck

This is my code I'm currently stuck in a function in which I have to
//remove and return a random card from the deck
card dealRandomCard();

And down from that..
Please help
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
#include <iostream>
#include <string>
using namespace std;

class card
{
public:
      int number;
      string suit;
      card(int n, string s);
	  card();
	 
};

card::card(int n, string s)
{
	number = n;
	suit = s;
}
card::card()
{
	number = 0;
	suit = "";
}
 
class deck
{
private:
      //array to hold some cards.  Array is allocated by constructor
      card * cardArray;
     
      //size of cardArray, set by constructor
      int maxSize;     
 
      //Number of cards in deck.  Should not exceed maxSize.
      int numCards;
 
public:
      //default constructor:  create the standard 52 card deck
      deck();
 
      //parameterized constructor:  create a special deck that has
      //card numbers from 1 to numberRange and numSuits different suits.
      //In addition, the names of the different suits will be provided
      //by the array of strings 'suitList'.  This constructor should
      //create a card for each possible pairing of a number and a suit.
      //Therefore, there should be numberRange*numSuits total cards.
      //If numberRange=13, numSuits=4, and
      //suitList = {"heart","club","spade","diamond"}, then this constructor
      //should create the same deck that would be created by
      //the default constructor.
      deck(int numberRange, int numSuits, string * suitList);
 
      //place input card onto top of deck
      void addCardTop(card);
 
      //remove and return the card at the top of the deck
      card dealCardFromTop();
 
      //remove and return a random card from the deck
      card dealRandomCard();
 
      //rearrange all the cards in the deck into random order
      void shuffle();
 
      //Cut the deck at position'p':  swap the lower portion
      //of the card array (from 0 to p-1) with the
      //top portion of the deck (position p to numCards-1).
      void cut(int p);
 
      //arrange the cards into increasing order by card number.
      void sort();

	  void printDeck();
};

deck::deck()
{
	maxSize = 52;
	numCards = 0;
	cardArray = new card[maxSize];
	string suitArray[]={"Spades", "Clubs","Hearts","Diamonds"};
	for(int x=0;x<4;x++)
	{
		cout << "Making suit: " << suitArray[x] << endl;
		for(int i=0; i < 13; i++)
		{
			cardArray[x * 13+i].number = i + 1;
			cardArray[x * 13+i].suit = suitArray[x];
			numCards++;
		}
	}
}

void deck::addCardTop(card c)
{
	if(numCards==maxSize)
	{
		card* tempArray= new card [maxSize+5];

		for(int i=0;i<maxSize;i++)
		{
			tempArray[i]=cardArray[i];
		}
		delete[] cardArray;
		cardArray=new card[maxSize+5];
		for(int i=0;i<maxSize+5;i++)
		{
			cardArray[i]=tempArray[i];
		}
		cardArray[maxSize]=c;
		numCards++;
		maxSize+=5;
	}
	else
	{
		cardArray[numCards]=c;
		numCards++;
	}

	printDeck();
}

void deck::printDeck()
{
	for(int i = 0; i < numCards; i++)
	{
		cout << cardArray[i].suit << " " << cardArray[i].number << endl;
	}
}

card deck::dealCardFromTop()
{
	
}
card deck::dealRandomCard()
{

int temp = numCards - 1;
	numCards--;
	return cardArray[temp];

}



This is the other part of my code sorry bout that


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

#include "cardstuff.h"

using namespace std;

int main ()
{ 
	srand ( time(NULL) );
	deck* d = new deck();
	d->addCardTop(card(600, "HELLO"));
	
	d->printDeck();
	card c = d->dealCardFromTop();
	cout <<c.suit << " " << c.number << endl;
	
	
	
	return 0;
}
Last edited on
Hi,
i see that you use a basic c array from a pointer:

1
2
      //array to hold some cards.  Array is allocated by constructor
      card * cardArray;


you life will probably made easier if you use a list, something like

1
2
3
#include <list>
 (...)
list<card> cardArray


because it is much easier to remove an element from that, you do not need to reallocate and make a copy etc. as you would with basic c arrays.

google a bit on c++ stl list to see how it works, you will find it is easy.



to pick a random number you can use the "srand " function
Last edited on
Hello and yes I agree a list would be easier, but the proff wants us to use arrays..:/

I'm really stuck been starring at this for about an hour...
what's the part you are having trouble with?

-generating a random number ?
-removing an item from the array?
I think I have an idea of how to generate the random number please check it

1
2
3
4
5
6
7
card deck::dealRandomCard()
{
	int randc=rand()%52+1;
    card drawnCard=cardArray[randc];
  return cardArray[randc]; 

}


Dunno if it's wrong or not...
Also don't know how to output it and removing the item would be my main problem.
hi,

to remove the item you should do something very similar to what you did to add a card:
*create a copy of your vector
*destroy the old one
*make a new one a bit smaller
*copy the stuff from the copy to the new one excep the unsdesired element
*destroy the copy

Okay so I got it to create the random card
1
2
3
4
5
6
7
8
card deck::dealRandomCard()
{
	int randc=rand()%52+1;
    card drawnCard=cardArray[randc];
	
  return cardArray[randc]; 

}



Now I don't know how to delete the array
Okay so I got it to create the random card
Something to consider, if you are actually removing the card from the array, and shrinking the array, then your random logic will as is try to access memory outside the bounds of your "shrunk" array.

You may want to do something like

randc=rand()%numcards;



Last edited on
What I did was I swapped the random one I made to the top and than just decremented it like

numCards--

and it worked :)

Now I'm stuck in the shuffle mode
Topic archived. No new replies allowed.