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.