#include "graphics.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

/* This is the name of the command that's executed to clear the display.
 * We use some preprocessor hackery to guess whether that should be
 * "clear" or "cls".  On Windows, it's "cls;" elsewhere it's "clear."
 */
#ifdef _MSC_VER /* This is only defined in Visual Studio */
	const string kClearCommand = "cls";
#else
	const string kClearCommand = "clear";
#endif

/* PrintCard(cardT &card)
 * Prints a card's value and suit.  This uses some low-order
 * ASCII characters and other hackery; apologies in advance.
 */
void PrintCard(cardT &card)
{
	switch(card.value)
	{
	case Ten:   cout << 'T'; break;
	case Jack:  cout << 'J'; break;
	case Queen: cout << 'Q'; break;
	case King:  cout << 'K'; break;
	case Ace:   cout << 'A'; break;
	default:
		cout << card.value + 1;
	}
	
	/* Low-order ASCII characters are playing card suits! */
	cout << (char(card.suit + '\x03')) << ' ';
}

/* Prints a cell.  If there's a card, it prints the card.  Otherwise,
 * it prints enough spaces to compensate.
 */
void PrintCell(cellT cell)
{
	if(cell.isFilled)
		PrintCard(cell.card);
	else
		cout << "   ";
}

/* Prints the game state.  This is a particularly uninteresting function,
 * except for the first line.
 */
void PrintState(gameT& game)
{
	/* Clear the display.  See the CS106L course reader's chapter on
	 * Snake for more explanation.
	 */
	system(kClearCommand.c_str());

	/* Print the done cells. */
	cout << "Done Cells: " << endl;
	for(int i = 0; i < game.done.size(); ++i)
		PrintCell(game.done[i]);
	cout << endl;

	/* Print the free cells. */
	cout << "Free Cells: " << endl;
	for(int i = 0; i < game.cells.size(); ++i)
	{
		cout << i << ": ";
		PrintCell(game.cells[i]);
	}
	cout << endl << endl;

	/* Print the piles. */
	for(int row = 0; row < game.piles.size(); ++row)
	{
		cout << "Row " << row << ": ";
		for(int col = 0; col < game.piles[row].size(); ++col)
			PrintCard(game.piles[row][col]);
		cout << endl;
	}
	cout << endl;
}
