Hello! I am working on a final project for my intermediate C++ class. For the project concepts to be utilized we are supposed to implement the following:
Opening screen with a description of the application and instructions
Menu for the user to choose options
At least 4 classes total
Inheritance (minimum 2 derived classes)
Polymorphism (Overloading and overriding)
Encapsulation
File input and output processing
Multi-Threading
Exception handling
Abstraction
I used an example of a Black Jack game in one of my books to get started
for the file I/O I created and saved a document as an input file with information pertaining to the tarot cards to display.
I intend on allowing the user to save readings to an output file.
I was thinking of somehow trying to figure out how to implement multithreading within the cards being drawn but I need to clean up the mess I have made first. So far only the opening screen runs correctly.
Your help is sincerely greatly appreciated. I'm hoping I can get this completed in time.
//functions for menu options
void startReading();
void retrieveData();
void viewTarot();
//menu options
char menu()
{
char response;
cout << "Would you like to - ";
cout << "[S]tart a new reading, [R]etrieve a previous reading, [V]iew up tarot card information or [Q]uit?" << endl;
cin >> response;
cin.ignore(256, '\n');
return toupper(response);
}
//starts card game simulation
void startReading()
{
system("pause");
system("CLS");
}
//displays last saved reading
void retrieveData()
{
system("pause");
system("CLS");
}
//opens text file
if (infile.is_open())
{
//outputs text file to console
if (outfile.is_open())
{
//reads through file until the end
while (getline(infile, writeLine))
{
cout << writeLine << endl;
outfile << writeLine << "\n"; //prints line
} //end of while
outfile.close(); //closes output file
} //end of outfile
//error if output file can not be opened
else cout << "Unable to open output file. \n" << endl;
infile.close(); //closes input file
} //end of infile
//error if input file can not be opened
else cout << "Unable to open input file. \n" << endl;
system("pause");
system("CLS");
} //end of void
}; //end of base class
//Base class - Opening menu to simulator
class startSim
{
private:
string name;
public:
//user input function
void getInput()
{
cout << "Welcome to Tarot Divination\n" << endl;
cout << "Please enter your name: " << endl;
cin >> name;
cout << "\nHello " << name << "!\n" << endl;
cout << "Prepare to enter your path to destiny..." << endl;
int Card::GetValue() const
{
//if card is face down its value is 0
int value = 0;
if (m_ifu)
{
value = m_rank;
if (value > 10)
{
value = 10;
}
}
return value;
}
void Card::Flip()
{
m_ifu = !(m_ifu);
}
//designed for a collection of cards
class Hand
{
public:
Hand();
virtual ~Hand();
void Hand::Clear()
{
//iterate through vector, freeing all memory on the heap
vector<Card*>::iterator iter = m_cards.begin();
for (iter = m_cards.begin(); iter != m_cards.end(); ++iter)
{
delete *iter;
*iter = 0;
}
//clear vector of pointers
m_cards.clear();
}
int Hand::GetTotal() const
{
//if no cards in hand return 0
if (m_cards.empty())
{
return 0;
}
//if first card has a value of 0, card is face down; return 0;
if (m_cards[0]->GetValue() == 0)
{
return 0;
}
//add card values, treat aces as 1
int total = 0;
vector<Card*>::const_iterator iter;
for(iter = m_cards.begin(); iter != m_cards.end(); ++iter)
{
total += (*iter)->GetValue();
}
//determine if hand contains an ace
bool containsAce = false;
for (iter = m_cards.begin(); iter != m_cards.end(); ++iter)
{
if ((*iter)->GetValue() == Card::ACE)
{
containsAce = true;
}
}
//wrote same bool param for every Major Arcana here as well
for (int s = Card::WANDS; s <= Card::SWORDS; ++s)
{
for (int r = Card::ACE; r <= Card::KING; ++r)
{
for (int a = Card::FOOL; a <= Card::WORLD; ++a)
{
Add(new Card(static_cast<Card::rank>(r), static_cast<Card::suit>(s), static_cast<Card::arcana>(a)));
}
}
}
}
//menu selection switch
bool run = true;
do
{
switch (menu())
{
case 'S': startReading(); break;
case 'R': retrieveData(); break;
case 'V': viewTarot(); break;
case 'Q': run = false; break;
}
} while (run);
I was thinking of somehow trying to figure out how to implement multithreading within the cards being drawn but I need to clean up the mess I have made first. So far only the opening screen runs correctly.
First design, then code. It looks like you're 'coding on the hoof' which isn't the way to do it. You're ended up with loads of code that doesn't do what you want - and as you say, "is a mess".
Design the game. What input/output/processing/algorithms/data structures/classes are needed? If more than one class, what are the relationships between them?
Once you have the design, then you start to code from the design. You code in small parts and after each part you have a working program. The more your code the nearer the required result. Each part builds upon the previous part which you know is correct because you're tested it before you start to code the next part.
If something doesn't compile work, then you know the problem is with the last part that you coded.
This way, when you're coded the last part you have (or should have if your design is right) a correct working program.
What you don't do is to start off writing the code for the whole program, then set about testing/debugging/changing it.