#include <iostream>
#include <string.h>
#include <conio.h>
#include <fstream>
#include <vector>
#include <windows.h>
#include "resource.h"
#pragma resource "hangman.res"
#define MAX 13
using namespace std;
void LoadFileInResource(int name, int type, int& size, char*& data)
{
HMODULE handle = GetModuleHandle(NULL);
HRSRC rc = FindResource(handle, MAKEINTRESOURCE(name),
MAKEINTRESOURCE(type));
HGLOBAL rcData = LoadResource(handle, rc);
size = ::SizeofResource(handle, rc);
data = (char*)(LockResource(rcData));
}
int lineCount(vector<char*> &fileLines) {
int number_of_lines(0), size(0);
char* lines, *buff, *data(NULL);
int language;
char lang = ' ';
cout << "Select Language\n 1. Romanian\n 2. English\n a. Four letter words\n b. Five letter words\n";
while(lang != '1' && lang != '2' && lang != 'a' && lang != 'b') {
cin >> lang;
switch(lang) {
case '1':
language = ROMANIAN;
break;
case '2':
language = ENGLISH;
break;
case 'a':
language = FOUR;
break;
case 'b':
language = FIVE;
break;
default:
cout << "1 or 2: ";
}
}
LoadFileInResource(language, TEXTFILE, size, data);
buff = new char[size];
cout << buff;
strncpy(buff, data, size);
lines = strtok(buff, "\r\n");
while (lines != NULL) {
++number_of_lines;
fileLines.push_back(lines);
lines = strtok (NULL, "\n");
}
delete[] buff;
return number_of_lines;
}
int main()
{
int count(0), count2(0), tries(7), use(0), pos;
char word[MAX], lines[MAX], used[2*MAX], guess;
char menuChar = ' ', again = ' ';
vector<char*> fileLines;
string menu;
srand(time(0));
bool restart = true;
while(restart) {
restart = false;
//initialize arrays slots as NULL
for(int i=0; i < MAX; i++){
lines = NULL;
word = NULL;
}
for(int i=0; i < 2*MAX; i++)
used = NULL;
//menu
cout << "Press '1' to play alone\nPress '2' to play with a friend\n";
cout << "1 or 2: ";
while(menuChar != '1' && menuChar != '2'){
cin >> menu;
menuChar = menu[0];
switch(menuChar) {
case '1':
pos = rand() % lineCount(fileLines);
memcpy(word, fileLines[pos], strlen(fileLines[pos])-1);
clrscr();
break;
case '2':
cout << "Enter the word to be guessed: ";
cin >> word;
clrscr();
break;
default:
cout << "1 or 2: ";
}
}
//count the number of letters in the word for lines
for(unsigned int i=0; i < MAX; i++) {
if(word != NULL) {
lines = '_';
count2++;
}
}
gallows(tries);
cout << endl << endl << endl;
for(int i=0; i < MAX; i++)
cout << lines << " ";
cout << endl << endl << used << endl;
while (tries > 0) {
cin >> guess;
used[use] = guess;
for(int j=0; j < count2; j++) {
if(guess == word[j])
lines[j] = guess;
if (lines[j] == word[j])
count++;
}
if(!memchr(word, guess, count2) && !memchr(used, guess, use)){
tries--;
}
if(count == count2){
cout << endl << lines << endl;
cout << "\nGood Job!" << endl;
goto END;
}
count = 0;
use++;
clrscr();
gallows(tries);
cout << endl << endl << endl;
for(int i=0; i < MAX; i++)
cout << lines << " ";
cout << endl << endl << used << endl;
}
cout << "\nThe word was: " << word << endl;
END:
cout << "Would you like to play again?(y/n): ";
while(again != 'y' && again != 'n'){
cin >> again;
}
if(again == 'y') { // reset counter and error checking values and clear vector
count = 0;
count2 = 0;
tries = 7;
use = 0;
again = ' ';
menuChar = ' ';
fileLines.clear();
clrscr();
restart = true;
}
}
return 0;
}