Словомеска

Ответить
ya
^-^
Сообщения: 2336
Зарегистрирован: 16 дек 2021, 19:56

Словомеска

Сообщение ya »

Код: Выделить всё

// Игра Word JumЫe
// Классическая игра-головоломка. в которой пользователь разгадывает слова. с подсказками или без них.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
// Знакомство с игрой «СЛовомеска»
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
	{"wall", "Do you feel you're banging your head against something?"},
	{"glasses", "These might help you see the answer. "},
	{"labored", "Going slowly. is it?"},
	{"persistent", "Кеер at it."},
	{"jumЫe", "It's what the game is all about."}
};

srand(static_cast<unsigned int>(time(0)));
int choice = (rand() % NUM_WORDS); //Я генерирую случайный индекс, исходя из количества сло
string theWord = WORDS[choice][WORD]; //слово. которое нужно угадать
string theHint = WORDS[choice][HINT]; //подсказка для слова

//Перемешивание слова
//Теперь, когда я выбрал слово, которое загадаю пользователю, мне нужно переставить в нем буквы:
string jumble = theWord; // перемешанный вариант слова
int length = jumble.size();
for (int i = 0; i < length; ++i)
{
	int index1 = (rand() % length);
	int index2 = (rand() % length);
	char temp = jumble[index1];
	jumble[index1] = jumble[index2];
	jumble[index2] = temp;
}

// Приглашение игрока
// Далее нужно пригласить пользователя поиграть, что я и делаю в следующем коде:
cout << "\t\t\tWelcome to Word JumЬle!\n\n";
cout << "UnscramЫ е the l etters to make а word. \n";
cout << "Enter 'hint' for а hint. \n";
cout << "Enter 'quit' to quit the game. \n\n";
cout << "The jumble is: " << jumble;
string guess;
cout << "\n\nYour guess: ";
cin >> guess;

// Начало игрового цикла
// Далее начинается игровой цикл:
while ((guess != theWord) && (guess != "quit"))
{
	if (guess == "hint")
	{
		cout << theHint;
	}
	else
	{
		cout << "Sorry. that's not it. ";
	}
	cout << "\n\nYour guess: ";
	cin >> guess;
}

//Когда цикл завершится (пользователь отгадает слово либо решит выйти из игры), с игроком нужно попрощаться:
if (guess == theWord)
{
	cout << "\nThat's it! You guessed it!\n";
}
cout << "\nThanks for playing.\n";
return 0;
}
Ответить