Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <time.h>
- #include <cstdlib> // для exit()
- using namespace std;
- const bool DEBUG = false;
- const int COLORS_NUMBER = 8;
- const string COLORS[COLORS_NUMBER] = {
- "red", "green", "blue", "white",
- "black", "yellow", "pink", "orange" };
- const int NEED_TO_GUESS = 4;
- const int LIVES = 10;
- int prompt_menu_item();
- void menu_main();
- void menu_rules();
- void menu_difficulty_level();
- void game(bool difficulty);
- string* init_guess_array(bool withRepetitions);
- bool include(int arr[NEED_TO_GUESS], int value);
- void print(string* arr, int length);
- bool is_colors_correct(string guess[]);
- int main() {
- menu_main();
- }
- // Вывести меню пользователю и вернуть номер варианта
- int prompt_menu_item()
- {
- setlocale(LC_ALL, "russian");
- // Выбранный вариант меню
- int variant;
- cout << "ЛОГИКА ЦВЕТА" << endl<< endl;
- cout << "1. Играть\n"
- << "2. Правила игры\n"
- << "3. Выход\n" << endl;
- cout << ">>> ";
- cin >> variant;
- return variant;
- }
- void menu_main()
- {
- int variant = prompt_menu_item();
- switch (variant)
- {
- case 1:
- {
- menu_difficulty_level();
- break;
- }
- case 2:
- {
- menu_rules();
- break;
- }
- case 3:
- {
- cout << "Выход из программы..." << endl;
- exit(EXIT_SUCCESS);
- break;
- }
- default:
- {
- cerr << "Вы выбрали неверный вариант" << endl;
- return menu_main();
- }
- }
- return;
- }
- void menu_rules()
- {
- int n;
- cout << endl << "1. УСЛОВИЕ";
- cout << endl << "2. УРОВНИ СЛОЖНОСТИ";
- cout << endl << "3. НАЗАД" << endl;
- cout << ">>> ";
- cin >> n;
- switch (n)
- {
- case 1:
- {
- cout << "Компьютер генерирует четыре разных цвета, размещенных в четырех последовательных позициях.Игрок предлагает свою последовательность, после чего компьютер формирует сигнал одного типа, если в предлагаемой последовательности совпадает цвет, и сигнал другого типа, если дополнительно совпадает позиция. Необходимо за заданное число ходов отгадать скрытую комбинацию цветов." << endl;
- return menu_rules();
- }
- case 2:
- {
- cout << "Легкий\n" << "Игрок знает цвета. Цвета не повторяются.\n" << endl
- << "Сложный\n" << "Цвета могут повторяться." << endl;
- return menu_rules();
- }
- case 3:
- {
- return menu_main();
- }
- default:
- {
- cerr << "Вы выбрали неверный вариант" << endl;
- return menu_rules();
- }
- }
- return;
- }
- void menu_difficulty_level()
- {
- int m;
- cout << endl << "ВЫБЕРИТЕ УРОВЕНЬ СЛОЖНОСТИ" << endl << endl;
- cout << "1. Легкий" << endl;
- cout << "2. Сложный" << endl;
- cout << "3. Назад" << endl;
- cout << ">>> ";
- cin >> m;
- switch (m)
- {
- case 1:
- {
- game(false);
- break;
- }
- case 2:
- {
- game(true);
- break;
- }
- case 3:
- {
- return menu_main();
- }
- default:
- {
- cerr << "Вы выбрали неверный вариант" << endl;
- return menu_difficulty_level();
- }
- }
- }
- bool include(int arr[NEED_TO_GUESS], int value)
- {
- bool res = false;
- for (int i = 0; i < NEED_TO_GUESS; i++)
- {
- if (arr[i] == value)
- {
- res = true;
- break;
- }
- }
- return res;
- }
- void print(string* arr, int length)
- {
- for (int i = 0; i < length; i++)
- {
- cout << arr[i] << " ";
- }
- cout << endl;
- }
- string* init_guess_array(bool withRepetitions)
- {
- static string colors_to_guess[NEED_TO_GUESS];
- srand(time(NULL));
- int usedIndex[NEED_TO_GUESS];
- for (int i = 0; i < NEED_TO_GUESS; i++)
- {
- int randIndex = rand() % NEED_TO_GUESS + 1;
- if (withRepetitions || !include(usedIndex, randIndex))
- {
- usedIndex[i] = randIndex;
- colors_to_guess[i] = COLORS[randIndex];
- }
- else
- {
- i--;
- continue;
- }
- }
- return colors_to_guess;
- }
- void game(bool difficulty)
- {
- string* colors_to_guess = init_guess_array(difficulty);
- int lives = LIVES;
- // Show randomized colors
- if (DEBUG)
- {
- cout << "!DEBUG! GENERATED COLORS: " << endl;
- print(colors_to_guess, NEED_TO_GUESS);
- }
- bool correct_input = false;
- do {
- string guess;
- // Display task description
- cout << "You have " << lives << "." << endl;
- cout << "Guess 4 colors from those: " << endl;
- for (int i = 0; i < COLORS_NUMBER; i++)
- {
- if (i != COLORS_NUMBER - 1)
- {
- cout << COLORS[i] << ", ";
- }
- else
- {
- cout << COLORS[i] << endl;
- }
- }
- // Input guess
- getline(cin, guess);
- // Split input to string array
- string splited_guess[4];
- string temp_string = "";
- int inputed_count = 0;
- for (int i = 0; i < guess.size(); i++)
- {
- if (guess[i] == ' ')
- {
- if (inputed_count == 3)
- {
- cout << "Not correct number of colors inputed." << endl;
- break;
- }
- splited_guess[inputed_count] = temp_string;
- temp_string = "";
- inputed_count++;
- }
- else
- {
- temp_string += guess[i];
- }
- }
- splited_guess[inputed_count] = temp_string;
- // Check if inputed count of colors is correct
- if (inputed_count != 3)
- {
- cout << "Not correct number of colors inputed." << endl;
- continue;
- }
- // Rank the guess
- int guess_rang[4];
- int guess_total = 0;
- for (int i = 0; i < NEED_TO_GUESS; i++)
- {
- int is_color_exist = 0;
- int is_index_right = 0;
- string color = splited_guess[i];
- for (int j = 0; j < NEED_TO_GUESS; j++)
- {
- if (color == colors_to_guess[j])
- {
- is_color_exist = 1;
- if (i == j) {
- is_index_right = 1;
- }
- }
- }
- guess_rang[i] = is_color_exist + is_index_right;
- guess_total += guess_rang[i];
- }
- // Display answer
- if (guess_total == NEED_TO_GUESS * 2) {
- cout << "You Win!" << endl;
- correct_input = true;
- }
- else if (lives - 1 == 0)
- {
- cout << "You Loose." << endl;
- break;
- }
- else {
- lives--;
- cout << "1 - right color. 2 - also on right position." << endl;
- for (int i = 0; i < NEED_TO_GUESS; i++)
- {
- if (i + 1 < NEED_TO_GUESS)
- cout << guess_rang[i] << ", ";
- else
- cout << guess_rang[i] << endl;
- }
- }
- } while (!correct_input);
- return menu_main();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement