Hangman

This is a bit more complicated than Guess the Number and allows us to play around with strings.

Lets set up the basics and making a list of words that can be picked for guessable words.

#include <iostream>		// Console output/input
#include <ctime>			// For the time function
#include <cstdlib>			// For the random number generater
#include <string>			// For the string class

using namespace std;

const int wordNo = 7;		// Define the number of words in the list
// Define the word list
string wordList[wordNo] = {"hello", "computer", "programming", "tutorial",
"codeblocks", "hangman", "gamelistonline"};

string word;		// The chosen word will be stored here
string guessed;		// The word made up of guesses

int main(){
	srand(time(0));
	word = wordList[(rand()%wordNo)];
	guessed = string(word.length(), '-');

	return 0;
}

There we have the basics of the program. We create an array of strings that can be picked (feel free to change the list) - just make sure you update wordNo to accurately reflect the number of words in the list.

Then two strings are created, one to store the complete word, the other to store at first nothing but a number of '-'s. Then as the user guesses correct letters the '-'s will be replaced by the letters of the word.

Then we seed the random number generater with the time (more on this in the "Guess the Number" tutorial). We set word to a random element from wordList, and we set guessed to be a string, the same length as word filled with '-'s. Now to get the user's guesses.

#include <iostream>		// Console output/input
#include <ctime>			// For the time function
#include <cstdlib>			// For the random number generater
#include <string>			// For the string class

using namespace std;

const int wordNo = 7;		// Define the number of words in the list
// Define the word list
string wordList[wordNo] = {"hello", "computer", "programming", "tutorial",
"codeblocks", "hangman", "gamelistonline"};

string word;		// The chosen word will be stored here
string guessed;		// The word made up of guesses

const int totalFails = 10;	// The total number of failed guesses
int fails = 0;		// The current number of failed guesses

int main(){
	srand(time(0));
	word = wordList[(rand()%wordNo)];
	guessed = string(word.length(), '-');

	std::cout << "Hangman\nPlease enter a letter you think is in the word\n";

	while(fails != totalFails){
		// Print the number of tries and the current word
		std::cout << "Fails: " << fails << "/" << totalFails << std::endl;
		std::cout << "Word: " << guessed << std::endl;
		std::cout << "->";
		// Get the users guess
		char guess;
		std::cin >> guess;

		fails++;
	}

	return 0;
}

Now if we run the program it will gladly take our guess ten times and then finish, which is a bit pointless. We need to check if the guess is in the word, incrememnt the fails only if it isn't, and then see if the whole word has been guessed. Add these two functions (you can either add them at the start of the code or put prototypes at the start and the declaration at the end).

bool CheckLetter(char guess){
	bool returnVal = false;
	for(int i=0; i<word.length(); i++){
		if(word[i] == guess){
			returnVal = true;
			guessed[i] = guess;
		}
	}
	return returnVal;
}

bool CheckWord(){
	for(int i=0; i<guessed.length(); i++){
		if(guessed[i] == '-')
			return false;
	}
	return true;
}

The first function takes a char as argument and uses a for loop to check if any of the characters in word are equal to guess, if it is equal then it changes the corresponding character in guessed to guess. It also returns a false if the guess is not in word.

The second function loops through word and returns a false if word contains a '-', this will tell us if all of the letters have been guessed or not.

Now we just need a few more lines of code, at the end of the while loop, add this:

		if(!CheckLetter(guess))
			fails++;
		if(CheckWord())
			break;

And to finish off, just after the while loop add:

	if(fails == totalFails)
		std::cout << "Sorry, you failed" << std::endl;
	else
		std::cout << "Congratulations, you won!" << std::endl;

And that should be finished, if you want to look at the complete copy of the source code click here.

If you would like to get in contact, please email me at:
conn_peter@hotmail.com