repl.it
C++

No description

fork
loading
Files
  • main.cpp
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Hangman
// The classic game of hangman
// Author: Justin Graham


// I made it to where they can choose difficulty rather than the topic

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

vector<string> hangman;

int wrong = 0;								// number of incorrect guesses
string board;
string used = "";							// letters already guessed
string THE_WORD;
char guess;

void getguess();
void checkguess();
void instructions();

int main()
{
	cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
	cout << "\n\t\t\t  Hangman\n";
	cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

	string howto;
	cout << "\nDo you know how to play (y/n)?";
	cin  >> howto;

	while(howto != "y" && howto != "Y" && howto != "n" && howto != "N")
	{
		cin.clear();
		cin.ignore(99999, '\n');
		cout << "Incorrect syntax. Please enter either y or n: ";
		cin  >> howto;
	}

	if(howto == "n" || howto == "N")
	{
		instructions();
	}

	const int MAX_WRONG = 7;				// maximum number of incorrect guesses allowed

	vector<string> words;					// collection of possible words to guess
	// difficulty 1
	words.push_back("EASY GAME");
	words.push_back("MAKE IT RAIN");
	words.push_back("REPLIT IS FUN");
	words.push_back("ANDROIDS ARE BAD");

	// difficulty 2
	words.push_back("WINDOWS IS BETTER");
	words.push_back("HOMEWORK IS GROSS");
	words.push_back("I WILL WIN AT THIS");
	words.push_back("BUY A SUPER MONKEY");

	// difficulty 3
	words.push_back("COLLEGE READINESS");
	words.push_back("MINESWEEPER IS LIT");
	words.push_back("PROGRAMMING IS COOL");
	words.push_back("IPHONE IS THE BEST PHONE");

	srand(static_cast<unsigned int>(time(0)));

	cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
	cout << "\tDifficulty Selection\n";
	cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
	cout << " 1) Easy " << endl;
	cout << " 2) Medium " << endl;
	cout << " 3) Hard " << endl;

	int difficulty;
	cout << " Please select a difficulty (1-3): ";
	cin  >> difficulty;

	while(cin.fail() || difficulty > 3 || difficulty < 1)
	{
		cin.clear();
		cin.ignore(99999, '\n');
		cout << "Incorrect syntax. Please enter a selection between 1 and 3: ";
		cin  >> difficulty;
	}

	// difficulty selection
	if(difficulty == 1)
	{
		// words 0-3
		random_shuffle(words.begin(), words.begin() + 3);
		THE_WORD = words[0];					// word to guess
	}

	if(difficulty == 2)
	{
		// words 4-7
		random_shuffle(words.begin() + 4, words.begin() + 7);
		THE_WORD = words[4];					// word to guess
	}

	if(difficulty == 3)
	{
		// words 8-11
		random_shuffle(words.begin() + 8, words.end());
		THE_WORD = words[8];					// word to guess
	}

	board = THE_WORD;

	// changes letters to dashes; leaves the spaces there (makes it capable for multiple words)
	for(int pos = 0; pos != board.length(); pos++)	
	{
		if(board[pos] != ' ')
		{
			board[pos] = '-';
		}
	}

    // main loop
	while ((wrong < MAX_WRONG) && (board != THE_WORD))
    {
		cout << "\nYou have " << (MAX_WRONG - wrong);
		cout << " incorrect guesses left.\n";

		cout << "\n====================";
		cout << "\n                   !";
		cout << "\n                   !\n";
		for(int i = 0; i < hangman.size(); ++i)
		{
			cout << "                 " << hangman[i] << endl;
		}

		cout << "\nYou've used the following letters:\n" << used << endl;
		cout << "\nSo far, the phrase is:\n" << board << endl;

		cout << "\n\nEnter your guess: ";
		getguess();

		used += guess;

		checkguess();
    }

	cout << "\n====================";
	cout << "\n                   !";
	cout << "\n                   !\n";
	for(int i = 0; i < hangman.size(); ++i)
	{
		cout << "                 " << hangman[i] << endl;
	}

    // shut down
	if (wrong == MAX_WRONG)
	{
		cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
		cout << "\nYou've seem to have been hanged, sadly.";
	}
	else
	{
		cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
		cout << "\nYou guessed it! Good job!";
	}

	cout << "\nThe phrase was " << THE_WORD << endl;

	return 0;
}

void getguess()
{
	cin >> guess;
	guess = toupper(guess);					//make uppercase since secret word in uppercase
	while (used.find(guess) != string::npos)
    {
		cout << "\nYou've already guessed " << guess << endl;
		cout << "Enter your guess: ";
		cin >> guess;
		guess = toupper(guess);
    }
}

void checkguess()
{
	if(THE_WORD.find(guess) != string::npos)
	{
		cout << "That's right! " << guess << " is in the phrase.";

		// update board to include newly guessed letter
		for (unsigned int pos = 0; pos < THE_WORD.length(); pos++)
		{
			if(THE_WORD[pos] == guess)
			{
				board[pos] = guess;
			}
		}
	}
	else
	{
		cout << "Sorry, " << guess << " isn't in the phrase.";

		wrong++;

		if(wrong == 1)
		{
			hangman.push_back("  O");
		}
		else if(wrong == 2)
		{
			hangman.push_back("  |");
		}
		else if(wrong == 3)
		{
			hangman.pop_back();
			hangman.push_back("\\ |");
		}
		else if(wrong == 4)
		{
			hangman.pop_back();
			hangman.push_back("\\ | /");
		}
		else if(wrong == 5)
		{
			hangman.push_back("  |");
		}
		else if(wrong == 6)
		{
			hangman.push_back("/");
		}
		else if(wrong == 7)
		{
			hangman.pop_back();
			hangman.push_back("/   \\");
		}
	}
}

void instructions()
{
	cout << "\nYou'll be given a topic and there'll be dashes, each representing a letter.\n";
	cout << "All you have to do is guess the letters to replace the dashes before you guess seven times incorrectly.\n";
}
Fetching token
?