-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj.cpp
125 lines (123 loc) · 3.3 KB
/
proj.cpp
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
121
122
123
124
125
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int word_check(string str,string randomWord)
{
if(randomWord==str)
{
return 1;
}
else
{
return 0;
}
}
string selectRandomWordFromFile(const string &filename)
{
ifstream file(filename);
vector<string> words;
string word;
if (file.is_open())
{
while (file >> word)
{
words.push_back(word);
}
file.close();
if (!words.empty())
{
srand(static_cast<unsigned int>(time(nullptr)));
int randomIndex = rand() % words.size();
return words[randomIndex];
}
else
{
cerr << "File is empty." << endl;
}
}
else
{
cerr << "Unable to open the file." << endl;
}
return "";
}
int main()
{
string randomWord = selectRandomWordFromFile("words.txt");
if (!randomWord.empty())
{
cout << "Random word selected: " << randomWord << endl;
}
cout << "WELCOME TO HANGMAN" << endl;
int tr = 7, option,res;
string word;
char letter;
string guessedWord(randomWord.length(), '_');
while (tr != 0)
{
cout << "you have " << tr << " tries" << endl;
cout << "Word: ";
for (char c : guessedWord)
{
cout << c << " ";
}
cout << endl;
cout << "1. GUESS THE WORD 2. GUESS THE LETTER" << endl;
cin >> option;
switch (option)
{
case 1: {
cout<<"GUESS THE WORD"<<endl;
cin>>word;
res=word_check(word,randomWord);
if(res==0)
{
cout<<"Sorry wrong guess!"<<endl;
}
tr--;
break;
}
case 2: {
cout << "GUESS THE LETTER" << endl;
cin >> letter;
size_t found = randomWord.find(letter);
if (found != string::npos)
{
cout << "Correct guess!" << endl;
while (found != string::npos)
{
guessedWord[found] = letter;
found = randomWord.find(letter, found + 1);
}
}
else
{
cout << "Incorrect guess!" << endl;
}
tr--;
break;
}
default:cout << "TRY AGAIN" << endl;
}
if (guessedWord == randomWord || res==1)
{
cout << "Congratulations! You guessed the word!" << endl;
break;
//tr = 0;
}
}
if(tr==0)
{
cout << "You ran out of tries! Game Over!" << endl;
}
cout << "The word was: " << randomWord << endl;
cout << endl;
cout << "EXITING THE GAME" << endl;
cout << ".................." << endl;
cout << "THANK YOU FOR PLAYING" << endl;
return 0;
}