#include #include #include using namespace std; #include "Animal.h" #include "QuestionNode.h" ////////////////////////////////////////// // Recursive function that plays the game. void Play(QuestionNode *&qN) { // Complete this code for Task 1. } void saveMaybe(string sFN, QuestionNode *qNS[]) { if (0 == getAnswer("Save this run to '"+sFN+"'?", "yes,no")) { ofstream saveOut(sFN.c_str()); if (saveOut) { cout << "Saving tree to textfile '"+sFN+"'" << flush; for (unsigned i= 0; i<3; i++) QuestionNode::Save(saveOut, qNS[i], 0); dots(3); // 1438. cout << " done." << endl; } else { cout << "Could not save to '"+sFN+"'." << endl; } saveOut.close(); } else cout << "Not saved." << endl; } /////////////////////////////////////////// // Play the game from the root, repeatedly. #define DEFAULT_DATA "animalData" int main(int argc, char *argv[]) { string loadFileName= DEFAULT_DATA, saveFileName= DEFAULT_DATA; // Default file names. if (argc >= 2 && string(argv[1]) == "-help") { cout << "guess [" << loadFileName << " [" << saveFileName << "]]" << endl; exit(0); } // Reminder of syntax. if (argc > 4) { cout << "Try guess -help." << endl; exit(0); } if (argc >= 2) loadFileName= argv[1]; // First parameter is load file name, if present. if (argc >= 3) saveFileName= argv[2]; else saveFileName= loadFileName; // Second parameter is save file name, if present. QuestionNode *qNS[3]= {NULL, NULL, NULL}; ifstream loadIn(loadFileName.c_str()); if (loadIn) { cout << "Loading tree from textfile '"+loadFileName+ "'" << flush; for (unsigned i= 0; i<3; i++) qNS[i]= QuestionNode::Load(loadIn); for (unsigned i= 0; i<3; i++) { system("sleep 1"); cout << "." << flush; } // 1438: For dramatic effect. cout << " done." << endl << endl; } else { cout << "No file '"+loadFileName+"' found: starting from scratch." << endl << endl; } while (1) { unsigned i= getAnswer("Is it animal, vegetable or mineral?", "animal,vegetable,mineral,save,quit"); if (i == 3) { saveMaybe(saveFileName, qNS); continue; } if (i == 4) { cout << "Quitting: "; break; } QuestionNode *&qN= qNS[i]; // qN now the one we're using for this round. if (qN == NULL) { // Has this branch been defined? cout << "What is it? "; string firstThing; getline(cin, firstThing); qN= new QuestionNode("Is it "+firstThing+"?"); cout << "It's "+firstThing+", eh? Now that's settled, let's try again." << endl; } else Play(qN); // It has been defined, so play it. cout << endl; // Separator from next round. } saveMaybe(saveFileName, qNS); }