-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangmanimprove.cpp
More file actions
120 lines (102 loc) · 3.08 KB
/
Hangmanimprove.cpp
File metadata and controls
120 lines (102 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <cctype>
using namespace std;
int random(int a, int b)
{
thread_local mt19937 eng{random_device{}()};
uniform_int_distribution<int> dist(a, b);
return dist(eng);
}
string randomWord()
{
string words[] =
{
"Nepal", "Pakistan", "India", "Malaysia", "Philippines",
"Australia", "Iran", "Ethiopia", "Oman", "Indonesia"
};
int index = random(0, 9);
return words[index];
}
int letterFill(char guess, string secretword, string &guessword)
{
int matches = 0;
int len = secretword.length();
for (int i = 0; i < len; i++)
{
if (tolower(guess) == tolower(guessword[i])) return 0;
if (tolower(guess) == tolower(secretword[i]))
{
guessword[i] = secretword[i];
matches++;
}
}
return matches;
}
void displayCurrentState(const string &unknown, int tries, const vector<char>& guessedLetters)
{
cout << "Current word: " << unknown << endl;
cout << "You have " << tries << " tries left." << endl;
cout << "Previous guesses: ";
for (char g : guessedLetters) cout << g << " ";
cout << endl;
}
int main()
{
while (true)
{
int tries = 6;
string word = randomWord();
string unknown(word.length(), '_');
vector<char> guessedLetters;
cout << "Welcome to Hangman!" << endl;
cout << "A random word has been generated! It has " << word.length() << " letters!" << endl;
while (tries > 0)
{
displayCurrentState(unknown, tries, guessedLetters);
string input;
cout << "Guess a letter: ";
cin >> input;
if (input.empty() || input.length() > 1 || !isalpha(input[0]))
{
cout << "Invalid input! Please enter a single letter." << endl;
continue;
}
char letter = tolower(input[0]);
if (find(guessedLetters.begin(), guessedLetters.end(), letter) != guessedLetters.end())
{
cout << "You've already guessed that letter!" << endl;
continue;
}
guessedLetters.push_back(letter);
if (letterFill(letter, word, unknown) == 0)
{
cout << "Sorry, no '" << letter << "' in the word." << endl;
tries--;
}
else
{
cout << "Nice! '" << letter << "' is in the word!" << endl;
}
if (word == unknown)
{
cout << "Congratulations! You've guessed the word: " << word << endl;
break;
}
}
if (word != unknown)
{
cout << "Sorry, you've run out of tries! The word was: " << word << endl;
}
char playAgain;
cout << "Do you want to play again? (y/n): ";
cin >> playAgain;
if (tolower(playAgain) != 'y') {
cout << "Thanks for playing! Goodbye." << endl;
break;
}
}
return 0;
}