Skip to content

Instantly share code, notes, and snippets.

@tywoplenty
Created March 4, 2015 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tywoplenty/b20ea82cd27d93a8a6b5 to your computer and use it in GitHub Desktop.
Save tywoplenty/b20ea82cd27d93a8a6b5 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <string>
#include <cctype>
#include <conio.h>
#include <windows.h>
using namespace std;
void shuffle(vector<string> &rvec);
void takeguess(char& rguess);
void welcome(int& rcont);
void checkguess(string &rused, char &rguess);
void checkinword(const string &rtheword, const char &rguess, int &attempt, string &rsofar);
const int MAX_ATTEMPTS=5;
int wrongs;
int main()
{
char guess;
int cont;
vector<string> wordlist;
wordlist.push_back("TAIWO");
wordlist.push_back("KEHINDE");
wordlist.push_back("ILERI");
wordlist.push_back("IFE");
shuffle(wordlist);
const string THE_WORD = wordlist[0];
string sofar(THE_WORD.size(),'_');
welcome(cont);
while (cont==1) {
wrongs=0;
string notused="";
while (wrongs!=MAX_ATTEMPTS && sofar!=THE_WORD) {
takeguess(guess);
checkguess(notused, guess);
checkinword(THE_WORD, guess, wrongs, sofar);
}
if (wrongs==MAX_ATTEMPTS){
cout << "You've been hanged" << endl;
Sleep(10000);
}
else if (sofar==THE_WORD){
cout << "Congratulations you won!" << endl;
}
cout << "Will you like to play again\nEnter:\n1 - to continue.\n2 - quit the game." << endl;
cin >> cont;
}
cout << "Good Bye";
}
void checkinword(const string &rtheword, const char &rguess, int &attempt, string &rsofar) {
if (rtheword.find(rguess)!=string::npos){
cout << "Your Guess is Right" << endl;
for (unsigned int i=0; i<rtheword.size();i++){
if (rtheword[i]==rguess) {
rsofar[i]=rguess;
}
}
cout << "So far the word is: " << rsofar << endl;
}
else {
cout << "wrong guess" << endl;
wrongs++;
}
}
void checkguess(string &rused, char &rguess) {
rguess = toupper(rguess);
while (rused.find(rguess)!=string::npos) {
cout << "\nYou have tried this character before. Try a new guess: ";
cin >> rguess;
}
rused += rguess;
}
void welcome(int& rx) {
string name;
cout << "Enter your Name to start the game: ";
cin >> name;
cout << "Hello " << name << ", You are welcome to the hanging man game 2.0\nDo you want to Continue.\nEnter:\n1 - to continue.\n2 - to quit." << endl;
cin >> rx;
}
void shuffle(vector<string> &rvec) {
srand(time(0));
random_shuffle(rvec.begin(),rvec.end());
}
void takeguess(char& rx) {
cout << "You have " << (MAX_ATTEMPTS - wrongs) << " attempts left" << endl;
cout << "Enter your Guess" << endl;
cin >> rx;
}
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="mine" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/mine" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/mine" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="main.cpp" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1419182873 source:c:\users\taiwo\desktop\grading program\mine\main.cpp
<iostream>
<vector>
<algorithm>
<ctime>
<string>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="158" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment