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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <cctype>
#include "randword.h"
#include "myfuncts.h"
using namespace std;
void bubbleSort(char usedLettters[], int used);
int binarySearch(char usedLetters[], int used, char letterToFind);
inline string getPanel(int panel);
const string Panels[] =
{
" _____ \n | | \n | \n | \n | \n | \n __|__ \n",
" _____ \n | | \n O | \n | \n | \n | \n __|__ \n",
" _____ \n | | \n O | \n | | \n | \n | \n __|__ \n",
" _____ \n | | \n O | \n -| | \n | \n | \n __|__ \n",
" _____ \n | | \n O | \n -|- | \n | \n | \n __|__ \n",
" _____ \n | | \n O | \n -|- | \n / | \n | \n __|__ \n",
" _____ \n | | \n O | \n -|- | \n / \\ | \n | \n __|__ \n"
};
using namespace std;
int main()
{
string response;
string wordFromFile;
char letterToGuess;
unsigned int locationFound;
int plays = 0;
int resp2;
int numberOfTries=0;
unsigned int used = 0;
char usedLetters[26];
getWords("Hangman.dat");
wordFromFile = getNextWord();
do
{
int wrongGuesses=0;
used = 0;
if (wordFromFile == "")
{
cout << "Out of words. Goodbye!" << endl;
system ("pause");
return 0;
}
do
{
cout << "\nDo you want to play Hangman?" << endl;
cin >> response;
resp2 = PromptYN(response);
if (resp2 == ERROR)
{
cout << "Invalid Response" << endl;
numberOfTries++;
if (numberOfTries == 3)
resp2=STOP;
}
if (resp2 == STOP)
{
cout << "Goodbye!" << endl;
system ("pause");
return 0;
}
} while (resp2 != PLAY);
cout << "Let's play!" << endl;
do
{
cout << "Word to guess: ";
for(unsigned int i = 0; i < wordFromFile.length(); i++)
{
wordFromFile[i] = toupper(wordFromFile[i]);
if(-1 == binarySearch(usedLetters, used, wordFromFile[i]))
cout << "_ ";
else
cout << wordFromFile[i] << " ";
}
cout << endl << Panels[wrongGuesses];
cout << endl << "Letters Guessed: " << " ";
for (unsigned int i = 0; i < used; ++i)
cout << usedLetters[i] << " ";
cout << endl << "Enter a letter to guess: ";
cin >> letterToGuess;
letterToGuess = toupper(letterToGuess);
cout << endl << "You entered: " << letterToGuess << endl;
if(-1 == binarySearch(usedLetters, used, letterToGuess))
{
usedLetters[used++] = letterToGuess;
bubbleSort(usedLetters, used);
locationFound = wordFromFile.length();
for(unsigned int i = 0; i < wordFromFile.length(); ++i)
if(letterToGuess == wordFromFile[i])
{
locationFound = i;
break;
}
if(locationFound < wordFromFile.length())
cout << letterToGuess << " is in the word to guess." << endl;
else
{
wrongGuesses++;
cout << letterToGuess << " is NOT in the word to guess." << endl;
}
}
else
cout << "Letter already guessed." << endl;
cout << getPanel(wrongGuesses);
} while (wrongGuesses < 6 /* && !wordGuessed */);
if (wrongGuesses == 6)
cout << "You Lose! The Word was: " << wordFromFile << endl;
else
cout << "You Win!";
wordFromFile = getNextWord();
} while (wordFromFile != "");
}
inline string getPanel(int panel)
{
return Panels[panel];
}
int binarySearch(char usedLetters[], int used, char letterToFind)
{
int low = 0;
int high = used -1;
int mid;
while (low <= high)
{
mid = (low + high) /2;
if(letterToFind == usedLetters[mid])
return mid;
else if (letterToFind > usedLetters[mid])
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
void bubbleSort(char usedLetters[], int used)
{
for(int x = 0; x < used; x++)
{
for(int y = 0; y < used - 1; y++)
{
if(usedLetters[y] > usedLetters[y + 1])
{
char temp = usedLetters[y+1];
usedLetters[y + 1] = usedLetters[y];
usedLetters[y] = temp;
}
}
}
}
|