/**********************************************************
 * Bender's blackjack functions 
 *
 * Warning: Don't add or change code here!
 **********************************************************/
#include <stdio.h>
#include <stdlib.h>

#define MAX_NUM_OF_RANKS 13    


// The following enum and char array are used internally.
enum suit { SPADES, HEARTS, DIAMONDS, CLUBS }; 

char cardArray[MAX_NUM_OF_RANKS] = {
	'2', '3', '4', '5', '6', '7', '8', '9', '1', 'J', 'Q', 'K', 'A'
};


/********************************************************** 
 * drawRandomCard returns an int between 0 ~ 51. 
 *
 * Each card in the deck is numbered from 0 ~ 51.
 *  0 ~ 12 : Spades   2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
 * 13 ~ 25 : Hearts   2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
 * 26 ~ 38 : Diamonds 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
 * 39 ~ 51 : Clubs    2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
 *
 * Example: 
 *  If the card number returned by this function is 23, 
 *  the card is Queen of Hearts. 
 *
 * Use result with printCard() and getCardValue() functions.
 **********************************************************/
int drawRandomCard()
{
	int cardnum;
	cardnum = rand() % 52;
	return cardnum;
}

/********************************************************** 
 * This function is for internal use within this file. 
 * It checks whether cardnum is an int between 0 ~ 51, both inclusive
 **********************************************************/
void cardnumSanityCheck(int cardnum)
{
	if( cardnum < 0 || cardnum > 51 )
	{
		printf("Card number has to be between 0 ~ 51!\n");
		exit(0);
	}
}

/********************************************************** 
 * Prints suit and rank of card. 
 *
 * Suit is one of Spades, Hearts, Diamonds, or Clubs.
 * Rank is one of 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A.
 *
 * Example: 
 *  If the card number is 23, it prints "HQ " (Hearts, Queen). 
 *  If the card number is 47, it prints "C10 " (Clubs, 10)). 
 *
 * !!! WARNING !!!
 * Warning: does not print newline character! 
 *          Only prints one space after.
 **********************************************************/
void printCard(int cardnum)
{
	int position, mysuit; 
	char result[] = {'\0','\0','\0','\0'};

	cardnumSanityCheck(cardnum);

	switch (cardnum / 13)
	{
		case SPADES:
			result[0] = 'S';
			break;
		case HEARTS:
			result[0] = 'H';
			break;
		case DIAMONDS:
			result[0] = 'D';
			break;
		case CLUBS:
			result[0] = 'C';
			break;
	}

	result[1] = cardArray[cardnum % 13];

	if (cardnum % 13 == 8)
		result[2] = '0';

	printf("%s ", result);
}

/********************************************************** 
 * Returns the value of card. 
 *
 * Numbered cards (2, 3, 4, 5, 6, 7, 8, 9, 10) are worth 
 * the number printed on the card.
 *
 * Face cards (J, Q, K) are worth 10.
 *
 * Aces are worth 1 or 11, but the function returns 11.
 *
 * Example: 
 *  If the card number is 23, it returns 10 (Hearts, Queen). 
 *  If the card number is 16, it returns 5 (Hearts, 5). 
 *
 * !!! WARNING !!!
 * If the card is an Ace, 11 is returned. 
 * If you want the Ace to be 1 instead of 11, you need to 
 * deal with it within your own function.
 **********************************************************/
int getCardValue(int cardnum)
{
	int position, value;

	cardnumSanityCheck(cardnum);

	position = cardnum % 13;

	if (position < 9)
		value = position + 2;
	else if (position < 12)
		value = 10;
	else
		value = 11;	

	return value; 
}

/********************************************************** 
 * Reads an integer from user input 
 * Skips spaces, tabs, and newlines.
 **********************************************************/
int readInt()
{
	char input[500];
	int number;
	if (fgets(input, sizeof(input), stdin))
	{
		if ( sscanf(input, "%d", &number) == 1 )
			return number;
		else 
			printf("Please enter an integer!\n");
	}
}

/********************************************************** 
 * Reads a character from user input
 * Skips spaces, tabs, and newlines.
 **********************************************************/
char readChar()
{
	char input[500];
	char c;
	if ( fgets(input, sizeof(input), stdin))
	{
		int i = 0;
		while ( input[i] != '\0' && isspace(input[i]) ) 
			++i;

		if (input[i] == '\0')
			printf("Please enter a character!\n");

		c = input[i];
	}
	return c;
}

