the game is quite simple, you have to constantly guess the letters of a hidden word, but has a limited number of wrong attempts. The word you are looking for should be a random word from Wikipedia. I would like to get a review from you.
I used libcURL and the cURLpp wrapper.
Compilation (with Cygwin): g++ -L/usr/local/lib Hangman.cpp -o hm.exe -lcurl -lcurlpp
Hangman.cpp
:
#include <iostream>
#include <vector>
#include <string>
#include <regex>
#include <curlpp/Easy.hpp>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Options.hpp>
using namespace std;
string getRandomWordFromWikipedia()
{
curlpp::Cleanup myCleanup;
curlpp::Easy handle;
handle.setOpt(curlpp::options::Url(string("https://en.wikipedia.org/wiki/special:random")));
handle.setOpt(curlpp::options::FollowLocation(true));
ostringstream os;
os << handle;
string seq = os.str();
regex rgx1("<h1.+id=\"firstHeading\"[^>]*>(.+)</h1>");
regex rgx2("<[^>]+>(.+)</[^>]+>");
smatch sm1;
if (regex_search(seq, sm1, rgx1))
{
string seq2 = sm1[1];
while (seq2.find("<") != string::npos)
{
smatch sm2;
if (regex_search(seq2, sm2, rgx2))
{
seq2 = sm2[1];
}
else
{
return "";
}
}
return seq2;
}
return "";
}
struct ToFind
{
string w1;
string w2;
int tries = 0;
int right = 0;
int health = 100;
int hintsLeft = 2;
};
void initToFind(struct ToFind *stf)
{
string w1 = getRandomWordFromWikipedia();
string w2;
for (int i = 0; i < w1.size(); i++)
{
if (w1[i] == ' ')
{
w2 += " ";
}
else
{
w2 += "_ ";
}
}
stf->w1 = w1;
stf->w2 = w2;
}
int findInToFind(struct ToFind *stf, char c)
{
int f = 0;
for (int i = 0; i < stf->w1.size(); i++)
{
if (stf->w1[i] == c && stf->w2[i * 2] == '_')
{
f++;
stf->right++;
stf->w2[i * 2] = c;
}
}
stf->tries++;
if (f > 0)
{
stf->right++;
}
else
{
stf->health -= 10;
}
return f;
}
void printStatistics(struct ToFind *stf)
{
static int round = 1;
cout << "Round: " << round << endl;
cout << "Tries: " << stf->tries << endl;
cout << "Rights: " << stf->right << endl;
cout << "Health: " << stf->health << " from 100" << endl;
cout << "Hints left: " << stf->hintsLeft << endl;
round++;
}
int main(int argc, char const *argv[])
{
struct ToFind stf;
initToFind(&stf);
if (stf.w1.empty())
{
return EXIT_FAILURE;
}
cout << "The game begins!" << endl;
cout << "The word you are looking for has a length of " << stf.w1.size() << endl;
while (stf.w2.find("_") != string::npos && stf.health > 0)
{
printStatistics(&stf);
cout << stf.w2 << endl;
cout << "Which letter next?" << endl;
char c;
cin >> c;
int f = findInToFind(&stf, c);
if (f > 0)
{
cout << "Great, the letter " << c << " appeared " << f << " times!" << endl;
}
else
{
cout << "Unfortunately you weren't lucky this time ..." << endl;
}
}
cout << "The game is over ..." << endl;
if (stf.health > 0)
{
cout << "Great, you found the word " << stf.w1 << endl;
}
else
{
cout << "Unfortunately you didn't find the word " << stf.w1 << " and you're dead." << endl;
}
printStatistics(&stf);
cout << "Any char to continue ..." << endl;
char c;
cin >> c;
return EXIT_SUCCESS;
}
findInToFind
.right
is incremented multiple times. It should be incremented only once and should indicate the percentage of correct attempts. \$\endgroup\$