////
//
// This file contains function definitions for the functions declared in
// card-play.h, q.v.
//
// NOTE: this file has no main function.  It is designed to be compiled with a
// separate test driver file.  For example, to test the card reading functions
// in this file, it is compiled with a separate test driver file as follows:
//
//     CC test-card-reads.cpp card-play.cpp -o test-card-reads
//
// File test-card-reads.cpp, q.v., contains a main driver function that calls
// the card reading functions in this file.
//
////

#include "card-play.h"
#include <iostream.h>
#include <fstream.h>


void ReadCardAsString(char card[CARD_STR_LEN]) {
    cout << "Input a card: ";
    cin >> card;
}

Boolean ReadCardAsIntAndChar(int& face_value, char& suit) {

    cout << "Input a card: ";

    //
    // Initialize the face value to 0 and suit to ' '.  If these initial values
    // are not changed, then an input error was detected.
    //
    face_value = 0;
    suit = ' ';

    //
    // Input the face value.
    //
    ReadFaceValue(face_value);

    //
    // If a legal face value was read, proceed to input the suit character.
    //
    if (face_value != 0) {
        ReadSuit(suit);
    }

    //
    // Ignore any remaining input chars up to the next newline.
    //
    cin.ignore(100, '\n');

    //
    // Return true if face value is non-zero and suit is non-blank, false
    // otherwise.
    //
    if ((face_value != 0) && (suit != ' ')) {
        return TRUE;
    }
    else {
        return FALSE;
    }
}

void ReadFaceValue(int& face_value) {

    char in_char;               // Temporary input char

    //
    // Input the next character from the terminal.
    //
    cin >> in_char;

    //
    // Read the next character from the terminal, and check it as legal face
    // value.  Note the trick for values 2 through 9, which subtracts the ASCII
    // value of the character '0' to obtain the integer value of the character.

    //
    // If the char is between 2 and 9, compute its value with a cute trick.
    // Viz., subtract the value of character '0' from the input char value.
    // This will convert the digit character value to its numeric integer
    // value.  See pages 518-520, and page A30 of the textbook for more info.
    //
    if ((in_char >= '2') && (in_char <= '9')) {
        face_value = in_char - '0';
    }

    //
    // If the input char is for an ace, jack, queen, or king, make its face
    // value 1, 11, 12, or 13, respectively.
    //
    else if (in_char == 'A') {
        face_value = 1;
    }
    else if (in_char == 'J') {
        face_value = 11;
    }
    else if (in_char == 'Q') {
        face_value = 12;
    }
    else if (in_char == 'K') {
        face_value = 13;
    }

    //
    // If the input char is a '1', make sure the next char is a '0', and make
    // the face value 10.
    //
    else if (in_char == '1') {
        cin >> in_char;
        if (in_char == '0') {
            face_value = 10;
        }
    }

    //
    // Check that a legal face value was read.  If not, output an error
    // message.
    //
    if (face_value == 0) {
        cout << "Illegal face value for card." << endl;
    }
}

void ReadSuit(char& suit) {

    char in_char;               // Temporary input char

    //
    // Input the next character from the terminal.
    //
    cin >> in_char;

    //
    // Check that the suit char is legal and output an error message if
    // not.
    //
    if ((in_char == 'C') || (in_char == 'D') ||
            (in_char == 'H') || (in_char == 'S')) {
        suit = in_char;
    }
    else {
        cout << "Illegal suit for card." << endl;
    }
}

void ReadDeckFromFile(ifstream& deck_file,
        char deck[DECK_SIZE][CARD_STR_LEN]) {
    int i;

    //
    // Loop through the input file, putting the file data values into
    // successive elements of the deck array.
    //
    for (i = 0; deck_file; i++) {
        deck_file >> deck[i];
    }
}

void DumpDeck(char deck[DECK_SIZE][CARD_STR_LEN]) {
    int i;

    //
    // Loop through the deck, outputting each element to the terminal.
    //
    for (i = 0; i < DECK_SIZE; i++) {
        cout << "Card " << i << ": " << deck[i] << endl;
    }
}