HANGMAN USING LINEAR SEARCH AND SELECTION SORTSORTING

i need help creating hangman with selection sort/Insertion sort and linear search algorithms
Last edited on
What are you having trouble with?
We Won't write it for you.
Post what you've written.
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
using namespace std;
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>

int x;
string word[20] = { "string", "function", "character", "integer", "address", "program", "compiler", "hardware", "prototype" };

void hangmanBuild(int level)
{

		if (level == 0)
		{
			cout << "\n\t ______________\n";
			cout << "\t| _____________|\n";
			cout << "\t| | //     |\n";
			cout << "\t| |//      |\n";
			cout << "\t| |/\n";
			cout << "\t| |\n";
			cout << "\t| |\n";
			cout << "\t| |\n";
			cout << "\t| |\n";
			cout << "\t| |\n";
			cout << "      __| |________________\n";
			cout << "     |_____________________|\n\n";
		}
		else
			if (level == 1)
			{
				cout << "\n\t ______________\n";
				cout << "\t| _____________|\n";
				cout << "\t| | //     |\n";
				cout << "\t| |//      |\n";
				cout << "\t| |/      ( )\n";
				cout << "\t| |\n";
				cout << "\t| |\n";
				cout << "\t| |\n";
				cout << "\t| |\n";
				cout << "\t| |\n";
				cout << "      __| |________________\n";
				cout << "     |_____________________|\n\n";
			}
			else
				if (level == 2)
				{
					cout << "\n\t ______________\n";
					cout << "\t| _____________|\n";
					cout << "\t| | //     |\n";
					cout << "\t| |//      |\n";
					cout << "\t| |/      ( )\n";
					cout << "\t| |        |\n";
					cout << "\t| |        |\n";
					cout << "\t| |\n";
					cout << "\t| |\n";
					cout << "\t| |\n";
					cout << "      __| |________________\n";
					cout << "     |_____________________|\n\n";
				}
				else
					if (level == 3)
					{
						cout << "\n\t ______________\n";
						cout << "\t| _____________|\n";
						cout << "\t| | //     |\n";
						cout << "\t| |//      |\n";
						cout << "\t| |/      ( )\n";
						cout << "\t| |     ___|\n";
						cout << "\t| |        |\n";
						cout << "\t| |\n";
						cout << "\t| |\n";
						cout << "\t| |\n";
					}
					else
						if (level == 4)
						{
							cout << "\n\t ______________\n";
							cout << "\t| _____________|\n";
							cout << "\t| | //     |\n";
							cout << "\t| |//      |\n";
							cout << "\t| |/      ( )\n";
							cout << "\t| |     ___|___\n";
							cout << "\t| |        |\n";
							cout << "\t| |\n";
							cout << "\t| |\n";
							cout << "\t| |\n";
						}
						else
							if (level == 5)
							{
								cout << "\n\t ______________\n";
								cout << "\t| _____________|\n";
								cout << "\t| | //     |\n";
								cout << "\t| |//      |\n";
								cout << "\t| |/      ( )\n";
								cout << "\t| |     ___|___\n";
								cout << "\t| |        |\n";
								cout << "\t| |       /\n";
								cout << "\t| |     _/\n";
								cout << "\t| |\n";
							}
							else
								if (level == 6)
								{
									cout << "\n\t ______________\n";
									cout << "\t| _____________|\n";
									cout << "\t| | //     |\n";
									cout << "\t| |//      |\n";
									cout << "\t| |/      ( )\n";
									cout << "\t| |     ___|___\n";
									cout << "\t| |        |\n";
									cout << "\t| |       / \\\n";
									cout << "\t| |     _/   \\_\n";
									cout << "\t| |\n";
								}
								else
									if (level == 7)
									{
										cout << "\n\t ______________\n";
										cout << "\t| _____________|\n";
										cout << "\t| | //     |\n";
										cout << "\t| |//      |\n";
										cout << "\t| |/      (x)\n";
										cout << "\t| |     ___|___\n";
										cout << "\t| |        |\n";
										cout << "\t| |       / \\\n";
										cout << "\t| |     _/   \\_\n";
										cout << "\t| |\n";
										cout << "\n\tOh no! You Loose! The correct word was: " << word[x] << "\n\n";
									}

}

