I need to add two things to my Animal Guessing Game and I'm not sure how to do them.
- saving your database tree and
- loading/reloading a database into your game.
For the first thing, our teacher told us that to save the tree, we will need 2 functions:
- Prepare an output file to print the tree to.
- Recursively traverse and print the tree using preorder traversal (node, left subtree, right subtree), printing each node as we come to it. We will start with the root node.
I plan to use the names :
- void displayPreOrder (animalNode* curNode) – using cout, as shown in our text
- void printPreOrder(animalNode* curNode) – using a file stream that we create
An example of the output file I am trying to do:
Q
does it have cubs for babies?
Q
does it perform in the circus?
G
Tiger
G
bear
Q
does it swim?
Q
did it swallow Jonah?
G
whale
G
fish
Q
does it eat cheese?
G
mouse
Q
does it purr?
G
cat
Q
is it afraid of mice?
G
elephant
Q
is it often nicknamed Billy?
G
goat
G
Lizard
This is the code I have so far (C++):
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
struct animalNode
{
string question;
string guess;
animalNode* yesAnswer, * noAnswer;
};
class Game
{
struct animalNode* root;
public:
Game()
{
root = new animalNode();
root->question = "";
root->guess = "lizard";
root->yesAnswer = NULL;
root->noAnswer = NULL;
}
void operation()
{
int end1 = 0; //learning a new animal:end game
int end2 = 0; //correctly guesses animal:end game
while (end1 == 0 && end2 == 0)
{
cout << "\nHello user, just think of an animal and Press enter to continue...\n";
cin.ignore();
animalNode* temp = root;
char answer;
string name, q;
while (1)
{
if (temp->question == "")
{
cout << "\nWere you thinking about " << temp->guess;
cout << "\n(y or n)";
cin >> answer;
if (answer == 'y' || answer == 'Y') //makes the input case insensitive
{
end2 = 1;
break;
}
else
{
animalNode* newYesAnswer, * newNoAnswer;
newYesAnswer = new animalNode();
newNoAnswer = new animalNode();
newYesAnswer->yesAnswer = NULL;
newYesAnswer->noAnswer = NULL;
newNoAnswer->yesAnswer = NULL;
newNoAnswer->noAnswer = NULL;
cin.ignore();
cout << "\nWhat animal it was?";
getline(cin, name); //adds a new animal to the guesses
cout << "\nA question that can be used to differentiate that animal?\n";
getline(cin, q); //adds a correlating question for the new animal
temp->question = q;
cout << "\nWhat would be its answer to identify it as " << name;
cout << "\nenter y for Yes and n for No: "; //verifies that the question matches the animal
cin >> answer;
if (answer == 'y' || answer == 'Y')
{
//answer yes=guesses the new animal
newYesAnswer->guess = name;
newNoAnswer->guess = temp->guess;
}
else
{
//answer no==guesses the new animal
newNoAnswer->guess = name;
newYesAnswer->guess = temp->guess;
}
temp->yesAnswer = newYesAnswer;
temp->noAnswer = newNoAnswer;
end1 = 1;
break;
}
}
else
{
cout << "\n" << temp->question;
cout << "\nEnter y for Yes and n for No: ";
cin >> answer;
if (answer == 'y' || answer == 'Y')
temp = temp->yesAnswer;
else
temp = temp->noAnswer;
}
}
if (end1 == 1) //prompt to play again if new animal was learned
{
cout << "\nDo you want to play again.. (y/n)";
cin >> answer;
if (answer == 'y' || answer == 'Y')
end1 = 0;
}
if (end2 == 1) //prompt to play again if animal was guessed
{
cout << "\nThe program guessed it right...\n";
cout << "\nDo you want to play again.. (y/n)";
cin >> answer;
if (answer == 'y' || answer == 'Y')
end2 = 0;
}
}
}
};
int main() //main function that runs the game
{
Game g;
g.operation();
return 0;
}