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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
//Code source mostly based on HANGMAN by Sam Paterson.
//You may contact him at <peabodyenator@gmail.com>.
//Actual Hangman.cpp coded by Bluezor or Jackie.
//You may contact me at <***.******@gmail.com>.
/////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <ctime>
using namespace std;
vector <string> words;
vector <string> board;
vector <pair <pair <size_t, size_t>, char > > body;
set <char> used_letters;
string word;
string word_letters;
int randomNumber = rand() % 89523;
bool completed_loading = false;
inline void TOLOWER (string& st)
{
for (size_t i = 0; i < st.size(); i++)
{
st[i] = tolower(st[i]);
}
}
inline void check_cin()
{
if (!cin)
{
if (cin.eof())
exit(EXIT_SUCCESS);
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
void readFile()
{
string line;
ifstream myFile ("EnglishWords.txt");
if (myFile.is_open())
{
while (!myFile.eof())
{
getline (myFile, line);
words.push_back(line);
}
myFile.close();
completed_loading = true;
}
else
{
cout << "Could not locate or open 'EnglishWords.txt'." << endl;
cout << "Press ENTER to continue...";
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
}
void fill_place_holders()
{
for (size_t i = 0; i < word.size(); i++)
word_letters.append("-");
}
void pickWord()
{
word = words[randomNumber];
}
void print_board()
{
vector <string>::iterator it;
for (it = board.begin(); it != board.end(); it++)
{
cout << *it << endl;
}
cout << endl << endl << "Word: " << word_letters << endl;
cout << "Used letters: ";
set <char>::iterator set_it;
for (set_it = used_letters.begin(); set_it != used_letters.end(); set_it++)
{
cout << *set_it << " " << endl;
}
}
void create_board_and_body()
{
board.push_back ("-----");
board.push_back (" |");
board.push_back (" |");
board.push_back (" |");
board.push_back (" |");
board.push_back (" /|\\");
body.push_back(make_pair(make_pair(4,3), '\\'));
body.push_back(make_pair(make_pair(4,1), '/'));
body.push_back(make_pair(make_pair(3,2), '|'));
body.push_back(make_pair(make_pair(2,3), '\\'));
body.push_back(make_pair(make_pair(2,2), '|'));
body.push_back(make_pair(make_pair(2,1), '/'));
body.push_back(make_pair(make_pair(1,2), '0'));
}
void add_part()
{
if (!body.size())
{
cout << "The man got hanged!" << endl;
cout << "You thoroughly mourn to realise the word was " << word << endl;
cout << "Press ENTER to continue...";
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
exit(EXIT_SUCCESS);
}
pair <pair <size_t, size_t>, char > part = body.back();
body.pop_back();
board[part.first.first][part.first.second] = part.second;
}
void users_turn()
{
char letter;
cout << "Enter a letter to guess." << endl;
check_cin();
cin >> letter;
letter = tolower(letter);
while (used_letters.find(letter) != used_letters.end() || !isalpha(letter))
{
cout << "Letter taken already, or bad letter. \n\n" << endl;
cout << "Enter a letter to guess." << endl;
check_cin();
cin >> letter;
letter = tolower(letter);
}
used_letters.insert(letter);
bool found = false;
for (size_t i = 0; i < word.size(); i++)
{
if (word[i] == letter)
{
word_letters[i] = letter;
found = true;
}
}
if (word_letters.find("-") == string::npos)
{
cout << "You got it! The word is " << word << "!" << endl;
cout << "Congratulations! You saved the man from being hanged!" << endl;
exit(EXIT_SUCCESS);
}
if (!found)
add_part();
}
int main()
{
srand(time(0));
cout << "Choosing a word from 'EnglishWords.txt', please wait." << endl;
readFile();
pickWord();
if (completed_loading = true)
{
cout << "COMPLETE!" << endl;
}
fill_place_holders();
create_board_and_body();
print_board();
while(true)
{
users_turn();
print_board();
}
cout << "Press ENTER to continue...";
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return(0);
}
|