Need Help With Hangman Program
Nov 4, 2011 at 10:06pm UTC
I'm making a program to help construct 4-8 letter words using 12 letters given by user input. I'm still new to c++ and I'm having trouble but i don't know whats wrong.Below is my current code and i appreciate any suggestions.FYI HangManWord.txt is a word list with 58,000 words.
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
vector<string>PossibleWords;
vector<string>Words;
vector<char >letters;
void initialize();
string MakeWord(){
for (unsigned int i=0;i<(PossibleWords.size());i++)
{
string tmp=PossibleWords.at(i);
unsigned int test=0;
for (unsigned int j=0;j<letters.size();j++)
{
char tmpc=letters.at(j);
unsigned int place=tmp.find(tmpc);
if (place!=string::npos)
{
test=test+1;
}
}
if (((tmp.length())==test))
{
Words.push_back(tmp);
}
}
string output;
for (unsigned int i=0;i<Words.size();i++)
{
output=output+(Words.at(i))+", " ;
}
return output;
}
int main()
{
initialize();
string input;
cout<<"Enter your Letters: " ;
getline(cin,input);
for (int i=0;i<12;i++)
{
char tmpstr=input.at(i);
letters.push_back(tmpstr);
}
string answer=MakeWord();
cout<<answer;
return 0;
}
void initialize(){ //subroutine that puts word list into vector called "PossibleWords" status: Works
string line;
ifstream myfile ("HangManWord.txt" );
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
PossibleWords.push_back(line);
cout << line << endl;
}
myfile.close();
}
}
Nov 4, 2011 at 10:15pm UTC
i have recently made a hangman program myself that read 200+ words from a .txt file. here's my code
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
// 230 word hangman.cpp : Defines the entry point for the console application.
// impossible hangman
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <fstream>
#include <vector>
#include <time.h>
using namespace std;
void PrintHeadOfMan(){cout<<" O\n" ;}
void PrintUpperBodyOfMan(){cout<<"(|)\n" ;}
void PrintLegsOfMan(){cout<<" ^" ;}
void PrintDeadMan(){cout<<"v_|o\n" ;}
int guess=16;
int hints=3;
int guessTemp=16;
string Inputchar;
//our words
vector <string> words;
//our display characters
string displayCharacters;
//timer
void tick(){Sleep(1000);}
//our current word
string currentWord;
//here's where we load the words
void LoadInWords()
{
ifstream myfile ("hangman.txt" );
if (myfile.is_open())
{
string temp = "" ;
for (int i =0; myfile.good(); i++)
{
getline (myfile,temp);
words.push_back(temp);
}
myfile.close();
}
else
{ cout << "ERROR" << endl;}
}
string RandomWord()
{
return words[rand()%words.size()];
}
//print lives to screen
void PrintLives()
{
guessTemp=guess/2;
while (guessTemp>0)
{cout<<"M" ;guessTemp--;}
cout<<"\n" ;
guessTemp=guess/2;
while (guessTemp > 0)
{cout <<"V" ; guessTemp--;}
/*if(guessTemp==0)
if (guessTemp>0)
{system("cls");guessTemp=guess;}
}*/
}
void PrintLetters()
{
cout << endl << displayCharacters;
}
void Print()
{
//clear the screen first
system("cls" );
PrintLives();
if (guess<=3){cout<<" " ; PrintHeadOfMan();}
if (guess<=2){cout<<" " ; PrintUpperBodyOfMan();}
if (guess<=1){cout<<" " ; PrintLegsOfMan();}
if (guess<=0)
{system("cls" );cout<<"Ulost! " ; PrintDeadMan();system("pause" ); exit(0);}
PrintLetters();
}
bool IsLetterInWord(char letter)
{
bool inWord = false ;
for (int i=0; i<currentWord.length(); i++)
{
if (Inputchar[0] == currentWord[i])
{
inWord = true ;
break ;
}
}
return inWord;
}
//are we done with the current word?
bool done;
void GetNextMove();
bool IsLetterInWord(char letter);
void GetNextMove()
{
cout <<endl << "Enter next letter:" << endl;
//use cin to get the next letter
cin>>Inputchar;
//is this letter in our string??
if (IsLetterInWord(Inputchar[0]))
{
//where is it in the word?
for (int i=0; i<currentWord.length(); i++)
{
if (currentWord[i] == Inputchar[0])
{
displayCharacters[i] = Inputchar[0];
}
}
//winner!!
if (displayCharacters == currentWord)
{
done = true ;
}
}
//if it's not, lose a life.
else
{
guess--;
}
}
//number of words the player got right
int numWordsCorrect = 0;
int _tmain(int argc, _TCHAR* argv[])
{
srand(time(NULL));
LoadInWords();
cout << "HANGMAN!" << endl << endl << "Two lives for every heart!" << endl << "Try to complete all the words!" ;
//setup the word and the displayCharacters
currentWord = RandomWord();
displayCharacters = currentWord;
//initialize our display characters
for (int i=0; i< currentWord.length(); i++)
{
displayCharacters[i] = '_' ;
}
while (numWordsCorrect < 230)
{
GetNextMove();
Print();
if (done)
{
currentWord = RandomWord();
displayCharacters = currentWord;
//initialize our display characters
for (int i=0; i< currentWord.length(); i++)
{
displayCharacters[i] = '_' ;
}
done = false ;
numWordsCorrect ++;
Print();
}
}
return 0;
}
Nov 4, 2011 at 10:23pm UTC
Thanks for your help!
Topic archived. No new replies allowed.