Created
May 18, 2012 01:47
-
-
Save leahloughran/2722651 to your computer and use it in GitHub Desktop.
Simple hangman win32 console application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Hangman.cpp : Defines the entry point for the console application. | |
// | |
//Leah Loughran, 2012 | |
//Simple Hangman console game | |
#include "stdafx.h" | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <ctype.h> | |
#include <algorithm> | |
#include <ctime> | |
using namespace std; | |
void printHangman(int chancesLeft) | |
{ | |
switch (chancesLeft) | |
{ | |
case 5: | |
cout << " ____ " << endl; | |
cout << " | | " << endl; | |
cout << " | " << endl; | |
cout << " | " << endl; | |
cout << " | " << endl; | |
cout << "_|___ " << endl; | |
cout << endl; | |
break; | |
case 4: | |
cout << " ____ " << endl; | |
cout << " | | " << endl; | |
cout << " | O " << endl; | |
cout << " | " << endl; | |
cout << " | " << endl; | |
cout << "_|___ " << endl; | |
cout << endl; | |
break; | |
case 3: | |
cout << " ____ " << endl; | |
cout << " | | " << endl; | |
cout << " | _O_ " << endl; | |
cout << " | " << endl; | |
cout << " | " << endl; | |
cout << "_|___ " << endl; | |
cout << endl; | |
break; | |
case 2: | |
cout << " ____ " << endl; | |
cout << " | | " << endl; | |
cout << " | _O_ " << endl; | |
cout << " | | " << endl; | |
cout << " | " << endl; | |
cout << "_|___ " << endl; | |
cout << endl; | |
break; | |
case 1: | |
cout << " ____ " << endl; | |
cout << " | | " << endl; | |
cout << " | _O_ " << endl; | |
cout << " | | " << endl; | |
cout << " | / " << endl; | |
cout << "_|___ " << endl; | |
cout << endl; | |
break; | |
case 0: | |
cout << " ____ " << endl; | |
cout << " | | " << endl; | |
cout << " | _O_ " << endl; | |
cout << " | | " << endl; | |
cout << " | / \\ " << endl; | |
cout << "_|___ " << endl; | |
cout << endl; | |
break; | |
} | |
//depending on the number of chances left (1-5), print out a gallows with a guy hanging | |
} | |
void printWord(string word, vector<char> revealedLetters) | |
{ | |
//go through each character in the word; if that character is found in "revealedLetters", then print the letter, else print blank | |
for (int i = 0; i < word.length(); i++) | |
{ | |
if (count(revealedLetters.begin(), revealedLetters.end(), word[i]) > 0) //the character is found in "revealedLetters" | |
{ | |
cout << char(toupper(word[i])) << " "; //print the uppercase letter | |
} | |
else //the character is not found | |
{ | |
cout << "__ "; //print a blank | |
} | |
} | |
cout << endl; | |
} | |
bool letterExists(string word, char letter) | |
{ | |
//check if the letter exists in the word | |
bool exists = false; | |
for (int i = 0; i < word.length(); i++) //go through each character in the word | |
{ | |
if (word[i] == letter) //check if that character matches the letter we're looking for | |
{ | |
exists = true; //set it to true | |
} | |
} | |
return exists; | |
} | |
int numOfUniqueLetters(string word) | |
{ | |
//calculates the number of unique letters in a word | |
vector<char> uniqueLetters; | |
for (int i = 0; i < word.length(); i++) | |
{ | |
if (!(count(uniqueLetters.begin(), uniqueLetters.end(), word[i]) > 0)) //if it's not already in there | |
{ | |
uniqueLetters.push_back(word[i]); | |
} | |
} | |
return (int)uniqueLetters.size(); | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
cout << "Welcome to Hangman!" << endl; | |
vector<string> dictionary; //putting random words in the dictionary... | |
//not very pretty but basic enough for beginners to understand | |
//note: this program does not works with multi-word phrases | |
dictionary.push_back("biscuit"); | |
dictionary.push_back("alarming"); | |
dictionary.push_back("microscopic"); | |
dictionary.push_back("memorable"); | |
dictionary.push_back("yellow"); | |
dictionary.push_back("jacket"); | |
dictionary.push_back("bumblebee"); | |
dictionary.push_back("purse"); | |
dictionary.push_back("pencil"); | |
dictionary.push_back("genes"); | |
dictionary.push_back("programming"); | |
dictionary.push_back("technology"); | |
dictionary.push_back("keyboard"); | |
dictionary.push_back("tabby"); | |
dictionary.push_back("cupboard"); | |
vector<char> guessedLetters; | |
vector<char> successfulLetters; | |
string playAgain = "y"; | |
while (playAgain == "y") | |
{ | |
srand(time(NULL)); | |
int randomSpot = (rand()%(dictionary.size())); //gets a random spot in the dictionary | |
string randomWord = dictionary[randomSpot]; //gets a random word from the dictionary | |
int chances = 5; //starts the game with 5 chances to guess | |
while ((chances > 0)&&(successfulLetters.size() != numOfUniqueLetters(randomWord))) //continue getting guesses until the player runs out of chances | |
{ | |
cout << "\n\n" << endl; | |
printHangman(chances); //print the gallows | |
printWord(randomWord, successfulLetters); //print the word in its currently revealed state | |
char guess = '1'; //start guess out as the number 1 | |
while (!isalpha(guess)) //guess is not a letter | |
{ | |
cout << "Please enter a letter to guess! You have " << chances << " chance(s) left." << endl; | |
cin >> guess; | |
guess = tolower(guess); //make sure the guess is lowercase | |
if (!isalpha(guess)) //if it's still not a letter, prompt the user to guess again | |
{ | |
cout << "Your guess is not a letter; guess again" << endl; | |
} | |
else if (count(guessedLetters.begin(), guessedLetters.end(), guess) > 0) //if the letter has already been guessed | |
{ | |
cout << "You have already guessed that; guess again" << endl; | |
} | |
else | |
{ | |
guessedLetters.push_back(guess); //the guess is valid; add the guess to the list of guessed letters | |
if (letterExists(randomWord, guess)) //if the letter is found in the word | |
{ | |
cout << "Great guess! That letter is found in the word." << endl; | |
successfulLetters.push_back(guess); //add the letter to the list of successful guesses | |
} | |
else //the letter does not exist in the word | |
{ | |
cout << "Sorry, your guess was not found in the word!" << endl; | |
chances--; //they lost one chance to guess | |
} | |
} | |
} | |
} | |
/*if they're out of the loop, that either means: | |
1) chances == 0 | |
-THEY LOST | |
2) successfulLetters.size() == numOfUniqueLetters(randomWord) | |
-THEY WON | |
*/ | |
if (chances == 0) //player lost | |
{ | |
printHangman(0); | |
cout << "Sorry, you lost Hangman! The word was \"" << randomWord << "\"." << endl; | |
} | |
else if (successfulLetters.size() == numOfUniqueLetters(randomWord)) | |
{ | |
cout << "Congratulations! You won Hangman by correctly guessing the word \"" << randomWord << "\"." << endl; | |
} | |
playAgain = "foo"; //reset playAgain | |
while (!((playAgain == "y")||(playAgain == "n"))) | |
{ | |
cout << "Would you like to play again? Enter y for yes or n for no." << endl; | |
cin >> playAgain; | |
if (!((playAgain == "y")||(playAgain == "n"))) //invalid input | |
{ | |
cout << "Invalid input; try again" << endl; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment