I've been working in C++ for awhile now, but always like for others to look over my code as much as I can get them to. My main concerns are if I've made something overly complicated or if I've broken any general conventions in my code. Anyways, thank you for reviewing and I hope you enjoy playing the game if you choose to.
Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <ctime>
#include <sstream>
#include <cctype>
#include <iterator>
#include <cstring>
void prompt(const int& rMaxWrongGuesses, int& rWrongGuesses, std::string& rGuessedLetters, std::string& rSoFar);
char getGuess(std::string& rGuessedLetters, const std::string& rTheWord, std::string& rSoFar, int& rWrongGuesses);
void checkGuess(char guess, const std::string& rTheWord, std::string& rSoFar, int& rWrongGuesses);
void shutDown(int& rWrongGuesses, const int& rMaxWrongGuesses, const std::string& rTheWord);
void drawGallows(int& rWrongGuesses);
int main()
{
//setup
const int kMaxWrongGuesses = 8; //maximum number of incorrect guesses allowed
// Gets list of words from file and assigns them to vector by line
std::vector<std::string> words; // collection of possible words to guess
std::ifstream myfile("words.txt");
std::copy(std::istream_iterator<std::string>(myfile),
std::istream_iterator<std::string>(),
std::back_inserter(words));
// Shuffles Words
srand(static_cast<unsigned int>(time(0)));
random_shuffle(words.begin(), words.end());
// Split Words and Definitions
std::stringstream ss(words[0]);
std::string item;
char delim = ',';
while (std::getline(ss, item, delim)) {
words.push_back(item);
}
// Find all characters after a comma in kTheWord and erase them.
std::string kTheWord = words[0];
kTheWord = kTheWord.substr(0, kTheWord.find(",", 0));
// Initialize Hint and strip underscores used in file
std::string hint = words.back();
std::replace(hint.begin(), hint.end(), '_', ' ');
// Set Variables used in game
int wrongGuesses = 0;
std::string soFar(kTheWord.size(), '-');
std::string GuessedLetters = "";
char guess;
// References for performance and ease of access
const int& rMaxWrongGuesses = kMaxWrongGuesses;
int& rWrongGuesses = wrongGuesses;
const std::string& rTheWord = kTheWord;
std::string& rGuessedLetters = GuessedLetters;
std::string& rSoFar = soFar;
std::cout << "Welcome to Hangman. Good luck!\n";
std::cout << "Here is your hint: " << hint << " \n";
while ((wrongGuesses < kMaxWrongGuesses) && (soFar != kTheWord))
{
prompt(rMaxWrongGuesses, rWrongGuesses, rGuessedLetters, rSoFar);
char guess = getGuess(rGuessedLetters, rTheWord, rSoFar, rWrongGuesses);
checkGuess(guess, rTheWord, rSoFar, rWrongGuesses);
drawGallows(rWrongGuesses);
}
shutDown(rWrongGuesses, rMaxWrongGuesses, rTheWord);
}
void prompt(const int& rMaxWrongGuesses, int& rWrongGuesses, std::string& rGuessedLetters, std::string& rSoFar)
{
std::cout << "\n\nYou have " << (rMaxWrongGuesses - rWrongGuesses);
std::cout << " incorrect guesses left.\n";
std::cout << "\nYou've used the following letters:\n" << rGuessedLetters << std::endl;
std::cout << "\nSo far, the word is:\n" << rSoFar << std::endl;
}
char getGuess(std::string& rGuessedLetters, const std::string& rTheWord, std::string& rSoFar, int& rWrongGuesses)
{
char guess;
std::cout << "\n\nEnter your guess: ";
std::cin >> guess;
guess = toupper(guess); // make uppercase since secret word is in uppercase
while (rGuessedLetters.find(guess) != std::string::npos)
{
std::cout << "\nYou've already guessed " << guess << std::endl;
std::cout << "Enter your guess: ";
std::cin >> guess;
guess = toupper(guess);
}
rGuessedLetters += guess;
return guess;
}
void checkGuess(char guess, const std::string& rTheWord, std::string& rSoFar, int& rWrongGuesses)
{
if (rTheWord.find(guess) != std::string::npos)
{
std::cout << "That's right! " << guess << " is in the word.\n";
//update soFar to include newly guessed letter
for (int i = 0; i < rTheWord.length(); ++i)
{
if (rTheWord[i] == guess)
{
rSoFar[i] = guess;
}
}
}
else
{
std::cout << "Sorry, " << guess << " isn't in the word.\n";
++rWrongGuesses;
}
}
void shutDown(int& rWrongGuesses, const int& rMaxWrongGuesses, const std::string& rTheWord)
{
if (rWrongGuesses == rMaxWrongGuesses)
{
std::cout << "\nYou've been hanged!";
}
else
{
std::cout << "\nYou guessed it!";
}
std::cout << "\nThe word was " << rTheWord << std::endl;
system("pause");
}
void drawGallows(int& rWrongGuesses)
{
if(rWrongGuesses==8)
{
std::cout<<std::endl<<std::endl
<<" +----+ "<<std::endl
<<" | | "<<std::endl
<<" | O "<<std::endl
<<" | /|\\ "<<std::endl
<<" | / \\ "<<std::endl
<<" | "<<std::endl
<<" ============"<<std::endl<<std::endl;
}
else if(rWrongGuesses==7)
{
std::cout<<std::endl<<std::endl
<<" +----+ "<<std::endl
<<" | | "<<std::endl
<<" | O "<<std::endl
<<" | /|\\ "<<std::endl
<<" | \\ "<<std::endl
<<" | "<<std::endl
<<" ============"<<std::endl<<std::endl;
}
else if(rWrongGuesses==6)
{
std::cout<<std::endl<<std::endl
<<" +----+ "<<std::endl
<<" | | "<<std::endl
<<" | O "<<std::endl
<<" | /|\\ "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" ============="<<std::endl<<std::endl;
}
else if(rWrongGuesses==5)
{
std::cout<<std::endl<<std::endl
<<" +----+ "<<std::endl
<<" | | "<<std::endl
<<" | O "<<std::endl
<<" | /| "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" ============="<<std::endl<<std::endl;
}
else if(rWrongGuesses==4)
{
std::cout<<std::endl<<std::endl
<<" +----+ "<<std::endl
<<" | | "<<std::endl
<<" | O "<<std::endl
<<" | | "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" ============="<<std::endl<<std::endl;
}
else if(rWrongGuesses==3)
{
std::cout<<std::endl<<std::endl
<<" +----+ "<<std::endl
<<" | | "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" ============="<<std::endl<<std::endl;
}
else if(rWrongGuesses==2)
{
std::cout<<std::endl<<std::endl
<<" +----+ "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" ============="<<std::endl<<std::endl;
}
else if(rWrongGuesses==1)
{
std::cout<<std::endl<<std::endl
<<" + "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" | "<<std::endl
<<" ============="<<std::endl<<std::endl;
}
else {};
}