int mysteryWord(string word)
{
	int wordLength = word.size(), k = 0, j = 0, i = 0, h = 0, m = 0, countRight1, countRight2 = 0;
	string wordBlanks = "                              ", wordBank = " ";
	char letter, underscore = '_';

	while (i < wordLength) // adds a " _" for each letter of word
	{
		wordBlanks.at(2 * i + 1) = underscore;
		i++;
	}

	while (m == 0) // if user guesses a correct character, a blank is replaced
	{
		countRight1 = 0;
		cout << "\t" << wordBlanks << "\tLetters guessed incorrectly: " << wordBank << "\n\n";
		cout << "\tGuess a letter: ";
		cin >> letter;
		letter = tolower(letter);
		k = 0;

		while ((k < wordLength))
		{
			if (word.at(k) == letter)
			{
				wordBlanks.at(2 * k + 1) = letter;
				countRight1++;
				countRight2++;
			}
			k++;
		}

		if (countRight1 == 0)
		{
			wordBank = wordBank + letter + " ";
			h++;
			hangmanBuild(h);
		}

		if (countRight2 == wordLength) // if all letters are guessed
		{
			cout << "\t" << wordBlanks;
			cout << "\n\n\tCongratulations! You won!\n\n";
			m++;
			return x;
		}

		if (h == 7)
		{
			m++;
			return x;
		}


	}

	x++;
	return x;
}

int main()
{
	char play;
	cout << "\n\n\tHello! Welcome to the Hangman Game.\n\n";
	do
	{
		cout << "\tWould you like to play? (y/n): ";
		cin >> play;

		while ((play != 'y') && (play != 'n') && (play != 'Y') && (play != 'N')) // error message
		{
			cout << "\n\tI'm sorry, I didn't understand that.\n\n";
			cout << "\tPlease type 'Y' to play or 'N' to quit: ";
			cin >> play;
		}

		if ((play == 'y') || (play == 'Y'))
		{
			int hangman = 0;
			x = (rand() % (19 - 0 + 1)) + 0;
			hangmanBuild(0);
			mysteryWord(word[x]);

		}
		else if ((play == 'N') || (play == 'n'))
		{
			cout << "\n\tOk, goodbye! ";
			return 0;
		}

	} while (play);
	return 0;
}
Last edited on
I want to use linear search and sorting algorithm in this code
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Okay thanks I edited it.
I want to use linear search and sorting algorithm in this code


Where for what? If you want to sort something, then there is std::sort()

http://www.cplusplus.com/reference/algorithm/sort/

For linear search (without using an explicit for loop et al), then there are various find() functions in std::algorithms

http://www.cplusplus.com/reference/algorithm/
@seeplus
The point of the assignment is to write the linear search and selection sort yourself. Not to use the standard library (BTW, std::sort() uses Introsort).
@Duthomhas
Can you help?
i only understand how to use those algorithms on numbers, i am new to C++
I'll ask again. What do you want to sort and search?
I want to sort and search letters for the word that has to be guessed. I have to write a program for word guessing game
typically in hangman type games, you do something like this to directly map it. I am going to use C strings and simple logic so you can see what is going on:

1
2
3
4
5
6
7
8
9
10
11
12
13
char word[100] = "word"; //you will have logic to get a random word here
char hide[100] = "____"   //you will have logic to fill this with blanks for the length of word here
... game logic 
bool hang{true}; //should we hang the sucker?
for(int i = 0; i < strlen(word); i++)
{
    if(word[i] == guess)
       {
          hide[i] = guess;
          hang = false;
       }
}

