forked from trinhvua1/AdvProg_L3-HangMan
/
hangman.cpp
180 lines (162 loc) · 4.39 KB
/
hangman.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include "hangman.h"
#include <cstdlib>
using std::cin;
using std::domain_error;
using std::ifstream;
using std::string;
using std::vector;
/***
Args:
min (int): left margin of a range
max (int): right margin of a range
Returns:
number (int) : random number in range [min; max]
***/
int generateRandomNumber(const int min, const int max)
{
return rand() % (max - min + 1) + min;
}
vector<string> readWordListFromFile(const string &filePath)
{
vector<string> wordList;
string word;
ifstream wordFile(filePath);
if (!wordFile.is_open())
{
throw domain_error("Unable to open file");
}
// while ( getline (wordFile, word) ){ // Thong thuong doc tung line.
// Chuong trinh nay cung chay.
while (wordFile >> word)
{ // Nhung voi chuong trinh nay, doc tung word cung duoc
// Tuc ca 2 cach doc deu chay.
wordList.push_back(word);
// cout << word << '\n';
}
wordFile.close();
return wordList;
}
/***
Args:
ch (char): A character
word (string): a word
Returns:
result (bool) : the character ch is in the word or not.
***/
bool isCharInWord(const char ch, const string &word)
{
return word.find(ch) != string::npos;
}
/***
Args:
wordList (vector<string>): A list of words
index (int): an integer number
Returns:
answer (string) : the lowercase word is in the position index of wordList
***/
string chooseWordFromList(const vector<string> &wordList, int index)
{
// TODO: Return a lowercase word in the index position of the vector wordList.
string answer = wordList[index];
for (char &c : answer)
{
c = tolower(c);
}
return answer;
}
/***
Args:
answerWord (string): a word that player needs to guess
Returns:
secretWord (string): answerWord in hidden form (form of ---)
***/
string generateHiddenCharacters(string answerWord)
{
// TODO: Based on answerWord's length, generate hidden characters in form of "---"
string secretWord(answerWord.size(), '-');
return secretWord;
}
char getInputCharacter()
{
char ch;
cin >> ch;
return tolower(ch);
}
/***
Args:
secretWord (string): secret word in hidden form
ch (char): a charater
word (string): the answer word
Returns:
void
***/
void updateSecretWord(string &secretWord, const char ch, const string &word)
{
// TODO: Update the secret word if the character ch is in the answer word.
for (int i = 0; i < secretWord.size(); ++i)
{
if (word[i] == ch)
{
secretWord[i] = ch;
}
}
}
/***
Args:
ch (char): a character
chars (string): an array of characters
Returns:
void
***/
void updateEnteredChars(const char ch, string &chars)
{
// TODO: append the character ch is in end of the text chars
chars = chars + ch + ' ';
}
/***
Args:
incorrectGuess (int): a number that store the number of player's wrong guess
Returns:
void
***/
void updateIncorrectGuess(int &incorrectGuess)
{
// TODO: increase the value of incorrectGuess by 1
++incorrectGuess;
}
/***
Args:
ch (char): a character that player enter to console
word (string): answer word that play needs to guess
secretWord (string): answer word in hidden form
correctChars (string): a string that stores correct inputs of player
incorrectGuess (int): a number that stores the number of player's wrong guess
incorrectChars (string): a string that stores incorrect inputs of player
Returns:
void
***/
void processData(const char ch, const string &word,
string &secretWord,
string &correctChars,
int &incorrectGuess, string &incorrectChars)
{
/*** TODO
If ch in word:
update secretWord: call updateSecretWord() function
update correctChars: call updateEnteredChars() function
else:
update incorrectGuess: call updateIncorrectGuess() function
update incorrectChars: call updateEnteredChars() function
***/
if (isCharInWord(ch, word))
{
updateSecretWord(secretWord, ch, word);
updateEnteredChars(ch, correctChars);
}
else
{
updateIncorrectGuess(incorrectGuess);
updateEnteredChars(ch, incorrectChars);
}
}