Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <time.h>
- #include <fstream>
- #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 RECORDS[10];
- int RECORDS_NUMBER = 0;
- int SCORE = 1000;
- int prompt_menu_item();
- void menu_main();
- void menu_rules();
- void menu_difficulty_level();
- void menu_records();
- 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[]);
- void read_records();
- void write_records();
- int main() {
- read_records();
- menu_main();
- }
- // Вывести меню пользователю и вернуть номер варианта
- int prompt_menu_item()
- {
- setlocale(LC_ALL, "russian");
- // Выбранный вариант меню
- int variant;
- cout << "ЛОГИКА ЦВЕТА" << endl << endl;
- cout << "1. Играть\n"
- << "2. Правила игры\n"
- << "3. Таблица рекордов\n"
- << "4. Выход\n";
- 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:
- {
- menu_records();
- break;
- }
- case 4:
- {
- 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 << "Компьютер генерирует четыре разных цвета, размещенных в четырех последовательных позициях.Игрок предлагает свою последовательность, после чего компьютер формирует сигнал одного типа, если в предлагаемой последовательности совпадает цвет, и сигнал другого типа, если дополнительно совпадает позиция. Необходимо за заданное число ходов отгадать скрытую комбинацию цветов." << 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:
- {
- SCORE += 500;
- game(true);
- break;
- }
- case 3:
- {
- return menu_main();
- }
- default:
- {
- cerr << "Вы выбрали неверный вариант" << endl;
- return menu_difficulty_level();
- }
- }
- }
- void menu_records()
- {
- int m;
- cout << "Таблица рекордов: " << endl;
- for (int i = 0; i < RECORDS_NUMBER; i++)
- {
- cout << i + 1 << ". " << RECORDS[i] << endl;
- }
- cout << endl;
- cout << "1. Назад" << endl;
- cout << ">>> ";
- cin >> m;
- switch (m)
- {
- case 1:
- {
- return menu_main();
- }
- default:
- {
- cerr << "Вы выбрали неверный вариант" << endl;
- return menu_records();
- }
- }
- }
- 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)
- {
- // clean input buffer
- string temp;
- getline(cin, temp);
- 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 << " 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;
- write_records();
- correct_input = true;
- }
- else if (lives - 1 == 0)
- {
- cout << "You Loose." << endl;
- break;
- }
- else {
- lives--;
- SCORE -= difficulty ? 150 : 100;
- 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();
- }
- void read_records()
- {
- string str;
- ifstream fin("records.txt");
- RECORDS_NUMBER = 0;
- for (int i = 0; i < 10 && fin >> str; i++)
- {
- // Parse record from file to Integer
- RECORDS[i] = stoi(str);
- RECORDS_NUMBER++;
- }
- fin.close();
- }
- void write_records()
- {
- int new_records[10];
- bool score_inserted = false;
- ofstream fout;
- fout.open("records.txt");
- for (int i = 0; i < RECORDS_NUMBER + 1 && i < 10; i++)
- {
- if (score_inserted)
- {
- new_records[i] = RECORDS[i - 1];
- }
- else if (SCORE > RECORDS[i])
- {
- new_records[i] = SCORE;
- score_inserted = true;
- }
- else
- {
- new_records[i] = RECORDS[i];
- }
- }
- for (int i = 0; i < RECORDS_NUMBER + 1 && i < 10; i++)
- {
- fout << new_records[i] << "\n";
- }
- fout.close();
- read_records();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement