All,
I have an assigment that I have to write several Classes for. I have an input text file of, say, 7 lines with 13 values per line.
Each value represents a card. I'm supposed to design a class to retrieve a card, CardReader, sends to the Hand class to build a hand of 13 playing cards. I can easily read the entire file into a large array or read only the 1st 13 values into an array. What I am not sure of is why I need two classes, one to send the value to another class that builds the hand. I find this redundant, but it is what I have to do. So, how would one read a card one at a time, send it to an array built in another class and stop after 13 cards or at the end of the line. How would one do this for every line after the previous one is processed and counted up for the amount of points in the hand?
It's obvious that I am not a bridge player, but have a lot of questions since I have only written programs with one class in it. I have included some code that I have written already.
CardReader class
Hand Class:
Any help would be greatly appreciated.
Thanks,
Todd
I have an assigment that I have to write several Classes for. I have an input text file of, say, 7 lines with 13 values per line.
Code:
QH 3D JD 7H 4C KC 9D 2H QC 6H JH QS 8C
4S 4C 9H 5H QC 8H JH 6C 3D 5S 9D 2C TH
AH TS AS QH QC TD 7H KS 2C 3S QS 3D 8D
8H TD 6D 8S JS 5C TS 3S KS 9S 6S 8D KD
7H 3C TC 4H QD JC 9H TS KH AC 6H 6D 9S
2C JH 3H JD TS 8C 7C 3D 5S TC 4S KD KS
JD QD JH 5S 3H 2S 5H QS 4D 2H AD 4H 7H
It's obvious that I am not a bridge player, but have a lot of questions since I have only written programs with one class in it. I have included some code that I have written already.
CardReader class
Code:
// cardreader.h: interface for the CardReader class.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
const int CARDS = 100;
//Declare an array of class CardReader
#ifndef CARDREADER
#define CARDREADER
class CardReader
{
private:
ifstream infile; //input file declaration
char theCards[CARDS][2];
char oneCard[2];
int size, count;
public:
CardReader(void); //constructor
~CardReader(void); //destructor
void readCards (void); //reads cards from a text file and stores
//into array
//void printCards (void); //print of cards
}; //end class CardReader
#endif
//*****************************************************************************
// readCards(): method reads data in the file, hands.txt, into the
// array and keeps track of (count) the number of cards read.
//-----------------------------------------------------------------------------
void CardReader::readCards (void)
{
count = 0;
while (!infile.eof()) //read all cards from file
{
infile >> oneCard; //read into temporary card first
strcpy(theCards[count], oneCard); //store word read in array
count++;
}
}//end readCards
Hand Class:
Code:
// hand.h: interface for the Hand class.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
const int HAND_SIZE = 13;
//Declare an array of class Hand
#ifndef HAND
#define HAND
class Hand
{
private:
ifstream infile; //input file declaration
char theHand[HAND_SIZE][2];
char oneCard[2];
int size, count;
public:
Hand(void); //constructor
~Hand(void); //destructor
void buildHand (void); //reads cards from a text file and stores
//into array
void printHand (void); //print of cards
}; //end class Hand
#endif
void Hand::buildHand (void)
{
count = 0;
while (count < 13) //read no more than 13 cards
{
//infile >> oneCard; //read into temporary card first
strcpy(theHand[count], oneCard); //store word read in array
count++;
// read next card in temporary location
}
}//end
Any help would be greatly appreciated.
Thanks,
Todd