#include <stdio.h>
#include <stdlib.h>
#include "deck.h"
#include "ok.h"

int box_status[10];

struct generic_card reg_deck[52] = {
	DEUCE, CLUBS,     0, "Two of Clubs",      "2C",
	TREY,  CLUBS,     0, "Three of Clubs",    "3C",
	FOUR,  CLUBS,     0, "Four of Clubs",     "4C",
	FIVE,  CLUBS,     0, "Five of Clubs",     "5C",
	SIX,   CLUBS,     0, "Six of Clubs",      "6C",
	SEVEN, CLUBS,     0, "Seven of Clubs",    "7C",
	EIGHT, CLUBS,     0, "Eight of Clubs",    "8C",
	NINE,  CLUBS,     0, "Nine of Clubs",     "9C",
	TEN,   CLUBS,     0, "Ten of Clubs",      "10C",
	JACK,  CLUBS,     0, "Jack of Clubs",     "JC",
	QUEEN, CLUBS,     0, "Queen of Clubs",    "QC",
	KING,  CLUBS,     0, "King of Clubs",     "KC",
	ACE,   CLUBS,     0, "Ace of Clubs",      "AC",
	DEUCE, HEARTS,    0, "Two of Hearts",     "2H",
	TREY,  HEARTS,    0, "Three of Hearts",   "3H",
	FOUR,  HEARTS,    0, "Four of Hearts",    "4H",
	FIVE,  HEARTS,    0, "Five of Hearts",    "5H",
	SIX,   HEARTS,    0, "Six of Hearts",     "6H",
	SEVEN, HEARTS,    0, "Seven of Hearts",   "7H",
	EIGHT, HEARTS,    0, "Eight of Hearts",   "8H",
	NINE,  HEARTS,    0, "Nine of Hearts",    "9H",
	TEN,   HEARTS,    0, "Ten of Hearts",     "10H",
	JACK,  HEARTS,    0, "Jack of Hearts",    "JH",
	QUEEN, HEARTS,    0, "Queen of Hearts",   "QH",
	KING,  HEARTS,    0, "King of Hearts",    "KH",
	ACE,   HEARTS,    0, "Ace of Hearts",     "AH",
	DEUCE, DIAMONDS,  0, "Two of Diamonds",   "2D",
	TREY,  DIAMONDS,  0, "Three of Diamonds", "3D",
	FOUR,  DIAMONDS,  0, "Four of Diamonds",  "4D",
	FIVE,  DIAMONDS,  0, "Five of Diamonds",  "5D",
	SIX,   DIAMONDS,  0, "Six of Diamonds",   "6D",
	SEVEN, DIAMONDS,  0, "Seven of Diamonds", "7D",
	EIGHT, DIAMONDS,  0, "Eight of Diamonds", "8D",
	NINE,  DIAMONDS,  0, "Nine of Diamonds",  "9D",
	TEN,   DIAMONDS,  0, "Ten of Diamonds",   "10D",
	JACK,  DIAMONDS,  0, "Jack of Diamonds",  "JD",
	QUEEN, DIAMONDS,  0, "Queen of Diamonds", "QD",
	KING,  DIAMONDS,  0, "King of Diamonds",  "KD",
	ACE,   DIAMONDS,  0, "Ace of Diamonds",   "AD",
	DEUCE, SPADES,    0, "Two of Spades",     "2S",
	TREY,  SPADES,    0, "Three of Spades",   "3S",
	FOUR,  SPADES,    0, "Four of Spades",    "4S",
	FIVE,  SPADES,    0, "Five of Spades",    "5S",
	SIX,   SPADES,    0, "Six of Spades",     "6S",
	SEVEN, SPADES,    0, "Seven of Spades",   "7S",
	EIGHT, SPADES,    0, "Eight of Spades",   "8S",
	NINE,  SPADES,    0, "Nine of Spades",    "9S",
	TEN,   SPADES,    0, "Ten of Spades",     "10S",
	JACK,  SPADES,    0, "Jack of Spades",    "JS",
	QUEEN, SPADES,    0, "Queen of Spades",   "QS",
	KING,  SPADES,    0, "King of Spades",    "KS",
	ACE,   SPADES,    0, "Ace of Spades",     "AS"
};

/*
	Function:      Deck::Deck()

	Parameters:    none

	Description:   Deck constructor reshuffles the deck

	Returns:       none
*/

Deck::Deck()
{
	int number_shuffled = 0, i, card_number;

	for (i = 0; i < 52; i++) {
		reg_deck[i].used = 0;
      card[i].SetUsed(0);
   }

	while (number_shuffled < 52) {
		card_number = random(52);		  
		if (!reg_deck[card_number].used) {
			card[number_shuffled].SetValue(reg_deck[card_number].value);
         card[number_shuffled].SetSuit(reg_deck[card_number].suit);
         card[number_shuffled].SetDescription(reg_deck[card_number].description);
			card[number_shuffled].SetBitMapName(reg_deck[card_number].bitmapname);
			reg_deck[card_number].used = 1;
			number_shuffled++;
		}
	}
}


/*
	Function:      Deck::Deal()

	Parameters:    player_hand -- the player's seven card hand
                  dealer_hand -- the dealer's seven card hand

	Description:   Deals seven card hands to the player and dealer

	Returns:       none
*/

void 
Deck::Deal(Hand *player_hand, Hand *dealer_hand)
{
	int player_total = 0, dealer_total = 0, card_number, total_dealt = 0, i;
   char buffer[256];

	for (i = 0; i != 7; i++)
		box_status[i] = 0;			// make them all not selected

	while (total_dealt < 14) {
		if (player_total < 7) {
			card_number = random(52);

			if (!card[card_number].GetUsed()) {
				player_hand->card[player_total] = card[card_number];
				card[card_number].SetUsed(1);
				player_total++;
				total_dealt++;
			}
		}
		if (dealer_total < 7) {
			card_number = random(52);				//0 to 51

			if (!card[card_number].GetUsed()) {
				dealer_hand->card[dealer_total] = card[card_number];
				card[card_number].SetUsed(1);
				dealer_total++;
				total_dealt++;
			}
		}
	}
}

