I know this is off topic, but if you #include <windows.h> you can use Sleep(200); where the number inside is the time in milliseconds. That way you can use a code like this to make your game seem better
I see where you are going wrong. You are overpopulating your correct and incorrect strings. That is because you are adding the incorrect letter for every letter in the correct word during the for loop. Take that out and place is outside the for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
bool found = false;
for (k=0; k<word.length();k++) //Compare you guess to the word
{
if (guess == word[k])
{
cout << "Good Guess!" << endl;
letters[k] = guess;
found = true;
count++;
}
}
if(found)
correct += guess;
else
incorrect += guess;
The other problem is that the player can't win until the loop finishes i.e. the 15 guesses. Here is what you do. Don't reset count instead have a boolean called found which you set to false at the beginning of the for loop. If the player finds a correct letter, it is set to true skipping the wrong letter section. Keep incrementing count for each correct letter. If count is equal to word length then the player has won the game. Something like this:
// Hangman.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cmath>
#include <conio.h>
#include <time.h>
usingnamespace std;
//Ramon Herrera
//Hangman Game
string LoadFile() //To load the file
{
string W; //Declare Varibles
string words[20];
int count = 0;
ifstream txtfile; //the in file
txtfile.open("words.txt"); //opens file
for (count=0; !txtfile.eof(); count++)
{
getline(txtfile,W);
words[count] = W;
}
int k=(rand()%count); //Picks a random word from file.
txtfile.close(); //closes file
return words[k];
}
void RunGame(string word) //Runs the game
{
int k;
int wrong = 0;
char guess;
string letters;
char x;
string incorrect;
string correct;
for (k=0; k<word.length();k++) //Blanks for each letter of your word
{
letters+= '_';
}
int count = 0;
for (x=0; x<15; x++) //15 Guesses
{
cout << "Make a Guess" << endl;
cin >> guess;
bool found = false;
for (k=0; k<word.length();k++) //Compare you guess to the word
{
if (guess == word[k])
{
cout << "Good Guess!" << endl;
letters[k] = guess;
found = true;
count++;
}
}
if(found)
correct += guess;
else
incorrect += guess;
if(count == word.length())
{
cout << "YOU HAVE WON!" << endl;
break;
}
if (!found) //You lose when you get 6 wrong letters
{
cout << "Wrong Letter!" << endl;
wrong++;
if (wrong==6)
{
cout << "You are Dead! GameOver!" << endl;
cout << "Your word was " << word << endl;
cout << endl;
break;
}
}
cout << "Word:" << letters << endl;
cout << "Correct:" << correct << endl;
cout << "Incorrect:" << incorrect << endl;
cout << endl;
cout << endl;
}
}
int _tmain(int argc, _TCHAR* argv[]) //main
{
cout << "Let's Play HangMan!" << endl;
srand(time(NULL));
char playagain = 'Y';
while (playagain == 'Y')
{
string word = LoadFile();
RunGame(word);
cout << endl;
cout << "Play Again? (Y/N)" << endl;
cin >> playagain;
playagain = toupper(playagain);
}
cout << "Thank You for Playing" << endl;
_getch();
return 0;
}
Okay, now two things I noticed. When I'm choosing a random word from the file, it just gives me blank spaces...so when I type in any random letter, it says I win. Why is that? (You'll notice if you do "cout << word << endl; just at the begin of the loop to make sure the loadfile() is working.)
Another thing, how do I make it so the when the user type a letter, Upper or Lower Case, it will match the letter of the word you a trying to guess, Upper or Lower case?
EX: Word is Maine. If they type "m", they guess right. If they type "A", they guess right