0% found this document useful (0 votes)
32 views

Using Namespace Void Bool True Bool True If: Cout Cout

This C++ program implements a hangman game where it loads a random word from a file, draws the hangman as the player makes incorrect guesses, tracks the number of incorrect guesses, and declares a win or loss based on guessing the word correctly within 10 guesses. It uses functions to handle printing messages and drawings, tracking guesses, loading a random word, and determining if the player has won or lost. The main function runs the game loop, getting guesses from the user and updating the hangman drawing and number of guesses remaining until the game ends based on winning or losing.

Uploaded by

Shivam Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Using Namespace Void Bool True Bool True If: Cout Cout

This C++ program implements a hangman game where it loads a random word from a file, draws the hangman as the player makes incorrect guesses, tracks the number of incorrect guesses, and declares a win or loss based on guessing the word correctly within 10 guesses. It uses functions to handle printing messages and drawings, tracking guesses, loading a random word, and determining if the player has won or lost. The main function runs the game loop, getting guesses from the user and updating the hangman drawing and number of guesses remaining until the game ends based on winning or losing.

Uploaded by

Shivam Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

#include <iostream>
2. #include <vector>
3. #include <fstream>
4. #include <string>
5. #include <time.h>
6. using namespace std;
7. void PrintMessage(string message, bool printTop = true, boolprintBottom = true)
8. {
9. if (printTop)
10. {
11. cout << "+---------------------------------+" <<endl;
12. cout << "|";
13. }
14. else
15. {
16. cout << "|";
17. }
18. bool front = true;
19. for (int i = message.length(); i < 33; i++)
20. {
21. if (front)
22. {
23. message = " " + message;
24. }
25. else
26. {
27. message = message + " ";
28. }
29. front = !front;
30. }
31. cout << message.c_str();
32.
33. if (printBottom)
34. {
35. cout << "|" << endl;
36. cout << "+---------------------------------+" <<endl;
37. }
38. else
39. {
40. cout << "|" << endl;
41. }
42. }
43. void DrawHangman(int guessCount = 0)
44. {
45. if (guessCount >= 1)
46. PrintMessage("|", false, false);
47. else
48. PrintMessage("", false, false);
49.
50. if (guessCount >= 2)
51. PrintMessage("|", false, false);
52. else
53. PrintMessage("", false, false);
54.
55. if (guessCount >= 3)
56. PrintMessage("O", false, false);
57. else
58. PrintMessage("", false, false);
59.
60. if (guessCount == 4)
61. PrintMessage("/ ", false, false);
62.
63. if (guessCount == 5)
64. PrintMessage("/| ", false, false);
65.
66. if (guessCount >= 6)
67. PrintMessage("/|\\", false, false);
68. else
69. PrintMessage("", false, false);
70.
71. if (guessCount >= 7)
72. PrintMessage("|", false, false);
73. else
74. PrintMessage("", false, false);
75.
76. if (guessCount == 8)
77. PrintMessage("/", false, false);
78.
79. if (guessCount >= 9)
80. PrintMessage("/ \\", false, false);
81. else
82. PrintMessage("", false, false);
83. }
84. void PrintLetters(string input, char from, char to)
85. {
86. string s;
87. for (char i = from; i <= to; i++)
88. {
89. if (input.find(i) == string::npos)
90. {
91. s += i;
92. s += " ";
93. }
94. else
95. s += " ";
96. }
97. PrintMessage(s, false, false);
98. }
99. void PrintAvailableLetters(string taken)
100. {
101. PrintMessage("Available letters");
102. PrintLetters(taken, 'A', 'M');
103. PrintLetters(taken, 'N', 'Z');
104. }
105. bool PrintWordAndCheckWin(string word, string guessed)
106. {
107. bool won = true;
108. string s;
109. for (int i = 0; i < word.length(); i++)
110. {
111. if (guessed.find(word[i]) == string::npos)
112. {
113. won = false;
114. s += "_ ";
115. }
116. else
117. {
118. s += word[i];
119. s += " ";
120. }
121. }
122. PrintMessage(s, false);
123. return won;
124. }
125. string LoadRandomWord(string path)
126. {
127. int lineCount = 0;
128. string word;
129. vector<string> v;
130. ifstream reader(path);
131. if (reader.is_open())
132. {
133. while (std::getline(reader, word))
134. v.push_back(word);
135.
136. int randomLine = rand() % v.size();
137.
138. word = v.at(randomLine);
139. reader.close();
140. }
141. return word;
142. }
143. int TriesLeft(string word, string guessed)
144. {
145. int error = 0;
146. for (int i = 0; i < guessed.length(); i++)
147. {
148. if (word.find(guessed[i]) == string::npos)
149. error++;
150. }
151. return error;
152. }
153. int main()
154. {
155. srand(time(0));
156. string guesses;
157. string wordToGuess;
158. wordToGuess = LoadRandomWord("words.txt");
159. int tries = 0;
160. bool win = false;
161. do
162. {
163. system("cls"); //replace this line with system("clear"); if you run Linux or
MacOS
164. PrintMessage("HANGMAN");
165. DrawHangman(tries);
166. PrintAvailableLetters(guesses);
167. PrintMessage("Guess the word");
168. win = PrintWordAndCheckWin(wordToGuess, guesses);
169.
170. if (win)
171. break;
172.
173. char x;
174. cout << ">"; cin >> x;
175.
176. if (guesses.find(x) == string::npos)
177. guesses += x;
178.
179. tries = TriesLeft(wordToGuess, guesses);
180.
181. } while (tries < 10);
182.
183. if (win)
184. PrintMessage("YOU WON!");
185. else
186. PrintMessage("GAME OVER");
187.
188. system("pause"); //this line wont work on Linux or MacOS so remove it
189. getchar();
190. return 0;
191. }
192. /*
193. +---------------------------------+
194. | HANG MAN |
195. +---------------------------------+
196. | | |
197. | | |
198. | O |
199. | /|\ |
200. | | |
201. | / \ |
202. | +----------+ |
203. | | | |
204. +---------------------------------+
205. | Available letters |
206. +---------------------------------+
207. | A B C D E F G H I J K L M |
208. | N O P Q R S T U V W X Y Z |
209. +---------------------------------+
210. | Guess the word |
211. +---------------------------------+
212. | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
213. +---------------------------------+
214. >
215. */

You might also like