-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo_fish.cpp
More file actions
272 lines (228 loc) · 8.7 KB
/
go_fish.cpp
File metadata and controls
272 lines (228 loc) · 8.7 KB
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
268
269
270
271
272
// FILE: card_demo.cpp
// This is a small demonstration program showing how the Card and Deck classes are used.
#include <iostream> // Provides cout and cin
#include <fstream>
#include <cstdlib> // Provides EXIT_SUCCESS
#include "card.h"
#include "player.h"
#include "deck.h"
using namespace std;
// PROTOTYPES for functions used by this demonstration program:
void dealHand(Deck &d, Player &p, int numCards);
void testCard();
void testDeckInit();
void testDeckShuffle();
void testGame();
bool roundOfGame(Player &p1, Player &p2, Deck &d1, ofstream &fout);
void gameResults(Player &p1, Player &p2, ofstream &fout);
void emptyHanded(Player &p, Deck &d1, ofstream &fout);
bool moreCards(Player &p1, Player &p2, Deck &d1);
void stateOfGame(Player &p1, Player &p2, Deck d1);
int main (){
//testCard();
//testDeckInit();
//testDeckShuffle();
testGame();
return 0;
}
void testCard(){
Card c1;
Card c2(13,Card::hearts);
Card c3 = c2;
cout << "Card 1: " << c1.toString() << endl;
cout << "Card 2: " << c2.toString() << endl;
cout << "Card 3: " << c3.toString() << endl;
if (c1==c2){
cout << "Card 1 == Card 2: TRUE" << endl;
}
else if (c1!=c2){
cout << "Card 1 != Card 2: TRUE" << endl;
}
else {
cout << "Something is seriously wrong here" << endl;
}
if (c2==c3){
cout << "Card 2 == Card 3: TRUE" << endl;
}
else if (c2!=c3){
cout << "Card 2 != Card 3: TRUE" << endl;
}
else {
cout << "Something is seriously wrong here" << endl;
}
}
void testDeckInit(){
Deck d1;
cout << "Starting size: " << d1.size() << endl;
d1.showDeck();
//cout << "Ending size: " << d1.size() << endl;
}
void testDeckShuffle(){
cout << endl << "*** TESTING Deck::shuffle() ***" << endl;
Deck d1;
cout << "Original Deck:" << endl;
d1.showDeck();
d1.shuffle();
cout << "Shuffled Deck:" << endl;
d1.showDeck();
d1.shuffle();
cout << "Shuffled again:" << endl;
d1.showDeck();
}
void testGame(){
Deck d1;
Player john("John");
Player bob("Bob");
ofstream fout("gofish_results.txt");
d1.shuffle();
for (int i=0; i<7; i++){
john.addCard(d1.dealCard());
bob.addCard(d1.dealCard());
}
//cout << "John's Initial Hand: " << john.showHand() << endl;
fout << "John's Initial Hand: " << john.showHand() << endl;
//cout << "Bob's Initial Hand: " << bob.showHand() << endl << endl;
fout << "Bob's Initial Hand: " << bob.showHand() << endl;
Card j1, j2, b1, b2;
while(john.checkHandForBook(j1,j2)){
john.bookCards(j1,j2);
john.removeCardFromHand(j1);
john.removeCardFromHand(j2);
}
while(bob.checkHandForBook(b1,b2)){
bob.bookCards(b1,b2);
bob.removeCardFromHand(b1);
bob.removeCardFromHand(b2);
}
if (john.getBookSize()>0){
//cout << "John Books: " << john.showBooks() << endl;
fout << "John Books: " << john.showBooks() << endl;
}
else {
//cout << "John Books: no cards." << endl;
fout << "John Books: no cards." << endl;
}
if (bob.getBookSize()>0){
//cout << "Bob Books: " << bob.showBooks() << endl;
fout << "Bob Books: " << bob.showBooks() << endl;
}
else{
//cout << "Bob Books: no cards" << endl;
fout << "Bob Books: no cards" << endl;
}
//cout << endl;
fout << endl;
//Main loop of game
while (moreCards(john,bob,d1)){
while((moreCards(john,bob,d1))&&(roundOfGame(john,bob,d1,fout)));
while((moreCards(john,bob,d1))&&(roundOfGame(bob,john,d1,fout)));
}
//Printing the results of the game to the screen
gameResults(john,bob,fout);
}
bool roundOfGame(Player &p1, Player &p2, Deck &d1, ofstream &fout){
bool guessedRight;
Card guess;
Card returnedCard;
Card drawnCard;
guess = p1.chooseCardFromHand();
//cout << p1.getName() << " asks - Do you have a " << guess.rankString(guess.getRank()) << "?" << endl;
fout << p1.getName() << " asks - Do you have a " << guess.rankString(guess.getRank()) << "?" << endl;
if(p2.rankInHand(guess)){
//they will get another turn since they guessed right
guessedRight = true;
//cout << p2.getName() << " says - Yes. I have a " << guess.rankString(guess.getRank()) << "." << endl;
fout << p2.getName() << " says - Yes. I have a " << guess.rankString(guess.getRank()) << "." << endl;
returnedCard = p2.removeRankFromHand(guess);
p1.bookCards(guess,returnedCard);
p1.removeCardFromHand(guess);
//cout << p1.getName() << " books " << guess.toString() << " and " << returnedCard.toString() << "." << endl;
fout << p1.getName() << " books " << guess.toString() << " and " << returnedCard.toString() << "." << endl;
//Checking if either player's hand is empty, and if deck is not empty
//If yes, will draw a new card for them
emptyHanded(p1,d1,fout);
emptyHanded(p2,d1,fout);
}
else{
guessedRight = false;
//cout << p2.getName() << " says - Go Fish." << endl;
fout << p2.getName() << " says - Go Fish." << endl;
if (d1.size()!=0){
drawnCard = d1.dealCard();
//cout << p1.getName() << " draws " << drawnCard.toString() << "." << endl;
fout << p1.getName() << " draws " << drawnCard.toString() << "." << endl;
if (p1.rankInHand(drawnCard)){
Card matchingCard = p1.removeRankFromHand(drawnCard);
p1.bookCards(drawnCard, matchingCard);
//cout << p1.getName() << " has a pair. " << p1.getName() << " books " << drawnCard.toString() << " and " << matchingCard.toString() << "." << endl;
fout << p1.getName() << " has a pair. " << p1.getName() << " books " << drawnCard.toString() << " and " << matchingCard.toString() << "." << endl;
//If hand is empty after booking, draw another card
emptyHanded(p1,d1,fout);
}
else{
p1.addCard(drawnCard);
}
}
else{
//cout << "Deck is empty. No card is drawn." << endl;
fout << "Deck is empty. No card is drawn." << endl;
}
}
//cout << endl;
fout << endl;
return guessedRight;
}
void gameResults(Player &p1, Player &p2, ofstream &fout){
//cout << "RESULTS:" << endl;
fout << "RESULTS:" << endl;
//cout << p1.getName() << " has " << p1.getBookSize() << " cards booked." << endl;
fout << p1.getName() << " has " << p1.getBookSize() << " cards booked." << endl;
//cout << p2.getName() << " has " << p2.getBookSize() << " cards booked." << endl;
fout << p2.getName() << " has " << p2.getBookSize() << " cards booked." << endl;
if (p1.getBookSize()>p2.getBookSize()){
//cout << p1.getName() << " wins!" << endl;
fout << p1.getName() << " wins!" << endl;
}
else if (p1.getBookSize()<p2.getBookSize()){
//cout << p2.getName() << " wins!" << endl;
fout << p2.getName() << " wins!" << endl;
}
else{
//cout << "It's a tie!" << endl;
fout << "It's a tie!" << endl;
}
}
//Checks if the player is empty handed
//Draws a new card for them if they are empty handed and the deck has more cards remaining
void emptyHanded(Player &p, Deck &d1, ofstream &fout) {
if (p.getHandSize() == 0) {
//If the deck has 1 or less cards, the game will end
if (d1.size() == 0) {
//Done
//********************************************
//guessedRight = false;
} else {
Card drawnCard = d1.dealCard();
p.addCard(drawnCard);
//cout << p.getName() << "'s hand is empty. " << p.getName() << " draws " << drawnCard.toString() << endl;
fout << p.getName() << "'s hand is empty. " << p.getName() << " draws " << drawnCard.toString() << endl;
}
}
}
//Checks if there are more cards (can the game be continued?)
//returns true if the sum of all the cards in the deck and players' hands is > 0
//returns false if the sum is 0
bool moreCards(Player &p1, Player &p2, Deck &d1){
if ((d1.size()+p1.getHandSize()+p2.getHandSize())!=0) return true;
else return false;
}
//For testing purposes, shows the current state of the game and players' hands
void stateOfGame(Player &p1, Player &p2, Deck d1){
cout << "Deck: ";
d1.showDeck();
cout << p1.getName() << "'s hand: " << p1.showHand() << endl;
cout << p1.getName() << "'s book size: " << p1.getBookSize() << endl;
cout << p2.getName() << "'s hand: " << p2.showHand() << endl;
cout << p2.getName() << "'s book size: " << p2.getBookSize() << endl;
cout << endl;
}