-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.cpp
More file actions
139 lines (132 loc) · 3.65 KB
/
hangman.cpp
File metadata and controls
139 lines (132 loc) · 3.65 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <bits/stdc++.h>
using namespace std;
void readDictionary(vector<pair<string, int>> &v)
{
ifstream file("dictionary.txt");
if (!file.is_open())
{
cerr << "Error opening file." << endl;
}
string line;
while (getline(file, line))
{
string formattedLine = "";
for (int i = 0; i < line.size(); i++)
{
if (isalpha(line[i]))
{
formattedLine += line[i];
}
}
v.push_back({formattedLine, formattedLine.size()});
}
file.close();
}
void generateRandomNumber(int &arrSize, int &randNum)
{
srand(static_cast<unsigned int>(std::time(nullptr)));
randNum = rand() % arrSize;
}
void playGame(string word, int len)
{
string coveredWord = "";
int coveredLen = len;
for (int i = 0; i < len; i++)
{
coveredWord += "_";
}
vector<char> enterredLetters;
string remainingChances = "hangman";
int remainingChancesLen = remainingChances.length();
char letter;
while (remainingChancesLen > 0 && coveredLen > 0)
{
cout << "Chances Remaining: " << remainingChances << " (" << remainingChancesLen << ")" << endl;
cout << endl;
cout << "Word: ";
for (int i = 0; i < len; i++)
{
cout << coveredWord[i] << " ";
}
cout << endl;
cout << endl;
cout << "Enter letter: ";
cin >> letter;
auto it = find(enterredLetters.begin(), enterredLetters.end(), letter);
while (it != enterredLetters.end())
{
cout << "Already enterred this letter. Enter again: ";
cin >> letter;
it = find(enterredLetters.begin(), enterredLetters.end(), letter);
}
enterredLetters.push_back(letter);
bool flag = false;
for (int i = 0; i < len; i++)
{
if (word[i] == letter)
{
coveredWord[i] = letter;
coveredLen--;
flag = true;
}
}
if (!flag)
{
remainingChancesLen--;
remainingChances = remainingChances.substr(0, remainingChancesLen);
}
cout << endl;
}
if (remainingChancesLen == 0)
{
cout << "Oops! You ran out of chances." << endl
<< "Word: " << word << endl
<< endl;
}
if (coveredLen == 0)
{
cout << "Congratulation! you guessed it correct!" << endl
<< "Word: " << coveredWord << endl
<< endl;
}
}
int main()
{
vector<pair<string, int>> words;
readDictionary(words);
int randomNumber, wordSize = words.size();
int choice;
bool flag = true;
while (flag)
{
cout << "-----------------" << endl;
cout << "---- HANGMAN ----" << endl;
cout << "-----------------" << endl;
cout << "1. Play" << endl
<< "2. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (choice)
{
case 1:
cout << endl;
generateRandomNumber(wordSize, randomNumber);
playGame(words[randomNumber].first, words[randomNumber].second);
break;
case 2:
flag = false;
break;
default:
flag = false;
break;
}
}
cout << "Goodbye!";
return 0;
}