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 273 274 275 276 277 278 279
|
// Go Fish
// Written by Neil Munroe
// 4/8/2013
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int SIZE = 32;
int ocean[SIZE];
int oceanPos;
void shuffle( int deck[], int& position );
void initialize( int deck[] );
void drawCard ( int deck[], int& position, int hand[], int& handSize, int &drawn );
int countRank( int rank, int hand[], int handSize );
void stealCards ( int rank, int from[], int& fromSize, int to[], int& toSize );
void checkSet ( int rank, int hand[], int& handSize, int& score, char pronoun[] );
void humanTurn ( int deck[], int humanHand[], int computerHand[], int& position, int& humanHandSize, int& computerHandSize, int& score );
void computerTurn ( int deck[], int computerHand[], int humanHand[], int& position, int& computerHandSize, int& humanHandSize, int& score );
int main()
{
char yesOrNo = 'y';
while ( yesOrNo == 'y' || yesOrNo == 'Y' )
{
// initialize deck
initialize ( ocean );
// shuffle deck
shuffle( ocean, oceanPos );
// initial deal
int humanHand[50];
int humanHandSize = 0;
int computerHand[50];
int computerHandSize = 0;
int drawn;
int humanScore = 0;
int computerScore = 0;
int i;
for ( i = 0; i < 5; i++ )
{
drawCard( ocean, oceanPos, humanHand, humanHandSize, drawn );
}
for ( i = 0; i < 5; i++ )
{
drawCard( ocean, oceanPos, computerHand, computerHandSize, drawn );
}
for ( i = 0; i < humanHandSize; i++ )
{
cout << humanHand[i] << " ";
}
for ( i = 0; i < computerHandSize; i++ )
{
cout << computerHand[i] << " ";
}
bool breaking = false;
while ( oceanPos != 3 && humanHandSize != 0 && computerHandSize != 0 && !breaking )
{
// human turn
humanTurn ( ocean, humanHand, computerHand, oceanPos, humanHandSize, computerHandSize, humanScore );
if ( computerHandSize == 0 || oceanPos == 31 )
{
breaking = true;
cout << "Game Over." << endl;
}
else
{
//computer turn
computerTurn ( ocean, computerHand, humanHand, oceanPos,computerHandSize, humanHandSize, computerScore );
}
if ( humanHandSize == 0 || oceanPos == 31 )
{
breaking = true;
cout << "Game Over." << endl;
}
}
cout << "*Final Score*" << endl;
cout << "You: " << humanScore << endl;
cout << "Computer: " << computerScore << endl;
cout << "Would you like to play again? (y/n) ";
cin >> yesOrNo;
}
}
void initialize ( int deck[] )
{
int i;
for ( i = 0; i < SIZE; i++ )
{
deck[i] = 2 + ( i % 8 );
}
}
void shuffle ( int deck[], int& position )
{
int i;
int j;
int temp;
for ( i = 0; i < SIZE; i++ )
{
j = 1 + rand() % 32;
temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
position = 0;
}
void drawCard ( int deck[], int& position, int hand[], int& handSize, int &drawn )
{
handSize++;
hand[handSize-1] = deck[position];
drawn = hand[handSize-1];
position++;
}
int countRank ( int rank, int hand[], int handSize )
{
int i;
int count = 0;
for ( i = 0; i < handSize; i++ )
{
if ( hand[i] == rank )
{
count++;
}
}
return count;
}
void stealCards ( int rank, int from[], int& fromSize, int to[], int& toSize )
{
int i;
for ( i = 0; i < fromSize; i++ )
{
if ( from[i] == rank )
{
toSize++;
to[toSize-1] = rank;
from[i] = from[fromSize-1];
fromSize--;
}
}
}
void checkSet ( int rank, int hand[], int& handSize, int& score, char pronoun[] )
{
int count = countRank( rank, hand, handSize );
int i;
if ( count == 4 )
{
for ( i = 0; i < handSize; i++ )
{
if ( hand[i] == rank )
{
hand[i] = hand[handSize - 1];
handSize--;
}
}
score++;
cout << pronoun << " scored a set!" << endl;
}
}
void humanTurn ( int deck[], int humanHand[], int computerHand[], int& position, int& humanHandSize, int& computerHandSize, int& score )
{
int count = 1;
char noun[] = "You";
bool breaking = false;
while ( count > 0 && !breaking )
{
int i;
cout << "You have: ";
for ( i = 0; i < humanHandSize; i++ )
{
cout << humanHand[i] << " ";
}
cout << endl;
int guess;
cout << "You ask if I have any ";
cin >> guess;
cout << endl;
count = countRank ( guess, computerHand, computerHandSize );
stealCards ( guess, computerHand, computerHandSize, humanHand, humanHandSize );
if ( count > 0 )
{
cout << "Yes I do. I have " << count << endl;
}
checkSet ( guess, humanHand, humanHandSize, score, noun );
if ( computerHandSize == 0 )
{
breaking = true;
}
}
if ( count == 0 )
{
cout << "No I don't. Go Fish!" << endl;
int drawn;
drawCard ( deck, position, humanHand, humanHandSize, drawn );
cout << "You draw a " << drawn << "." << endl;
cout << endl;
checkSet ( drawn, humanHand, humanHandSize, score, noun );
if ( position != 0 )
{
cout << "My turn!" << endl;
cout << endl;
}
}
}
void computerTurn ( int deck[], int computerHand[], int humanHand[], int& position, int& computerHandSize, int& humanHandSize, int& score )
{
int count = 1;
char noun[] = "I";
bool breaking = false;
while ( count > 0 && !breaking )
{
int i;
cout << "You have: ";
for ( i = 0; i < humanHandSize; i++ )
{
cout << humanHand[i] << " ";
}
cout << endl;
int guess = 2 + rand() % 8;
cout << "I ask: Do you have any " << guess << "'s?" << endl;
cout << endl;
count = countRank ( guess, humanHand, humanHandSize );
stealCards ( guess, humanHand, humanHandSize, computerHand, computerHandSize );
if ( count > 0 )
{
cout << "Yes you do. You have " << count << endl;
}
checkSet ( guess, computerHand, computerHandSize, score, noun );
if ( humanHandSize == 0 )
{
breaking = true;
}
}
if ( count == 0 )
{
cout << "No you don't. I will Go Fish." << endl;
int drawn;
drawCard ( deck, position, computerHand, computerHandSize, drawn );
checkSet ( drawn, computerHand, computerHandSize, score, noun );
if ( position != 0 )
{
cout << "Your turn" << endl;
cout << endl;
}
}
}
|