Help Make a Working Game of Hearts Program.

I'm stuck on this program, and Google doesn't seem to be working. I would've asked the teacher, but classes were canceled because of the degrading air quality. I'm assuming that Homework is still due on the same day though. Basically, I'm stuck and don't know what direction to go in. I'll post the full assignment here. I'm not asking for my homework to be done, but if you know any direction I can go in from here, it would be helpful. The game is detailed in the assignment if you don't know what Hearts is.

Assignment:
Homework 3 – Game of Hearts
Before writing any code, play a few games at playhearts-online.com
Write a program that plays the card game of Hearts. Your program should build on your code for
homework 2, including your sort and shuffle functions. Add members to your Card and Deck
classes as needed.
Create a new Player class with these member variables:
• Card hand[13] – stores cards dealt to player
• boolean played[13] – played[i] is true if hand[i] has been played, otherwise false
• int points – total number of points player has accumulated so far
• String name – name of player
Player has these member functions:
• void displayHand() – displays descriptions of cards in hand on console
• void addCard() – adds a card to hand during deal
• Card playCard(Suit lead, boolean leader) – returns the Card played – if leader is false,
card must be in the lead suit
Add constructors, get and set functions, and other members as needed.
Keep your main program simple. Most of your logic should be in member functions of classes.
See the following pages for sample display and rules of the game.
Challenge (optional - 1 bonus point)
When one player takes all 13 hearts and the queen of spades, instead of losing 26 points, that
player scores zero and each opponent scores 26 points. This is called ‘shooting the moon’.
Implement ‘shooting the moon’ in your program.
Challenge (optional – 1 bonus point)
Make your program smart enough so that any one of the computer players beats the TA two out
of three hands. You must follow two rules:
• You cannot ‘rig’ the deal. Shuffle and deal must be random.
• Players cannot see each other’s hands. Players are aware only of their own cards.

Display
Your display should look something like this:
Points Points
Player Round 3 Total
---------- ---------- -------
Me 0 0
Snoop Dogg 3 3
Lady Gaga 0 4
Elton John 0 0
My Hand
-------
1: Spade 9
2: Heart Ace
3: Heart 7
4:
5: Diamond Ace
6: Diamond 4
7:
8: Club Queen
9: Club Jack
10: Club 10
11: Club 9
12: Club 7
13:
Round 4
-----------
Snoop Dogg: Heart Jack
Lady Gaga: Diamond 6
Elton John: Heart 8
Your play? 3
Me: Heart 7

Rules of the Game
The Deal
There are four players, each with 13 cards initially. Players sit around a table. To deal, start with
any player and deal cards one at time clockwise.
The Play
Play proceeds in rounds, in which each player plays one card, in clockwise order around the
table. Playing the first card of a round is called ‘leading’. To start the first round, the player
holding the 2 of clubs must lead it. Subsequently, the winner of each round leads the next
round.
Following the first card of a round, the remaining players must play a card in the same suit. A
player with no cards in this suit may play a card in any other suit. The highest card of the suit
that was led wins the round.

Scoring
At the end of each game, players count the number of hearts they have won as well as the
queen of spades. Each heart counts as 1 point and the queen of spades counts as 13 points, for a
maximum of 26 possible points.
The player with the lowest score wins.

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
 #include "pch.h"
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
enum Suit { Spade, Heart, Diamond, Club };
class Card {
private:

	int number;
	string description;

public:
	Suit suit;
	void setNumber(int cNumber) {
		number = cNumber;
	}
	int getNumber() {
		return number;
	}
	void setSuit(Suit cSuit) {
		suit = cSuit;
	}
	Suit getSuit() {
		return suit;
	}
	void setDescription() {
		string temp = to_string(number);
		switch (number) {
		case 1:
			temp = "Ace";
			break;
		case 11:
			temp = "Jack";
			break;
		case 12:
			temp = "Queen";
			break;
		case 13:
			temp = "King";
			break;
		default:
			temp = to_string(number);
		}

		switch (suit) {
		case Spade:
			description = "Spade " + temp;
			break;
		case Heart:
			description = "Heart " + temp;
			break;
		case Diamond:
			description = "Diamond " + temp;
			break;
		case Club:
			description = "Club " + temp;
			break;
		default:
			break;
		}
	}
	string getDescription() {
		return description;
	}
	bool operator ==(const Card &c) {
		if (suit == c.suit && number == c.number)
			return true;
		return false;
	}
	bool operator <(const Card &c) {
		if (suit < c.suit)
			return true;
		if (suit == c.suit && c.number == 1)
			return true;
		else if (suit == c.suit && c.number != 1 && number < c.number)
			return true;
		return false;
	}
};
class Deck {
public:
	Card deck[52];

