Advertisement
Timtsa

Game

Dec 17th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4. using namespace std;
  5.  
  6.  
  7.  
  8.  
  9. template <typename T>
  10. class BTNode
  11. {
  12.     T data;
  13.     BTNode*pLeft;
  14.     BTNode*pRigt;
  15.  
  16. public:
  17.     BTNode(const T& _data = T(), BTNode *_pLeft = nullptr, BTNode *_pRigt = nullptr) : data(_data), pLeft(_pLeft), pRigt(_pRigt) {}
  18.  
  19.  
  20.     void setData(const T&_data)
  21.     {
  22.         data = _data;
  23.     }
  24.  
  25.  
  26.     T getData()
  27.     {
  28.         return data;
  29.     }
  30.  
  31.     void setpRight(BTNode*_pRigt)
  32.     {
  33.         pRigt = _pRigt;
  34.     }
  35.     void setpLeft(BTNode*_pLeft)
  36.     {
  37.         pLeft = _pLeft;
  38.     }
  39.  
  40.     const BTNode *getpRight()const
  41.     {
  42.         return pRigt;
  43.     }
  44.  
  45.     BTNode *getpRight()
  46.     {
  47.         return pRigt;
  48.     }
  49.  
  50.  
  51.     const BTNode *getpLeft()const
  52.     {
  53.         return pLeft;
  54.     }
  55.     BTNode *getpLeft()
  56.     {
  57.         return pLeft;
  58.     }
  59.  
  60.     bool isLeaf()const
  61.     {
  62.         if (pLeft == nullptr&&pRigt == nullptr)
  63.         {
  64.             return true;
  65.         }
  66.         else
  67.         {
  68.             return false;
  69.         }
  70.     }
  71. };
  72.  
  73. void fillTree(BTNode<string>*&root)
  74. {
  75.     root = new BTNode<string>("Оно Живет в воде?");
  76.     BTNode<string>*temp = new BTNode <string>("У него есть жабры?");
  77.     temp->setpLeft(new BTNode<string>("Щука"));
  78.     temp->setpRight(new BTNode<string>("Лягушка"));
  79.     root->setpLeft(temp);
  80.     temp = new BTNode <string>("у него 4 лапы?");
  81.     temp->setpLeft(new BTNode<string>("Кот"));
  82.     temp->setpRight(new BTNode<string>("Змея"));
  83.     root->setpRight(temp);
  84. }
  85. template <typename T>
  86. void printTree(BTNode <T>*root)
  87. {
  88.     if (root != nullptr)
  89.     {
  90.         printTree(root->getpLeft());
  91.         //cout << root->getData() << endl;
  92.         printTree(root->getpRight());
  93.  
  94.         cout << root->getData() << endl;
  95.         //cout << root->getData() << endl;
  96.     }
  97. }
  98.  
  99. void learn(BTNode<string>*pCurrent)
  100. {
  101.     string correctAnswer, wrongAnswer, question;
  102.  
  103.     cout << "Я сдаюсь! что это за животное?\n";
  104.     getline(cin, correctAnswer);
  105.     wrongAnswer = pCurrent->getData();
  106.     cout << "Какой вопрос может помочь их различить?\n ";
  107.     getline(cin, question);
  108.     pCurrent->setData(question);
  109.     cout << "Какой ответ для " << correctAnswer << '?';
  110.     string answer;
  111.     getline(cin, answer);
  112.    
  113.     if (answer == "yes" || answer == "y")
  114.     {
  115.         pCurrent->setpLeft(new BTNode<string>(correctAnswer));
  116.         pCurrent->setpRight(new BTNode<string>(wrongAnswer));
  117.  
  118.     }
  119.     else if (answer == "no" || answer == "n")
  120.     {
  121.         pCurrent->setpLeft(new BTNode<string>(wrongAnswer));
  122.         pCurrent->setpRight(new BTNode<string>(correctAnswer));
  123.     }
  124.  
  125. }
  126. bool play(BTNode<string>*root)
  127. {
  128.     BTNode<string>*pCurrent = root;
  129.     string answer;
  130.     while (pCurrent->isLeaf()==false)
  131.     {
  132.         cout << pCurrent->getData() << endl;
  133.         getline(cin,answer);
  134.         //cin.ignore(1, '\n');
  135.         if (answer=="yes"|| answer == "y")
  136.         {
  137.             pCurrent = pCurrent->getpLeft();
  138.         }
  139.         else if (answer == "no" || answer == "n")
  140.         {
  141.             pCurrent = pCurrent->getpRight();
  142.         }
  143.    
  144.     }
  145.     cout << "Я думаю это  " << pCurrent->getData();
  146.     cout << "  Это верно?\n";
  147.     getline(cin, answer);
  148.     //cin.ignore(1, '\n');
  149.     if (answer == "yes")
  150.     {
  151.         cout << "Урра!\n";
  152.     }
  153.     else
  154.     {
  155.         learn(pCurrent);
  156.         cout << "Продолжим игру?\n";
  157.         getline(cin, answer);
  158.         //cin.ignore(1, '\n');
  159.         if (answer == "yes" || answer == "y")
  160.         {
  161.             return true;
  162.         }
  163.         else
  164.         {
  165.             return false;
  166.         }
  167.     }
  168.  
  169.  
  170. }
  171.  
  172.  
  173. void main()
  174. {
  175.     SetConsoleCP(1251);
  176.     SetConsoleOutputCP(1251);
  177.  
  178.     //setlocale(LC_ALL, "Russian");
  179.     BTNode <string>*root = nullptr;
  180.     fillTree(root);
  181.     //printTree(root);
  182.     while (play (root)==true)
  183.     {
  184.         play(root);
  185.     }
  186.     //play(root);
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement