Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <Windows.h>
- using namespace std;
- template <typename T>
- class BTNode
- {
- T data;
- BTNode*pLeft;
- BTNode*pRigt;
- public:
- BTNode(const T& _data = T(), BTNode *_pLeft = nullptr, BTNode *_pRigt = nullptr) : data(_data), pLeft(_pLeft), pRigt(_pRigt) {}
- void setData(const T&_data)
- {
- data = _data;
- }
- T getData()
- {
- return data;
- }
- void setpRight(BTNode*_pRigt)
- {
- pRigt = _pRigt;
- }
- void setpLeft(BTNode*_pLeft)
- {
- pLeft = _pLeft;
- }
- const BTNode *getpRight()const
- {
- return pRigt;
- }
- BTNode *getpRight()
- {
- return pRigt;
- }
- const BTNode *getpLeft()const
- {
- return pLeft;
- }
- BTNode *getpLeft()
- {
- return pLeft;
- }
- bool isLeaf()const
- {
- if (pLeft == nullptr&&pRigt == nullptr)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- };
- void fillTree(BTNode<string>*&root)
- {
- root = new BTNode<string>("Оно Живет в воде?");
- BTNode<string>*temp = new BTNode <string>("У него есть жабры?");
- temp->setpLeft(new BTNode<string>("Щука"));
- temp->setpRight(new BTNode<string>("Лягушка"));
- root->setpLeft(temp);
- temp = new BTNode <string>("у него 4 лапы?");
- temp->setpLeft(new BTNode<string>("Кот"));
- temp->setpRight(new BTNode<string>("Змея"));
- root->setpRight(temp);
- }
- template <typename T>
- void printTree(BTNode <T>*root)
- {
- if (root != nullptr)
- {
- printTree(root->getpLeft());
- //cout << root->getData() << endl;
- printTree(root->getpRight());
- cout << root->getData() << endl;
- //cout << root->getData() << endl;
- }
- }
- void learn(BTNode<string>*pCurrent)
- {
- string correctAnswer, wrongAnswer, question;
- cout << "Я сдаюсь! что это за животное?\n";
- getline(cin, correctAnswer);
- wrongAnswer = pCurrent->getData();
- cout << "Какой вопрос может помочь их различить?\n ";
- getline(cin, question);
- pCurrent->setData(question);
- cout << "Какой ответ для " << correctAnswer << '?';
- string answer;
- getline(cin, answer);
- if (answer == "yes" || answer == "y")
- {
- pCurrent->setpLeft(new BTNode<string>(correctAnswer));
- pCurrent->setpRight(new BTNode<string>(wrongAnswer));
- }
- else if (answer == "no" || answer == "n")
- {
- pCurrent->setpLeft(new BTNode<string>(wrongAnswer));
- pCurrent->setpRight(new BTNode<string>(correctAnswer));
- }
- }
- bool play(BTNode<string>*root)
- {
- BTNode<string>*pCurrent = root;
- string answer;
- while (pCurrent->isLeaf()==false)
- {
- cout << pCurrent->getData() << endl;
- getline(cin,answer);
- //cin.ignore(1, '\n');
- if (answer=="yes"|| answer == "y")
- {
- pCurrent = pCurrent->getpLeft();
- }
- else if (answer == "no" || answer == "n")
- {
- pCurrent = pCurrent->getpRight();
- }
- }
- cout << "Я думаю это " << pCurrent->getData();
- cout << " Это верно?\n";
- getline(cin, answer);
- //cin.ignore(1, '\n');
- if (answer == "yes")
- {
- cout << "Урра!\n";
- }
- else
- {
- learn(pCurrent);
- cout << "Продолжим игру?\n";
- getline(cin, answer);
- //cin.ignore(1, '\n');
- if (answer == "yes" || answer == "y")
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- void main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- //setlocale(LC_ALL, "Russian");
- BTNode <string>*root = nullptr;
- fillTree(root);
- //printTree(root);
- while (play (root)==true)
- {
- play(root);
- }
- //play(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement