//----------------------------------------------------------------------
//  IMPLEMENTATION FILE (dealer.cpp)
//  This module exports CardValType, SuitType, and CardDeckType
//  to randomly deal playing cards.
//  Machine dependency: long ints must be at least 32 bits (4 bytes).
//----------------------------------------------------------------------
#include "dealer.h"

// Private members of class:
//     RandGen cardGen
//     int     cardsRemaining
//     int     card[52]           For each card[i],
//                                   card value == card[i] modulo 13 and
//                                   suit == card[i] / 13
//
// CLASSINV: card[0..cardsRemaining-1] contain the undealt portion
//           of the deck

CardDeckType::CardDeckType( /* in */ long initSeed ) : cardGen(initSeed)
    //..................................................................
    // Constructor
    // PRE:  initSeed >= 1
    // POST: initSeed has been passed to cardGen constructor
    //    && The deck has been shuffled
    //..................................................................
{
    ShuffleDeck();
}

void CardDeckType::ShuffleDeck()
    //..................................................................
    // POST: cardsRemaining == 52
    //    && For all i, (0<=i<=51), card[i] == i
    //       (So card[0] is two of clubs, card[1] is three of clubs, ...
    //        card[12] is ace of clubs, card[13] is two of diamonds, ...)
    //..................................................................
{
    int i;

    for (i = 0; i < 52; i++)
        card[i] = i;
    cardsRemaining = 52;
}

void CardDeckType::DealACard( /* out */ CardValType& cardVal,
                              /* out */ SuitType&    suit     )
    //..................................................................
    // PRE:  cardsRemaining >= 1
    // POST: Some card[r] has been chosen at random from
    //       card[0..cardsRemaining<entry>-1]
    //    && cardVal == card[r] modulo 13
    //    && suit == card[r] / 13
    //    && card[r] has been "removed" by swapping it
    //       with card[cardsRemaining<entry>-1]
    //    && cardsRemaining == cardsRemaining<entry> - 1
    //..................................................................
{
    int randNo;
    int temp;

    randNo = int( cardGen.NextRand()*float(cardsRemaining-1) );

    cardVal = CardValType( card[randNo] % 13 );     // Type cast required
    suit = SuitType( card[randNo] / 13 );

    temp = card[randNo];
    card[randNo] = card[cardsRemaining-1];
    card[cardsRemaining-1] = temp;

    cardsRemaining--;
}

int CardDeckType::CardsInDeck() const
    //..................................................................
    // POST: FCTVAL == cardsRemaining
    //..................................................................
{
    return cardsRemaining;
}


