Card game template

Pages: 12
closed account (o1vk4iN6)
You're for loop is overly complicated, you shouldn't even need the variable "std::string suit". Essentially you are allocating dynamic memory (string does this) to store it into "suit", you than pass "suit" into your constructor, another copy is made, and than you copy it again into "card::suit".

To put it in short, you are storing "Hearts", "Spades", "Diamonds", "Clubs" in 4 places in memory at one time.

Again this loop is more straight forward and easier to read and puts it in the order you want.

1
2
3
4
5
6
7
8
9
for(int i = 0; i < 52; i++){
    switch( i / 12 ){
        case 0: cards[i] = Card(i % 12 + 1, "Hearts"); break;
        case 1: cards[i] = Card(i % 12 + 1, "Clubs"); break;
        case 2: cards[i] = Card(i % 12 + 1, "Spade"); break;
        case 3: cards[i] = Card(i % 12 + 1, "Diamond"); break;
        default: cerr << "undefined behavior\n"; break;
   }
}


Enums are simply a list of integers each given a specific name.
Last edited on
Xendraw wrote:
i need the cards stored in order though i.e. ace of hearts, 2 of hearts....king of hearts, ace of spades ect, reading this seems it will store it; ace of hearts, ace of spades etc.


Then simply change the order of your for() loops:

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
int i = 0; // index into array

for(int s = 0; s < 4; ++s) // select suit
{
	for(int c = 1; c < 14; ++c) // select card value
	{
		// now make a Card using the values c and s
		std::string suit;

		switch(s)
		{
			case 0: suit = "Hearts";
			break;

			case 1: suit = "Spades";
			break;

			case 2: suit = "Diamonds";
			break;

			case 3: suit = "Clubs";
			break;
		};

		c[i] = Card(c, suit);

		++i; // ready for next position in the array
	}
}

Now for every successive suit each card in turn will be selected.
ok i have completed it, and it works fine, thank you all very much for your help
Topic archived. No new replies allowed.
Pages: 12