Nullware
Guru
I made this to see what I was capable of in a little bit of spare time and I thought it turned out all right so I've decided to share it in hopes that it might help some people. (It's commented almost throughout)
I also wouldn't mind if any of the better programmers want to point out flaws in my code or my style of programming. Points to improve on and criticism would actually be appreciated.
I use Dev-CPP which uses the MinGW compiler so you might have to change the return statement at the end or make other minor changes to get it to work.
I also wouldn't mind if any of the better programmers want to point out flaws in my code or my style of programming. Points to improve on and criticism would actually be appreciated.
I use Dev-CPP which uses the MinGW compiler so you might have to change the return statement at the end or make other minor changes to get it to work.
Code:
#include <cstdlib> // Standard C++ library
#include <iostream> // Basic input/output library
#include <conio.h> // Required for getch()
using namespace std;
int toupper(int c);
// Match the user's guess up against the puzzle word and update any characters guessed right
int updateWord(string& tempWord, string realWord, string guessedChar) {
string previousWord = tempWord; // Hold the previous value of the puzzle with unknowns
for(int i=0;i<=realWord.length()-1;i++) // Loop through the length of the puzzle string
{
if (realWord.substr(i,1)==guessedChar) { // If the current character being looped through matches the user's guess
tempWord = tempWord.substr(0,i)+ guessedChar + tempWord.substr(i+1,tempWord.length()); // Replace that character in the progress string
}
}
if(previousWord == tempWord) { // If the puzzle hasn't changed
cout << "Sorry, there are none of that letter in the puzzle.\n";
}
else{ // The puzzle was updated due to some letters being found
cout << "Some letters were found and now your puzzle has been updated.\n";
}
}
int main(int argc, char *argv[])
{
string dictionary[6] = { "hockey", "revolution", "extortion", "football", "alias", "detention" }; // Initialize the dictionary of words to use
string guess, playagain; // Strings to hold the user input
cout << "------------------ Welcome to Hangman! ------------------\n";
do
{
srand(time(0)); // Initialize our reference to the time variable for randomizations
int wordNumber = rand() % 6; // Generate a random number to choose a word from the dictionary
string word = dictionary[wordNumber]; // Save the value of the word we're using in an independant variable
string tempWord (word.length(),'_'); // Setup a variable with underscores to represent letters not known of the length of the chosen word
while(tempWord != word) // While the word hasn't been guessed loop through
{
cout << "\nThe puzzle is " << tempWord << " (" << tempWord.length() << " characters), guess a letter.\n"; // Ask the user to guess a letter and output his current status in guessing the word
guess = getch(); // Get the user's guess by keyboard input
updateWord(tempWord, word, guess); // Check the user's guess up against the puzzle word characters
}
cout << "You win! The word was: " << word << "\n"; // Output this message if the user wins
do {
cout << "\nWould you like to play again? (Y/N).\n";
playagain = getch();
}while(playagain!="y" && playagain !="n" && playagain !="Y" && playagain !="N"); // Keep asking if the user wants to play again until he enters yes (y) or no (n)
}while(playagain=="y" || playagain=="Y"); // Loop through the game again if he presses y (yes)
system("PAUSE");
return EXIT_SUCCESS;
}


















