Skip to content

Instantly share code, notes, and snippets.

@tywoplenty
Created March 4, 2015 20:20
Show Gist options
  • Save tywoplenty/a7aea7da790060fc4814 to your computer and use it in GitHub Desktop.
Save tywoplenty/a7aea7da790060fc4814 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <cctype>
#include <sstream>
#include <conio.h>
#include <windows.h>
using namespace std;
vector<string> words, words2;
vector <string> hints;
void loadgame(), pickaword(), play(),welcome(),playagain();
string THE_WORD;
int answer;
const int MAX_ATTEMPTS = 10;
void zee(){
Sleep(10000);
};
int main()
{ welcome();
cin >> answer;
while (answer==1){
loadgame();
pickaword();
play();
playagain();
}
cout << "Goodbye!!!";
return 0;
}
void welcome() {
string name;
cout << "Enter your first name:";
cin >> name;
cout << "\nWelcome " << name << " to the hanging man game." << endl <<"\nOptions:\n1- Start Game\n0- Quit Game\n";
}
void loadgame() {
ifstream wordfile("words.csv");
string line;
if (wordfile.is_open()) {
{//1st category
getline(wordfile,line);
istringstream s(line);
string word;
while (getline(s, word,',')) {
words.push_back(word);
}
}
{//2nd category
getline(wordfile,line);
istringstream s(line);
string word;
while (getline(s, word,',')) {
words2.push_back(word);
}
;
}
}
else {
cout << "file cannot be open" << endl;
}
wordfile.close();
}
void play() {
string sofar(THE_WORD.size(),'-'); // correct guesses made so far
string used = ""; // tried letters
int wrongs=0; // wrong attempts
cout << endl << "\nLives = " <<MAX_ATTEMPTS <<endl;
char guess;// declaring new character variable for guess
while (wrongs<MAX_ATTEMPTS && sofar!=THE_WORD) {
// welcome
// list number of lives left
cout << "So far the word is: " << sofar << endl; // shows guesses so far
cout << "USED: " << used << endl; //list letters you have tried before
cout << "\nEnter your Guess: ";
cin >> guess; // collect guess from player
guess=toupper(guess); //convert guess to capital letter to compare
// if user have tried the letter before run the following code:
while (used.find(guess)!=string::npos){
cout << "\nYou've already tried " << guess << endl;
cout << "USED: " << used << endl;
cout << "\nEnter your Guess: ";
cin >> guess;
guess = toupper(guess);
}
// add new letter to used guesses
used+=guess;
// if guess is present in the word
if (THE_WORD.find(guess)!= string::npos) {
cout << "\n..................That's right......" << guess << "......is a word.............\n";
/*update so far*/
for (unsigned int i =0; i<THE_WORD.size();++i) {
if (THE_WORD[i]==guess) {
sofar[i]=guess;
}
}
}
else {
cout << "\n*****************Wrong Answer*****************" << endl;
++wrongs; // increase wrong attempts
cout << "You have " << (MAX_ATTEMPTS-wrongs) << " Lives left\n" <<endl;
}
}
if (sofar==THE_WORD) {
cout << "\n^^^^^^^^^^^^^^^^You guessed it^^^^^^^^^^^^^^^^^^" << endl;
cout << "The word is " << THE_WORD << endl;
}
else if (wrongs==MAX_ATTEMPTS) {
cout << "YOU'VE BEEN HANGED" << endl;
zee();
}
}
void pickaword() {
int choice;
cout << "\nChoose a level:\nEnter '1' for Easy.\nEnter '2' for Difficult." << endl;
cin >> choice;
srand(time(0));
switch (choice) {
case 1:
random_shuffle(words.begin(),words.end());
THE_WORD=words[0];
break;
case 2:
random_shuffle(words2.begin(),words2.end());
THE_WORD=words2[0];
break;
}
}
void playagain() {
cout << "\nDo you want to play again:\nEnter '1' for yes.\n'0'to quit";
cin >> answer;
}
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="vectorhang" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/vectorhang" 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/vectorhang" 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
1419001943 source:c:\users\taiwo\desktop\grading program\vectorhang\main.cpp
<iostream>
<vector>
<algorithm>
<string>
<cstdlib>
<ctime>
<fstream>
<cctype>
<sstream>
<conio.h>
<windows.h>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="420" topLine="10" />
</Cursor>
</File>
</CodeBlocks_layout_file>
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 5 columns, instead of 3. in line 1.
TAIWO,KEHINDE,ILERI,IFE,ANKARA
ENCYCLOPEDIA,ISTANBULDA,MAINDOCKS
TAIWO, first name
ANKARA, city
LONDON, country
HOME, place
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment