daniv1

MyFirstGame

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