Advertisement
Guest User

Untitled

a guest
May 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.30 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <fstream>
  10. #include <cstdlib> // для exit()
  11.  
  12. using namespace std;
  13.  
  14. const bool DEBUG = false;
  15. const int COLORS_NUMBER = 8;
  16. const string COLORS[COLORS_NUMBER] = {
  17.     "red", "green", "blue", "white",
  18.     "black", "yellow", "pink", "orange" };
  19. const int NEED_TO_GUESS = 4;
  20. const int LIVES = 10;
  21. int RECORDS[10];
  22. int RECORDS_NUMBER = 0;
  23. int SCORE = 1000;
  24.  
  25. int prompt_menu_item();
  26. void menu_main();
  27. void menu_rules();
  28. void menu_difficulty_level();
  29. void menu_records();
  30. void game(bool difficulty);
  31. string* init_guess_array(bool withRepetitions);
  32. bool include(int arr[NEED_TO_GUESS], int value);
  33. void print(string* arr, int length);
  34. bool is_colors_correct(string guess[]);
  35. void read_records();
  36. void write_records();
  37.  
  38. int main() {
  39.     read_records();
  40.     menu_main();
  41. }
  42.  
  43. // Вывести меню пользователю и вернуть номер варианта
  44. int prompt_menu_item()
  45. {
  46.     setlocale(LC_ALL, "russian");
  47.     // Выбранный вариант меню
  48.     int variant;
  49.     cout << "ЛОГИКА ЦВЕТА" << endl << endl;
  50.     cout << "1. Играть\n"
  51.         << "2. Правила игры\n"
  52.         << "3. Таблица рекордов\n"
  53.         << "4. Выход\n";
  54.     cout << ">>> ";
  55.     cin >> variant;
  56.     return variant;
  57. }
  58.  
  59. void menu_main()
  60. {
  61.     int variant = prompt_menu_item();
  62.     switch (variant)
  63.     {
  64.     case 1:
  65.     {
  66.         menu_difficulty_level();
  67.         break;
  68.     }
  69.  
  70.     case 2:
  71.     {
  72.         menu_rules();
  73.         break;
  74.     }
  75.     case 3:
  76.     {
  77.         menu_records();
  78.         break;
  79.     }
  80.     case 4:
  81.     {
  82.         cout << "Выход из программы..." << endl;
  83.         exit(EXIT_SUCCESS);
  84.         break;
  85.     }
  86.  
  87.     default:
  88.     {
  89.         cerr << "Вы выбрали неверный вариант" << endl;
  90.         return menu_main();
  91.     }
  92.     }
  93.  
  94.     return;
  95. }
  96.  
  97. void menu_rules()
  98. {
  99.     int n;
  100.     cout << endl << "1. УСЛОВИЕ";
  101.     cout << endl << "2. УРОВНИ СЛОЖНОСТИ";
  102.     cout << endl << "3. НАЗАД" << endl;
  103.     cout << ">>> ";
  104.     cin >> n;
  105.     switch (n)
  106.     {
  107.     case 1:
  108.     {
  109.         cout << endl << "Компьютер генерирует четыре разных цвета, размещенных в четырех последовательных позициях.Игрок предлагает свою последовательность, после чего компьютер формирует сигнал одного типа, если в предлагаемой последовательности совпадает цвет, и сигнал другого типа, если дополнительно совпадает позиция. Необходимо за заданное число ходов отгадать скрытую комбинацию цветов." << endl;
  110.         return menu_rules();
  111.     }
  112.     case 2:
  113.     {
  114.         cout << "Легкий\n" << "Игрок знает цвета. Цвета не повторяются.\n" << endl
  115.             << "Сложный\n" << "Цвета могут повторяться." << endl;
  116.         return menu_rules();
  117.     }
  118.     case 3:
  119.     {
  120.         return menu_main();
  121.     }
  122.  
  123.     default:
  124.     {
  125.         cerr << "Вы выбрали неверный вариант" << endl;
  126.         return menu_rules();
  127.     }
  128.     }
  129.  
  130.     return;
  131. }
  132.  
  133. void menu_difficulty_level()
  134. {
  135.     int m;
  136.     cout << endl << "ВЫБЕРИТЕ УРОВЕНЬ СЛОЖНОСТИ" << endl << endl;
  137.     cout << "1. Легкий" << endl;
  138.     cout << "2. Сложный" << endl;
  139.     cout << "3. Назад" << endl;
  140.     cout << ">>> ";
  141.     cin >> m;
  142.  
  143.     switch (m)
  144.     {
  145.     case 1:
  146.     {
  147.         game(false);
  148.         break;
  149.     }
  150.     case 2:
  151.     {
  152.         SCORE += 500;
  153.         game(true);
  154.         break;
  155.     }
  156.     case 3:
  157.     {
  158.         return menu_main();
  159.     }
  160.     default:
  161.     {
  162.         cerr << "Вы выбрали неверный вариант" << endl;
  163.         return menu_difficulty_level();
  164.     }
  165.     }
  166. }
  167.  
  168. void menu_records()
  169. {
  170.     int m;
  171.    
  172.     cout << "Таблица рекордов: " << endl;
  173.     for (int i = 0; i < RECORDS_NUMBER; i++)
  174.     {
  175.         cout << i + 1 << ". " << RECORDS[i] << endl;
  176.     }
  177.     cout << endl;
  178.  
  179.     cout << "1. Назад" << endl;
  180.     cout << ">>> ";
  181.     cin >> m;
  182.  
  183.     switch (m)
  184.     {
  185.     case 1:
  186.     {
  187.         return menu_main();
  188.     }
  189.     default:
  190.     {
  191.         cerr << "Вы выбрали неверный вариант" << endl;
  192.         return menu_records();
  193.     }
  194.     }
  195. }
  196.  
  197. bool include(int arr[NEED_TO_GUESS], int value)
  198. {
  199.     bool res = false;
  200.  
  201.     for (int i = 0; i < NEED_TO_GUESS; i++)
  202.     {
  203.         if (arr[i] == value)
  204.         {
  205.             res = true;
  206.             break;
  207.         }
  208.     }
  209.  
  210.     return res;
  211. }
  212.  
  213. void print(string* arr, int length)
  214. {
  215.     for (int i = 0; i < length; i++)
  216.     {
  217.         cout << arr[i] << " ";
  218.     }
  219.     cout << endl;
  220. }
  221.  
  222. string* init_guess_array(bool withRepetitions)
  223. {
  224.     static string colors_to_guess[NEED_TO_GUESS];
  225.     srand(time(NULL));
  226.     int usedIndex[NEED_TO_GUESS];
  227.  
  228.     for (int i = 0; i < NEED_TO_GUESS; i++)
  229.     {
  230.         int randIndex = rand() % NEED_TO_GUESS + 1;
  231.  
  232.         if (withRepetitions || !include(usedIndex, randIndex))
  233.         {
  234.             usedIndex[i] = randIndex;
  235.             colors_to_guess[i] = COLORS[randIndex];
  236.         }
  237.         else
  238.         {
  239.             i--;
  240.             continue;
  241.         }
  242.     }
  243.  
  244.     return colors_to_guess;
  245. }
  246.  
  247. void game(bool difficulty)
  248. {
  249.     // clean input buffer
  250.     string temp;
  251.     getline(cin, temp);
  252.  
  253.     string* colors_to_guess = init_guess_array(difficulty);
  254.     int lives = LIVES;
  255.  
  256.     // Show randomized colors
  257.     if (DEBUG)
  258.     {
  259.         cout << "!DEBUG! GENERATED COLORS: " << endl;
  260.         print(colors_to_guess, NEED_TO_GUESS);
  261.     }
  262.  
  263.     bool correct_input = false;
  264.  
  265.     do {
  266.         string guess;
  267.  
  268.         // Display task description
  269.         cout << "You have " << lives << " lives." << endl;
  270.         cout << "Guess 4 colors from those: " << endl;
  271.         for (int i = 0; i < COLORS_NUMBER; i++)
  272.         {
  273.             if (i != COLORS_NUMBER - 1)
  274.             {
  275.                 cout << COLORS[i] << ", ";
  276.             }
  277.             else
  278.             {
  279.                 cout << COLORS[i] << endl;
  280.             }
  281.         }
  282.  
  283.         // Input guess
  284.         getline(cin, guess);
  285.  
  286.         // Split input to string array
  287.         string splited_guess[4];
  288.         string temp_string = "";
  289.         int inputed_count = 0;
  290.         for (int i = 0; i < guess.size(); i++)
  291.         {
  292.             if (guess[i] == ' ')
  293.             {
  294.                 if (inputed_count == 3)
  295.                 {
  296.                     cout << "Not correct number of colors inputed." << endl;
  297.                     break;
  298.                 }
  299.                 splited_guess[inputed_count] = temp_string;
  300.                 temp_string = "";
  301.                 inputed_count++;
  302.             }
  303.             else
  304.             {
  305.                 temp_string += guess[i];
  306.             }
  307.         }
  308.         splited_guess[inputed_count] = temp_string;
  309.  
  310.         // Check if inputed count of colors is correct
  311.         if (inputed_count != 3)
  312.         {
  313.             cout << "Not correct number of colors inputed." << endl;
  314.             continue;
  315.         }
  316.  
  317.         // Rank the guess
  318.         int guess_rang[4];
  319.         int guess_total = 0;
  320.         for (int i = 0; i < NEED_TO_GUESS; i++)
  321.         {
  322.             int is_color_exist = 0;
  323.             int is_index_right = 0;
  324.             string color = splited_guess[i];
  325.  
  326.             for (int j = 0; j < NEED_TO_GUESS; j++)
  327.             {
  328.                 if (color == colors_to_guess[j])
  329.                 {
  330.                     is_color_exist = 1;
  331.                     if (i == j) {
  332.                         is_index_right = 1;
  333.                     }
  334.                 }
  335.             }
  336.  
  337.             guess_rang[i] = is_color_exist + is_index_right;
  338.             guess_total += guess_rang[i];
  339.         }
  340.  
  341.         // Display answer
  342.         if (guess_total == NEED_TO_GUESS * 2) {
  343.             cout << "You Win!" << endl;
  344.             write_records();
  345.             correct_input = true;
  346.         }
  347.         else if (lives - 1 == 0)
  348.         {
  349.             cout << "You Loose." << endl;
  350.             break;
  351.         }
  352.         else {
  353.             lives--;
  354.             SCORE -= difficulty ? 150 : 100;
  355.             cout << "1 - right color. 2 - also on right position." << endl;
  356.             for (int i = 0; i < NEED_TO_GUESS; i++)
  357.             {
  358.                 if (i + 1 < NEED_TO_GUESS)
  359.                     cout << guess_rang[i] << ", ";
  360.                 else
  361.                     cout << guess_rang[i] << endl;
  362.             }
  363.         }
  364.  
  365.     } while (!correct_input);
  366.  
  367.     return menu_main();
  368. }
  369.  
  370.  
  371. void read_records()
  372. {
  373.     string str;
  374.     ifstream fin("records.txt");
  375.     RECORDS_NUMBER = 0;
  376.  
  377.     for (int i = 0; i < 10 && fin >> str; i++)
  378.     {
  379.         // Parse record from file to Integer
  380.         RECORDS[i] = stoi(str);
  381.         RECORDS_NUMBER++;
  382.     }
  383.  
  384.     fin.close();
  385. }
  386.  
  387. void write_records()
  388. {
  389.     int new_records[10];
  390.     bool score_inserted = false;
  391.  
  392.     ofstream fout;
  393.     fout.open("records.txt");
  394.  
  395.     for (int i = 0; i < RECORDS_NUMBER + 1 && i < 10; i++)
  396.     {
  397.         if (score_inserted)
  398.         {
  399.             new_records[i] = RECORDS[i - 1];
  400.         }
  401.         else if (SCORE > RECORDS[i])
  402.         {
  403.             new_records[i] = SCORE;
  404.             score_inserted = true;
  405.         }
  406.         else
  407.         {
  408.             new_records[i] = RECORDS[i];
  409.         }
  410.     }
  411.  
  412.     for (int i = 0; i < RECORDS_NUMBER + 1 && i < 10; i++)
  413.     {
  414.         fout << new_records[i] << "\n";
  415.     }
  416.  
  417.     fout.close();
  418.     read_records();
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement