// =============================================================================
// Solitare Simulator 
// Programmers: 	Gregory Perkins and Mike Petullo
// Class:			CS 66
// Due:				20 MAR 1997
// =============================================================================

#ifdef UNIX
typedef char Boolean;
const FALSE = 0;
const TRUE = 1;
#endif

#include "newdeal.h"
#include 

long SeedVal ();
// POST: FCTVAL == user input 
//    && FCTVAL > 0

short NumPiles ();
// POST: FCTVAL == user input
//    && 1 < FCTVAL <= 52

void DisplayPiles (short numPiles, CardType card[]);
// PRE:  numPiles > 0
//    && card[0...numPiles - 1] are assigned
// POST: cout == [stuff, card[0...numPiles - 1]

Boolean MatchFound (CardType card[], short& position1, short& position2, short numPiles);
// PRE:  card[0...numPiles - 1] are assigned
// POST: rank of card[position1] == rank of card[position2]
//    && FCTVAL == (a match was found)

// ==============================>  main () <==================================
int main ()
{
// Get seedVal and numPiles from cin
long seedVal = SeedVal (), drawCount = 0;
short numPiles = NumPiles ();

CardDeckType deck (seedVal);
CardType card[52];

for (short i = 0; i < numPiles; i++)						
	{
	// Deal initial cards to stacks
	card[i] = deck.DealACard ();
	cout << "Card drawn: ";
	card[i].display ();
	drawCount++;
	}

short position1, position2;
while (MatchFound (card, position1, position2, numPiles) && drawCount <= 50)
	{
	// ASSERT: rank of card[position1] == rank of card[position2]
	// Replace card[position1] with a newly dealt card
	card[position1] = deck.DealACard ();
	cout << "Card drawn: ";
	card[position1].display ();
	drawCount++;
	
	// Replace card[position1] with a newly dealt card
	card[position2] = deck.DealACard ();
	cout << "Card drawn: ";
	card[position2].display ();
	drawCount++;
	}

// ASSERT: card[0...numPiles - 1] all have unique ranks
//      || drawCount == 52
cout << endl << "Total cards drawn: " << drawCount << endl;
cout << endl << "Stack tops:" << endl;
DisplayPiles (numPiles, card);
return (0);
}

// ==============================>  SeedVal ()  <===============================
long SeedVal ()
// POST: FCTVAL == user input 
//    && FCTVAL > 0
{
long seedVal;
do
	{
	cout << "Please supply a positive integer to seed random numbers: ";
	cin >> seedVal;
	}	while (seedVal <= 0);
// ASSERT: seedVal > 0
cout << endl;
return (seedVal);
}

// ==============================>  NumPiles ()  <==============================
short NumPiles ()
// POST: FCTVAL == user input
//    && 1 < FCTVAL <= 52
{
short numPiles;
do
	{
	cout << "Please supply the number of piles you want dealt: ";
	cin >> numPiles;
	}	while (numPiles <= 1 || numPiles > 52);
// ASSERT: 1 < numPiles <= 52
cout << endl;
return (numPiles);
}

// ==============================>  DisplayPiles ()  <==========================
void DisplayPiles (short numPiles, CardType card[])
// PRE:  numPiles > 0
//    && card[0...numPiles] are assigned
// POST: cout == [stuff, card[0...numPiles - 1n]
{
for (short i = 0; i < numPiles; i++)
	{
	card[i].display ();
	}
}

// ==============================>  MatchFound ()  <============================
Boolean MatchFound (CardType card[], short& position1, short& position2, short numPiles)
// PRE:  card[0...numPiles - 1] are assigned
// POST: rank of card[position1] == rank of card[position2]
//    && FCTVAL == (a match was found)
{
	Boolean match = FALSE;	//assume that no matches are found
	
	for (position1 = 0; position1 < numPiles - 2; position1++)	//check the first to next-to-last piles 
		for (position2 = position1 + 1; position2 <= numPiles - 1; position2++)//compare with all other cards including the last pile
			if (card[position1].rank() == card[position2].rank())
			{
				match = TRUE;		//set to TRUE if a match was found
				return (match);		//return early if a match was found
			}
	
	return (match); 				//return final value of match if no match was found
	
}