Hangman Game High Scores

Im trying to do a hangman game and enter high scores. Here's my code I just need help getting started on making a way to enter the best scores of the game

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>


using namespace std;


const int MAX_TRIES = 10;

int letterFill(char, string, string&);

string displayHangman(int numIncorrectGuesses);

//write some cout<< code to display something hanging above the guy
string displayHangingStation(int x);

int main()
{
string name;
char letter;
int num_of_wrong_guesses = 0;
string word;
string words[] =
{
"apple",
"orange",
"car",
"truck",
"bicycle",
"cat",
"dog",
"snake",
"rock",
"sock"
};

//choose and copy a word from array of words randomly
srand(time(NULL));
int n = rand() % 10;
word = words[n];

// Initialize the secret word with the * character.
string unknown(word.length(), '*');


// welcome the user
cout << "\n\n WELCOME TO THE HANGMAN GAME";
cout << "\n\nYou have " << MAX_TRIES << " attempts to try and guess the word.";


// Loop until the guesses are used up
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << "\n\n" << unknown;
cout << "\n\nGuess a letter: ";
cin >> letter;
// Fill secret word with letter if the guess is correct,
// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Whoops! That letter isn't in there!" << endl;
num_of_wrong_guesses++;
cout << displayHangingStation(num_of_wrong_guesses);
cout << displayHangman(num_of_wrong_guesses) << endl;
}
else
{
cout << endl << "Good job you found a letter!" << endl;
}
// Tell user how many guesses has left.
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
// Check if user guessed the word.
if (word == unknown)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}
}

if (num_of_wrong_guesses == MAX_TRIES)
{
cout << "\nSorry you lose :(" << endl;
cout << "The word was : " << word << endl;
}

cin.ignore();
cin.get();
return 0;

/* Take a one character guess and the secret word, and fill in the
unfinished guessword. Returns number of characters matched.
Also, returns zero if the character is already guessed. */


}

int letterFill(char guess, string secretword, string &guessword)
{
int i;
int matches = 0;
int len = secretword.length();
for (i = 0; i< len; i++)
{
// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;

// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}

string displayHangman(int x)
{
for (int i = 0; i < 10; i++)
{
if (x == 1)
{
return " O";
}
if (x == 2)
{
return " O\n |";
}
if (x == 3)
{
return " O\n |\n |";
}
if (x == 4)
{
return " O\n |\n |\n /";
}
if (x == 5)
{
return " O\n |\n |\n /\\";
}
if (x == 6)
{
return " O\n -|\n |\n /\\";
}
if (x == 7)
{
return " O\n -|-\n |\n /\\";
}
if (x == 8)
{
return " O\n -|-\n |_\n /\\";
}
if (x == 9)
{
return " O\n -|-\n |__\n /\\";
}
else if (x == 10)
{
return " O\n -|-\n |___\n /\\";
}
}

}

string displayHangingStation(int x)
{
string station = "\n___________\n|_________|\n |\n |\n";
return station;
}

You can try saving the top score/s into a file(or memory) and open it everytime you enter say, from a menu(check rreference for fstream). Or do you mean something like a scoring system?
I was kinda hoping for help on a scoring system
A scoring system for Hangman? Well you get points for an original question OP. I would say that you would need to 'weigh' the words and assign them base values depending on string length and the number of unique characters. You would then subtract from that base score for every incorrect guess that the player makes. If it were me, I would assign bonus points for correct consecutive answers when it might indicate that the player knows what the word is.
try this:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <windows.h>
using namespace std;
const int MAX_TRIES=8;
int triesleft=8,score=100;
int letterFill(char, string, string&);
string displayHangman(int num_of_wrong_guesses);
string displayHangingStation(int x);

int main()
{
string name;
char letter;
int num_of_wrong_guesses = 0;
string word;
string words[] =
{
"apple",
"orange",
"car",
"truck",
"bicycle",
"cat",
"dog",
"snake",
"rock",
"sock"
};

///choose and copy a word from array of words randomly
srand(time(NULL));
int n = rand() % 10;
word = words[n];

/// Initialize the secret word with the * character.
string unknown(word.length(), '*');


/// welcome the user
cout << "\n\n WELCOME TO THE HANGMAN GAME";
cout << "\n\n You have 8 attempts to try and guess the word.";

/// Loop until the guesses are used up
while (num_of_wrong_guesses <= triesleft)
{
cout << "\n\n" << unknown;
cout << "\n\nGuess a letter: ";
cin >> letter;
/// Fill secret word with letter if the guess is correct,
/// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Whoops! That letter is already there!" << endl;
cout << displayHangingStation(num_of_wrong_guesses);
cout << displayHangman(num_of_wrong_guesses) << endl;
}
else
{
cout << endl << "Good job you found a new letter!" << endl;
}
/// Tell user how many guesses has left.
cout << "You have " <<triesleft--;
cout << " guesses left." << endl;
/// Check if user guessed the word.
if (word == unknown)
{
/if (word.length()-num_of_wrong_guesses==word.length())
{score=100;}
else if (word.length()-num_of_wrong_guesses==word.length()-1)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-2)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-3)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-4)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-5)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-6)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses==word.length()-7)
{score=word.length()/num_of_wrong_guesses;}
else if (word.length()-num_of_wrong_guesses>=word.length()-8)
{score=word.length()/num_of_wrong_guesses;}
cout << word << endl;
cout << "Yeah! You got it!\nYour Score was "<<score<<"%"<< endl;
break;
}
}

if (num_of_wrong_guesses >= triesleft)
{
cout << "\nSorry you lose :(\nYour Score was "<<score<<"%"<< endl;
cout << "The word was : " << word << endl;
}

cin.ignore();
cin.get();

/** Take a one character guess and the secret word, and fill in the
unfinished guess word. Returns number of characters matched.
Also, returns zero if the character is already guessed. */


}

int letterFill(char guess, string secretword, string &guessword)
{
int i;
int matches = 0;
int len = secretword.length();
for (i = 0; i< len; i++)
{
/// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;

/// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
string displayHangman(int x)
{
for (int i = 0; i < 10; i++)
{
if (x == 1)
{
return "  O";
}
if (x == 2)
{
return "  O\n  |";
}
if (x == 3)
{
return "  O\n  |\n  |";
}
if (x == 4)
{
return "  O\n  |\n  |\n /";
}
if (x == 5)
{
return "  O\n  |\n  |\n /\\";
}
if (x == 6)
{
return "  O\n -|\n  |\n /\\";
}
if (x == 7)
{
return "  O\n -|-\n  |\n /\\";
}
if (x == 8)
{
return "  O\n -|-\n  |\n /\\";
}
}
}
string displayHangingStation(int x)
{
string station = "\n___________\n|_________|\n  |\n  |\n";
return station;
}
I don't know how you want it to be scored , say maybe like a time limit, or weighted letter score(like when you are playing scrabble), or just the simple system based on the correct number of correct answer etc. Assuming it may be the third one.
// Check if user guessed the word.
if (word == unknown)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}

or here if you want it for every correct letter answer,
else
{
cout << endl << "Good job you found a letter!" << endl;
}

just add a variable, (ex: unsigned int x = 0) so that whenever the answer is right, it increments by 1, x++ or x+=1, your choice. :)
Topic archived. No new replies allowed.