so if the user enters say 'd' as guess, the loop runs through it..
w == d? nope... hang true
o == d? nope... hang true
r == d? nope... hang true
d==d yes, hang false, update hide.
hide is now "___d"
user guesses 'x'
nothing matches, hang stays true, so you add a piece to your drawing/count of how dead the player is.
My advice is to simply have 10 guesses or so, decrement when they get hung, and print the remaining guesses each go until you get the logic right, then you can fool with drawing the thing based off # of guesses left as your last step. It saves clutter in the code, and on screen, while debugging it.

Notice: no sort in that. It does a linear search as you said, though: that is what the loop really is.

if you need to sort something, for the assignment, sort the random word list or something.
and we can help with that. If you use c++ strings, it works JUST LIKE integers; you can use a < b on strings to compare them, and std::swap() works fine on strings too.
Last edited on
Okay i got the concept of Linear now all i need now is the sort concept
There are loads of resources on the internet re different types of sort. If you can sort an array of ints, then you can sort an array of std::string. The code is the same.

eg. for insertion sort see https://www.softwaretestinghelp.com/insertion-sort/
selection sort see https://www.softwaretestinghelp.com/selection-sort/

As word is an array of std::string, then the numeric sorting algorithms work just the same.

Have a go and if you get issues, post your attempted code.
As a first refactor, consider:

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
#include <iostream>
#include <iomanip>
#include <string>
#include <random>
#include <algorithm>
#include <cctype>

std::mt19937 rng(std::random_device {}());

const std::string words[] {"string", "function", "character", "integer", "address", "program", "compiler", "hardware", "prototype"};

void hangmanBuild(unsigned level) {
	std::cout << "Level " << level << '\n';
}

void mysteryWord(const std::string& word) {
	constexpr size_t maxTries {7};
	std::string wordBlanks (word.size(), '_');
	std::string wordBank;
	unsigned h {};

	//std::cout << word << '\n';
	std::cout << "The word has " << word.size() << " letters\n";

	while (h < maxTries && wordBlanks != word) {
		unsigned char letter {};
		bool found {};
		bool bad {};

		std::cout << "\t" << wordBlanks << "\tLetters guessed incorrectly: " << wordBank << "\n\n";

		do {
			bad = false;
			std::cout << "\tGuess a letter: ";
			std::cin >> letter;
			letter = static_cast<unsigned char>(tolower(letter));

			if (wordBank.find(letter) != std::string::npos || wordBlanks.find(letter) != std::string::npos) {
				std::cout << "That letter has already been tried\n";
				bad = true;
			}
		} while (bad);

		for (size_t k {}; k < word.size(); ++k)
			if (word[k] == letter) {
				wordBlanks[k] = letter;
				found = true;
			}

		if (!found) {
			hangmanBuild(++h);
			wordBank += letter;
			std::sort(wordBank.begin(), wordBank.end());
		}
	}

	if (wordBlanks != word)
		std::cout << "\nHanged\nThe word was '" << word << "'\n";
	else
		std::cout << word << "\n\nCongrats - you escaped the noose!\n";
}

int main() {
	std::uniform_int_distribution<size_t> distrib(0, std::size(words) - 1);
	unsigned char play {};

	std::cout << "\n\n\tHello! Welcome to the Hangman Game.\n\n";

	do {
		std::cout << "\tWould you like to play? (y/n): ";

		while ((std::cin >> play) && ((play = static_cast<unsigned char>(tolower(play))) != 'y') && (play != 'n')) {
			std::cout << "\n\tI'm sorry, I didn't understand that.\n\n";
			std::cout << "\tPlease type 'Y' to play or 'N' to quit: ";
		}

		if ((play == 'y')) {
			hangmanBuild(0);
			mysteryWord(words[distrib(rng)]);
		}
	} while (play == 'y');

	std::cout << "\n\tOk, goodbye! ";
}


This uses c++ random rather than c's rand()/srand().

It uses std::sort() on line 53 to sort the incorrect letters used. If wanted, this could be replaced by insertion/selection sort.

L44-48 are a linear search of the word to be guessed for the entered letter.

For testing, I've just replaced hangmanBuild() so that is simply displays the level.
Last edited on
Topic archived. No new replies allowed.