Beginning of class array is always the same

I am working on a crazy eights game.
I created a card class that has a Suit enum and a number for its value.
I created a vector of the card types and put them into the deck
it randomly shuffles the deck and then puts it into the player deck
when I print out the player deck the first card is always "H0" (Hearts 0):
H0 SQ SK SJ S10 S9 S8

What is the solution for this?

Code:

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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

enum Suits
{
	Hearts = 0, Diamonds, Clubs, Spades
};

class Card
{
	private:
		Suits suit;
	int value;
	public:
		void printCard()
		{
			std::string first, second;
			switch (this->suit)
			{
				case Suits::Hearts:
					first = "H";
					break;
				case Suits::Diamonds:
					first = "D";
					break;
				case Suits::Clubs:
					first = "C";
					break;
				case Suits::Spades:
					first = "S";
					break;
				default:
					break;
			}
			switch (this->value)
			{
				case 0:
					second = "0";	// only printing out 0 ortherwise it wont show up
					break;
				case 1:
					second = "1";
					break;
				case 2:
					second = "2";
					break;
				case 3:
					second = "3";
					break;
				case 4:
					second = "4";
					break;
				case 5:
					second = "5";
					break;
				case 6:
					second = "6";
					break;
				case 7:
					second = "7";
					break;
				case 8:
					second = "8";
					break;
				case 9:
					second = "9";
					break;
				case 10:
					second = "10";
					break;
				case 11:
					second = "J";
					break;
				case 12:
					second = "K";
					break;
				case 13:
					second = "Q";
					break;
				default:
					break;
			}
			std::cout << first << second;
		}
	public:
		Card(Suits suit, unsigned int value)
		{
			this->suit = suit;
			this->value = value;
		}
	Suits getSuit() const
	{
		return this->suit;
	}
	int getValue() const
	{
		return this->value;
	}
};

class Game
{
	public:
		void play()
		{
			// I never put any 0 in here
			this->deck.push_back(Card(Suits::Hearts, 1));
			this->deck.push_back(Card(Suits::Hearts, 2));
			this->deck.push_back(Card(Suits::Hearts, 3));
			this->deck.push_back(Card(Suits::Hearts, 4));
			this->deck.push_back(Card(Suits::Hearts, 5));
			this->deck.push_back(Card(Suits::Hearts, 6));
			this->deck.push_back(Card(Suits::Hearts, 7));
			this->deck.push_back(Card(Suits::Hearts, 8));
			this->deck.push_back(Card(Suits::Hearts, 9));
			this->deck.push_back(Card(Suits::Hearts, 10));
			this->deck.push_back(Card(Suits::Hearts, 11));
			this->deck.push_back(Card(Suits::Hearts, 12));
			this->deck.push_back(Card(Suits::Hearts, 13));
			this->deck.push_back(Card(Suits::Diamonds, 1));
			this->deck.push_back(Card(Suits::Diamonds, 2));
			this->deck.push_back(Card(Suits::Diamonds, 3));
			this->deck.push_back(Card(Suits::Diamonds, 4));
			this->deck.push_back(Card(Suits::Diamonds, 5));
			this->deck.push_back(Card(Suits::Diamonds, 6));
			this->deck.push_back(Card(Suits::Diamonds, 7));
			this->deck.push_back(Card(Suits::Diamonds, 8));
			this->deck.push_back(Card(Suits::Diamonds, 9));
			this->deck.push_back(Card(Suits::Diamonds, 10));
			this->deck.push_back(Card(Suits::Diamonds, 11));
			this->deck.push_back(Card(Suits::Diamonds, 12));
			this->deck.push_back(Card(Suits::Diamonds, 13));
			this->deck.push_back(Card(Suits::Clubs, 1));
			this->deck.push_back(Card(Suits::Clubs, 2));
			this->deck.push_back(Card(Suits::Clubs, 3));
			this->deck.push_back(Card(Suits::Clubs, 4));
			this->deck.push_back(Card(Suits::Clubs, 5));
			this->deck.push_back(Card(Suits::Clubs, 6));
			this->deck.push_back(Card(Suits::Clubs, 7));
			this->deck.push_back(Card(Suits::Clubs, 8));
			this->deck.push_back(Card(Suits::Clubs, 9));
			this->deck.push_back(Card(Suits::Clubs, 10));
			this->deck.push_back(Card(Suits::Clubs, 11));
			this->deck.push_back(Card(Suits::Clubs, 12));
			this->deck.push_back(Card(Suits::Clubs, 13));
			this->deck.push_back(Card(Suits::Spades, 1));
			this->deck.push_back(Card(Suits::Spades, 2));
			this->deck.push_back(Card(Suits::Spades, 3));
			this->deck.push_back(Card(Suits::Spades, 4));
			this->deck.push_back(Card(Suits::Spades, 5));
			this->deck.push_back(Card(Suits::Spades, 6));
			this->deck.push_back(Card(Suits::Spades, 7));
			this->deck.push_back(Card(Suits::Spades, 8));
			this->deck.push_back(Card(Suits::Spades, 9));
			this->deck.push_back(Card(Suits::Spades, 10));
			this->deck.push_back(Card(Suits::Spades, 11));
			this->deck.push_back(Card(Suits::Spades, 12));
			this->deck.push_back(Card(Suits::Spades, 13));

			for (int i = this->deck.size(); i > (int) this->deck.size() - 7; i--)
			{
				this->playerDeck.push_back(this->deck[i]);
				this->deck.erase(this->deck.end(), this->deck.end());
			}

			for (int i = 0; i < this->playerDeck.size(); i++)
			{
				this->playerDeck[i].printCard();
				std::cout << " ";
			}
			std::cout << std::endl;
		}
	private:
		std::vector<Card> deck;
	std::vector<Card> playerDeck;
	std::vector<Card> computerDeck;
	Card * currentCard;
};

int main()
{
	srand(time(NULL));
	rand();
	Game *myGame = new Game();
	myGame->play();
	return 0;
}

You are always trying to push back the card one beyond the end of the array on line 166.

That's well and truly ... not defined.


You are also trying to erase a non-existent card on line 167.
Last edited on
Topic archived. No new replies allowed.