Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <array>
  4. #include <random>
  5. #include <math.h>
  6.  
  7. int minimum = 0;
  8. int maximum = 0;
  9. bool doesUserWantsToPlay()
  10. {
  11.     bool rResult = false;
  12.  
  13.     std::cout << "Guess the number game" << std::endl;
  14.  
  15.     while (true)
  16.     {
  17.         std::cout << "Do you want to play? (1 - yes, 0 - no):";
  18.  
  19.         std::string answer;
  20.         std::cin >> answer;
  21.  
  22.         if ((answer == "1") || (answer == "0"))
  23.         {
  24.             rResult = (answer == "1");
  25.             break;
  26.         }
  27.  
  28.         std::cout << "Sorry, I did not understand." << std::endl;
  29.     }
  30.  
  31.     return rResult;
  32. }
  33.  
  34. void getInterval() {
  35.     std::cout << "enter the interval " << std::endl;
  36.     std::cout << " enter the start point" << std::endl;
  37.     std::cin >> minimum;
  38.     std::cout << " enter the end point " << std::endl;
  39.     std::cin >> maximum;
  40.     if (maximum < minimum)
  41.     {
  42.         int temp;
  43.         temp = maximum;
  44.         maximum = minimum;
  45.         minimum = temp;
  46.     }
  47.     else if(minimum == maximum)
  48.     {
  49.         std::cout<<"INPUT CORRECT INTERVAL"<<std::endl;
  50.         getInterval();
  51.     }
  52. }
  53.  
  54. int getRandomNumber() {
  55. static  std::random_device rd;   // non-deterministic generator
  56. static  std::mt19937 gen(rd());  // to seed mersenne twister.
  57. static  std::uniform_int_distribution<> dist(minimum, maximum); // distribute results between 1 and 6 inclusive.
  58. return (int)dist(gen);
  59. }
  60.  
  61. int ChooseCooplexity(){
  62.     int temp;
  63.     while (true)
  64.     {
  65.  
  66.         std::cout << " Choose the difficult ( 3 = easy , 2 = medium , 1 = hard ) " << std::endl;
  67.     std::cin >> temp;
  68.         if ((temp == 1) || (temp == 2) || (temp == 3))
  69.         {
  70.             break;
  71.         }
  72.  
  73.         std::cout << "Sorry, I did not understand." << std::endl;
  74.     }
  75.  
  76.  
  77.     return temp;
  78. }
  79.  
  80. int getNumberOfIterations(int r)
  81. {
  82.       int NumberOfIteration = 0;
  83.     int t;
  84.  
  85.     do
  86.     {
  87.         NumberOfIteration++;
  88.         int f1 = minimum;
  89.          t = (minimum + maximum) / 2.0;
  90.         int f2 = t;
  91.         if (f2< r) minimum = t;
  92.         else maximum = t;
  93.     } while (t!=r);
  94.     return NumberOfIteration;
  95. }
  96.  
  97. int  getComplexity(int r)
  98. {
  99.     int temp = ChooseCooplexity();
  100.     int n = getNumberOfIterations(r);
  101.  
  102.     return n + 2 *  temp;
  103.  
  104. }
  105. void PlayGame()
  106. {
  107.     std::cout << "Lets Start ! " << std::endl;
  108.  
  109.     getInterval();
  110.      int rand = getRandomNumber();
  111.     int value;
  112.     int difficult = getComplexity(rand);
  113.     int i;
  114.     for ( i = 0; i <= difficult ; i++)
  115.     {
  116.         std::cout << "you have "<< difficult - i << " attempts" << std::endl;
  117.         std::cout << " please, enter the number"<<std::endl;
  118.         std::cin >> value;
  119.         if (value > rand)
  120.         {
  121.             std::cout << " less"<< std::endl;
  122.         }
  123.         else if (value < rand)
  124.         {
  125.             std::cout << " more" << std::endl;
  126.         }
  127.         else {
  128.             std::cout << "you find the number" << std::endl;
  129.             break;
  130.         }
  131.         if (i == difficult)
  132.             std::cout << "you lose" << std::endl;
  133.     }
  134.  
  135.  
  136. }
  137.  
  138. int main()
  139. {
  140.     while (doesUserWantsToPlay()) {
  141.         PlayGame();
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement