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
|
void hangman(int outcount, int quest, string origword)
{
srand(time(0));
ifstream infile;
string letter = "";
string fileword = "";
string list = "--------------------------";
string stt = "";
string displayword = "-----";
char dashreplaced = 'N';
char gameover = 'N';
int mark = 0;
int count = 0;
int numincorrect = 0;
int left = 10;//0 ORIGINAL
int num = 0;
int hume = 0;
system("cls");
string ranks[10]={"RAP GOD", "SHAKESPEARE", "NOBEL LAUREATE",
"NEW YORK TIMES BESTSELLER",
"POPULAR NOVELIST", "HIGH SCHOOL ENGLISH TEACHER",
"CROSSWORD KING", "LOCAL BLOGGER",
"MEDIOCRE POET", "UNCULTURED FOOL"};
rankheader r;
if(quest==1)
{
num = rand() % outcount + 1;
string snum = ""; // string which will contain the result
ostringstream convert; // stream used for the conversion
convert << num; // random num
snum = convert.str();
infile.open("bank.txt", ios::in);
if (infile.is_open())
{
while (!infile.eof())
{
getline(infile, stt, '#');
getline(infile, fileword);
if (stt.substr(0,100)==snum.substr(0,100))
{
origword = fileword; // origword is the word that will be used in the hangman game.
}// what this has done is select a random word from the text file, and using it in the hangman game.
}
}
else
cout << "File could not be opened. " << endl;
int jacko = origword.length();
displayword.resize(jacko, '-');
hume = origword.length();
system("cls");
}
else
{
int jacko = origword.length();
displayword.resize(jacko, '-');
hume = origword.length();
system("cls");
}
cout << "Guess this word: " << displayword << endl;
while (gameover == 'N')
{
cout << "Enter an uppercase letter: ";
cin >> letter;
mark = 0;
dashreplaced = 'N';
if (letter.length() != 1)
{
cout << "Make sure to enter ONE uppercase letter." << endl;
left = 10 - numincorrect;
cout << "You have " << left << " guesses remaining. " << endl;
}
else
{
for (int y = 0; y < 26; y += 1)
{
if (letter == list.substr(y, 1))
{
mark = 1;
}
}
list.replace(count, 1, letter);
count += 1;
for (int x = 0; x < hume; x += 1)
{
if (origword.substr(x, 1) == letter)
{
displayword.replace(x, 1, letter);
dashreplaced = 'Y';
}
}
if (mark == 0)
{
if (dashreplaced == 'Y')
{
if (displayword.find("-", 0) == -1)
{
gameover = 'Y';
cout << endl << "Yes, the word is " << origword << endl;
cout << "Good work! " << endl;
r.setrank(ranks, left);
// I suspect the problem is here, when I pass it to the function.
}
else
{
cout << endl << "Guess this word: " << displayword << endl;
dashreplaced = 'N';
}
}
else
{
numincorrect += 1;
left = 10 - numincorrect;
cout << "You have " << left << " guesses remaining. " << endl;
if (numincorrect == 10)
{
gameover = 'Y';
cout << endl << "Sorry, the word is " << origword << endl;
cout<<"Your rank is: BARELY LITERATE "<<endl;
}
}
}
else if (mark == 1)
{
left = 10 - numincorrect;
cout << "You've used this letter before. " << endl;
cout << "You have " << left << " guesses remaining. " << endl;
}
}
}
}
| |