//------------------------------------------------------------------
//-- Lab 10: this is the beginning of a Go Fish game.
//------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
/*******************************************************
* PROTOTYPES
*******************************************************/
// Cards are represented as follows:
// sv (suit value): 1=clubs, 2=diamonds, 3=hearts, 4=spades
// fv (face value): 2-10 are 2-10, 11=J, 12=Q, 13=K, 14=A
// cv (card value): cv = 100*sv + fv
int mkCv(int fv, int sv); // In: facevalue & suitvalue, Out: cardvalue
int cvFaceValue(int cv); // In: cardvalue, Out: facevalue
int cvSuitValue(int cv); // In: cardvalue, Out: suitvalue
string faceValueToString(int fv); // In: facevalue, Out: string representation
string suitValueToString(int sv); // In: suitvalue, Out: string representation
string cvToString(int cv); // In: cardvalue, Out: string representation
// Out: array of the 52 cardvalues in order, Side Effects: array is allocated
int* mkDeck();
// In: array deck of deckSize cardvalues
// Side Effects: the first deckSize elements of deck are randomly shuffled
void shuffle(int* deck, int deckSize);
/*******************************************************
* MAIN
*******************************************************/
int main(int argc, char** argv)
{
// if run with a command-line argument x, x used as seed (else time used)
srand(argc < 2 ? time(0) : atoi(argv[1]));
// shuffle deck and deal hands to players 1 and 2
int *deck = mkDeck(), *hand1 = new int[52], *hand2 = new int[52];
int deckSize = 52, hand1Size = 0, hand2Size = 0; // rep # cards in ...
shuffle(deck,deckSize);
for(int i = 0; i < 7; i++)
{
hand1[hand1Size++] = deck[--deckSize];
hand2[hand2Size++] = deck[--deckSize];
}
// create empty piles for books
int *books1 = new int[52], *books2 = new int[52];
int books1Size = 0, books2Size = 0;
// print hands!
cout << "Player1:";
for(int i = 0; i < hand1Size; i++)
cout << ' ' << cvToString(hand1[i]);
cout << endl;
cout << "Player2:";
for(int i = 0; i < hand2Size; i++)
cout << ' ' << cvToString(hand2[i]);
cout << endl;
return 0;
}
/*******************************************************
* DEFINITIONS
*******************************************************/
int mkCv(int fv, int sv) { return 100*sv + fv; }
int cvFaceValue(int cv) { return cv%100; }
int cvSuitValue(int cv) { return cv/100; }
string faceValueToString(int fv)
{
if (fv == 11) return "J";
if (fv == 12) return "Q";
if (fv == 13) return "K";
if (fv == 14) return "A";
if (fv == 10) return "10";
string s;
return s + char(fv + '0');
}
string suitValueToString(int sv)
{
// http://en.wikipedia.org/wiki/Playing_cards_in_Unicode
if (sv == 1) return "\u2663";
if (sv == 2) return "\u2666";
if (sv == 3) return "\u2665";
if (sv == 4) return "\u2660";
}
string cvToString(int cv)
{
string res = faceValueToString(cvFaceValue(cv));
if (res.length() < 2)
res = " " + res;
return res + suitValueToString(cvSuitValue(cv));
}
int * mkDeck()
{
int i = 0;
int * D = new int[52];
for(int s = 1; s <= 4; s++)
for(int f = 2; f <= 14; f++)
D[i++] = mkCv(f,s);
return D;
}
void shuffle(int* deck, int deckSize)
{ // same as in project 2
for(int i = 0; i < deckSize; i++)
{
int j = rand()%deckSize;
swap(deck[i],deck[j]);
}
}