	Deck() {
		for (int i = 1; i < 14; i++) {
			for (int j = 1; j < 5; j++) {
				int pos = i * 4 + j - 5;
				switch (j) {
				case 1:
					deck[pos].setSuit(Club);
					deck[pos].getSuit();
					break;
				case 2:
					deck[pos].setSuit(Diamond);
					deck[pos].getSuit();
					break;
				case 3:
					deck[pos].setSuit(Heart);
					deck[pos].getSuit();
					break;
				case 4:
					deck[pos].setSuit(Spade);
					deck[pos].getSuit();
					break;
				default:
					break;
				}
				deck[pos].setNumber(i);
				deck[pos].setDescription();
			}
		}
	}
	void shuffleDeck() {
		for (int i = 0; i < 52; i++) {
			int r = rand() % 52;
			Card temp = deck[i];
			deck[i] = deck[r];
			deck[r] = temp;
		}
	}
};
class Player {
private:
	string name;

public:
	Card hand[13];
	int points;
	bool played[13];

	Player(string name) : name(name) {

	}
	void setName(string n) {
		name = n;
	}
	string getName() {
		return name;
	}
	void sort() {
		for (int i = 1; i < 13; i++) {
			auto save = hand[i];
			int j = i;
			for (; j > 0 && hand[j - 1] < save; --j)
				hand[j] = hand[j - 1];
			hand[j] = save;
		}
	}
	void displayHand() {
		for (int i = 0; i < 13; i++) {
			cout << left << i + 1 << ": " << hand[i].getDescription();
			cout << endl;
		}
	}
	void addCard() {

	}
	Card playCard(Suit lead, bool leader) {

	}
	/*void sort(Card array[], int size) {
		Card temp;
		bool swap;

		do {
			swap = false;
			for (int count = 0; count < (size - 1); count++) {
				if (array[count + 1] < array[count]) {
					temp = array[count];
					array[count] = array[count + 1];
					array[count + 1] = temp;
					swap = true;
				}
			}
		} while (swap);
	}*/
	int leader(Card value) {
		int size = 13;
		int first = 0;
		int last = size - 1;
		int middle;
		int position = -1;
		bool found = false;

		while (!found && first <= last) {
			middle = (first + last) / 2;
			if (hand[middle] == value) {
				found = true;
				position = middle;
			}
			else if (value < hand[middle])
				last = middle - 1;
			else
				first = middle + 1;
		}
		return position;
	}
	Player() {

	}
};
int main() {
	int seed = time(0);
	srand(seed);
	int m, i, j, results;
	Player leader;
	Deck deckHearts;
	Player p[4] = { {"Me"}, {"Snoop Dogg"}, {"Lady Gaga"}, {"Elton John"} };
	Card clubTwo = deckHearts.deck[4];
	Card played[4];

	do {
		cout << "Enter 1 to shuffle" << endl << "Enter 2 to deal" << endl << "Enter 3 to start game" << endl << "Enter 4 to stop" << endl;
		cin >> m;
		switch (m) {
		case 1:
			deckHearts.shuffleDeck();
			break;
		case 2:
			for (int i = 0; i < 13; i++)
				for (int j = 0; j < 4; j++)
					p[j].hand[i] = deckHearts.deck[j * 13 + i];

			for (int i = 0; i < 4; i++)
				p[i].sort();
			for (int i = 0; i < 4; i++) {
				results = p[i].leader(clubTwo);
				if (results == -1)
					cout << "Not Leader";
				else
					leader = p[i];
			}

			p[1].displayHand();

			//for (int i = 0; i < 4; i++)
			//	cout << left << setw(18) << p[i].getName();
			//cout << endl;

			//for (int i = 0; i < 13; i++) {
				//for (int j = 0; j < 4; j++)
					//cout << left << setw(18) << p[j].hand[i].getDescription();
				//cout << endl;
			//}
			break;
		case 3:
			for (i = 0; i < 13; i++) {
				for (j = 0; j < 4; j++) {
					//Convert j to p depending on leader
					played[p[j]] = p[j].playCard();
				}
				//determine winner
				//determine number of points in the round
				//assign points to winner
				//dispay hand
				//dispay points
				//set leader to the winner of the round
			}

			break;
		case 4:
			exit(1);
		}
	} while (m != 3);
	return 0;
}
Last edited on
That was the previous assignment. I guess I should title it something different then.
details... 'im stuck' is not helpful. what part is giving you stuckness?
making the AI pass intelligently and avoid runs, running itself, and generally playing half decent has always been a challenge. Its a simple game but never seen one that was better than a child at it (not counting cheating ones that look at the hands or collaborate against human as 3 to 1 AI).
Last edited on
Making the AI is the troubling part.
Ill bet :)
you need a smart pass function for the pass
you need to understand the queen of spades and how to drop it on others and avoid it yourself
you need to understand how to prevent someone from getting all 26 (including taking a hand just to force it)
you probably want to count cards (what has been played already, what is in your hand, and what is in the wild, which is different from looking at hands)

if its 3 of your ai vs the TA, you may want to coordinate. MS hearts does this; its AI will have one AI play to lose, and the others play to win, with all 3 trying to stick points to the human, so the loser ends the game, the human didn't win, one of the other 2 AI wins...

its an awful lot to do for 1 extra point!

It might help if you fire up the game and play it for a bit to learn the strategy, then try to drive your AI to play based off what you learn.
Last edited on
Topic archived. No new replies allowed.