Advertisement
Guest User

Untitled

a guest
May 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <cstdlib> // для exit()
  6.  
  7. using namespace std;
  8.  
  9. const bool DEBUG = false;
  10. const int COLORS_NUMBER = 8;
  11. const string COLORS[COLORS_NUMBER] = {
  12.   "red", "green", "blue", "white",
  13.   "black", "yellow", "pink", "orange" };
  14. const int NEED_TO_GUESS = 4;
  15. const int LIVES = 10;
  16.  
  17. int prompt_menu_item();
  18. void menu_main();
  19. void menu_rules();
  20. void menu_difficulty_level();
  21. void game(bool difficulty);
  22. string* init_guess_array(bool withRepetitions);
  23. bool include(int arr[NEED_TO_GUESS], int value);
  24. void print(string* arr, int length);
  25. bool is_colors_correct(string guess[]);
  26.  
  27. int main() {
  28.   menu_main();
  29. }
  30.  
  31. // Вывести меню пользователю и вернуть номер варианта
  32. int prompt_menu_item()
  33. {
  34.     setlocale(LC_ALL, "russian");
  35.     // Выбранный вариант меню
  36.     int variant;
  37.     cout << "ЛОГИКА ЦВЕТА" << endl<< endl;
  38.     cout << "1. Играть\n"
  39.         << "2. Правила игры\n"
  40.         << "3. Выход\n" << endl;
  41.     cout << ">>> ";
  42.     cin >> variant;
  43.     return variant;
  44. }
  45.  
  46. void menu_main()
  47. {
  48.   int variant = prompt_menu_item();
  49.   switch (variant)
  50.   {
  51.     case 1:
  52.     {
  53.       menu_difficulty_level();
  54.       break;
  55.     }
  56.  
  57.     case 2:
  58.     {
  59.       menu_rules();
  60.       break;
  61.     }
  62.     case 3:
  63.     {
  64.       cout << "Выход из программы..." << endl;
  65.       exit(EXIT_SUCCESS);
  66.       break;
  67.     }
  68.  
  69.     default:
  70.     {
  71.       cerr << "Вы выбрали неверный вариант" << endl;
  72.       return menu_main();
  73.     }
  74.   }
  75.  
  76.   return;
  77. }
  78.  
  79. void menu_rules()
  80. {
  81.   int n;
  82.   cout << endl << "1. УСЛОВИЕ";
  83.   cout << endl << "2. УРОВНИ СЛОЖНОСТИ";
  84.   cout << endl << "3. НАЗАД" << endl;
  85.   cout << ">>> ";
  86.   cin >> n;
  87.   switch (n)
  88.   {
  89.     case 1:
  90.     {
  91.       cout << "Компьютер генерирует четыре разных цвета, размещенных в четырех последовательных позициях.Игрок предлагает свою последовательность, после чего компьютер формирует сигнал одного типа, если в предлагаемой последовательности совпадает цвет, и сигнал другого типа, если дополнительно совпадает позиция. Необходимо за заданное число ходов отгадать скрытую комбинацию цветов." << endl;
  92.       return menu_rules();
  93.     }
  94.     case 2:
  95.     {
  96.       cout << "Легкий\n" << "Игрок знает цвета. Цвета не повторяются.\n" << endl
  97.       << "Сложный\n" << "Цвета могут повторяться." << endl;
  98.       return menu_rules();
  99.     }
  100.     case 3:
  101.     {
  102.       return menu_main();
  103.     }
  104.  
  105.     default:
  106.     {
  107.       cerr << "Вы выбрали неверный вариант" << endl;
  108.       return menu_rules();
  109.     }
  110.   }
  111.  
  112.   return;
  113. }
  114.  
  115. void menu_difficulty_level()
  116. {
  117.   int m;
  118.   cout << endl << "ВЫБЕРИТЕ УРОВЕНЬ СЛОЖНОСТИ" << endl << endl;
  119.   cout << "1. Легкий" << endl;
  120.   cout << "2. Сложный" << endl;
  121.   cout << "3. Назад" << endl;
  122.   cout << ">>> ";
  123.   cin >> m;
  124.  
  125.   switch (m)
  126.   {
  127.     case 1:
  128.     {
  129.       game(false);
  130.       break;
  131.     }
  132.     case 2:
  133.     {
  134.       game(true);
  135.       break;
  136.     }
  137.     case 3:
  138.     {
  139.       return menu_main();
  140.     }
  141.     default:
  142.     {
  143.       cerr << "Вы выбрали неверный вариант" << endl;
  144.       return menu_difficulty_level();
  145.     }
  146.   }
  147. }
  148.  
  149. bool include(int arr[NEED_TO_GUESS], int value)
  150. {
  151.     bool res = false;
  152.  
  153.     for (int i = 0; i < NEED_TO_GUESS; i++)
  154.     {
  155.         if (arr[i] == value)
  156.         {
  157.             res = true;
  158.             break;
  159.         }
  160.     }
  161.  
  162.     return res;
  163. }
  164.  
  165. void print(string* arr, int length)
  166. {
  167.     for (int i = 0; i < length; i++)
  168.     {
  169.         cout << arr[i] << " ";
  170.     }
  171.     cout << endl;
  172. }
  173.  
  174. string* init_guess_array(bool withRepetitions)
  175. {
  176.     static string colors_to_guess[NEED_TO_GUESS];
  177.     srand(time(NULL));
  178.     int usedIndex[NEED_TO_GUESS];
  179.  
  180.     for (int i = 0; i < NEED_TO_GUESS; i++)
  181.     {
  182.         int randIndex = rand() % NEED_TO_GUESS + 1;
  183.  
  184.         if (withRepetitions || !include(usedIndex, randIndex))
  185.         {
  186.             usedIndex[i] = randIndex;
  187.             colors_to_guess[i] = COLORS[randIndex];
  188.         }
  189.         else
  190.         {
  191.             i--;
  192.             continue;
  193.         }
  194.     }
  195.  
  196.     return colors_to_guess;
  197. }
  198.  
  199. void game(bool difficulty)
  200. {
  201.   string* colors_to_guess = init_guess_array(difficulty);
  202.     int lives = LIVES;
  203.  
  204.     // Show randomized colors
  205.     if (DEBUG)
  206.     {
  207.         cout << "!DEBUG! GENERATED COLORS: " << endl;
  208.         print(colors_to_guess, NEED_TO_GUESS);
  209.     }
  210.  
  211.     bool correct_input = false;
  212.  
  213.     do {
  214.         string guess;
  215.  
  216.         // Display task description
  217.         cout << "You have " << lives << "." << endl;
  218.         cout << "Guess 4 colors from those: " << endl;
  219.         for (int i = 0; i < COLORS_NUMBER; i++)
  220.         {
  221.             if (i != COLORS_NUMBER - 1)
  222.             {
  223.                 cout << COLORS[i] << ", ";
  224.             }
  225.             else
  226.             {
  227.                 cout << COLORS[i] << endl;
  228.             }
  229.         }
  230.  
  231.         // Input guess
  232.         getline(cin, guess);
  233.  
  234.         // Split input to string array
  235.         string splited_guess[4];
  236.         string temp_string = "";
  237.         int inputed_count = 0;
  238.         for (int i = 0; i < guess.size(); i++)
  239.         {
  240.             if (guess[i] == ' ')
  241.             {
  242.                 if (inputed_count == 3)
  243.                 {
  244.                     cout << "Not correct number of colors inputed." << endl;
  245.                     break;
  246.                 }
  247.                 splited_guess[inputed_count] = temp_string;
  248.                 temp_string = "";
  249.                 inputed_count++;
  250.             }
  251.             else
  252.             {
  253.                 temp_string += guess[i];
  254.             }
  255.         }
  256.         splited_guess[inputed_count] = temp_string;
  257.  
  258.         // Check if inputed count of colors is correct
  259.         if (inputed_count != 3)
  260.         {
  261.             cout << "Not correct number of colors inputed." << endl;
  262.             continue;
  263.         }
  264.  
  265.         // Rank the guess
  266.         int guess_rang[4];
  267.         int guess_total = 0;
  268.         for (int i = 0; i < NEED_TO_GUESS; i++)
  269.         {
  270.             int is_color_exist = 0;
  271.             int is_index_right = 0;
  272.             string color = splited_guess[i];
  273.  
  274.             for (int j = 0; j < NEED_TO_GUESS; j++)
  275.             {
  276.                 if (color == colors_to_guess[j])
  277.                 {
  278.                     is_color_exist = 1;
  279.                     if (i == j) {
  280.                         is_index_right = 1;
  281.                     }
  282.                 }
  283.             }
  284.  
  285.             guess_rang[i] = is_color_exist + is_index_right;
  286.             guess_total += guess_rang[i];
  287.         }
  288.  
  289.         // Display answer
  290.         if (guess_total == NEED_TO_GUESS * 2) {
  291.             cout << "You Win!" << endl;
  292.             correct_input = true;
  293.         }
  294.         else if (lives - 1 == 0)
  295.         {
  296.             cout << "You Loose." << endl;
  297.             break;
  298.         }
  299.         else {
  300.             lives--;
  301.             cout << "1 - right color. 2 - also on right position." << endl;
  302.             for (int i = 0; i < NEED_TO_GUESS; i++)
  303.             {
  304.                 if (i + 1 < NEED_TO_GUESS)
  305.                     cout << guess_rang[i] << ", ";
  306.                 else
  307.                     cout << guess_rang[i] << endl;
  308.             }
  309.         }
  310.  
  311.     } while (!correct_input);
  312.  
  313.     return menu_main();
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement