/* Program 1 Programmer: Dan Bennett Date : Jan 2013 Play a game of hangman. This program will play a game of hangman, the computer will select words from an input file and allow the user to guess letters in the word. The user gets 5 incorrect letters before they lose that round of the game. This program uses a trick of constructing a "guessed" word, which is all blanks in the beginning, but gets filled in as the game goes by. Thus _ can not be part of any word selected. It works somewhat like "wheel of fortune" in that it allows for phrases not just words. */ #include #include #include using namespace std; // note, I only used function prototypes for high level functions. // The others are declared before they are used. // // Your choice on this. We will find a way to do this better (or different) // later in the semester. void FixAWord(string & ); string GetAWord(ifstream & wordFile); // input file name const string WORD_FILE_NAME = "phrases.txt" ; // constants associated with drawing the suspect on the gallows. const char LEFT_ARM = '\\'; const char HEAD = '0'; const char RIGHT_ARM = '/'; const char BODY = '|'; const char LEFT_LEG = '/'; const char RIGHT_LEG = '\\'; const int LEFT_ARM_COUNT = 1; const int HEAD_COUNT = 2; const int RIGHT_ARM_COUNT = 3; const int BODY_COUNT = 4; const int LEFT_LEG_COUNT = 5; const int RIGHT_LEG_COUNT = 6; int main() { ifstream wordFile;// input file bool done = false;// true if finsihed playing all gams int wins = 0, // number of games won games = 0; // number of games played string word; // the word the user will guess char answer; // the answer to the "do you want to play again question" int i; // lcv for various routines int turn; // turn in a current round char letter; // the letter the player guessed string badLetters,// bad guesses solvedWord;// good guesses! wordFile.open(WORD_FILE_NAME.c_str()); cout << "Hello, welcome to hangman" << endl; cout << endl; while(not done) { games ++; // read the word /* Get the next word from the file. Remember the assumption that the file will have at least one word in it. The routine will start at the top when the end of file is hit. The routine will read all of the data on a line. Also replace any _ in the string with a space */ getline(wordFile, word); // if we are at the end of the file if (not wordFile) { // reset wordFile.close(); wordFile.open(WORD_FILE_NAME.c_str()); // read the word. getline(wordFile, word); } /* "fix" a word. This involves: changeing the case of all letters to lowercase rmoving all _, and replacing them with a space. */ for(i=0;i= LEFT_ARM_COUNT) { cout << LEFT_ARM; } else { cout << " "; } if (badLetters.size() >= HEAD_COUNT) { cout << HEAD; } else { cout << " "; } if (badLetters.size() >= RIGHT_ARM_COUNT) { cout << RIGHT_ARM; } else { cout << " "; } cout << endl; // main body cout << " | "; if (badLetters.size() >= BODY_COUNT) { cout << BODY; } else { cout << " "; } cout << endl; // legs cout << " | "; if (badLetters.size() >= LEFT_LEG_COUNT) { cout << LEFT_LEG; } else { cout << " "; } cout << " "; if (badLetters.size() >= RIGHT_LEG_COUNT) { cout << RIGHT_LEG; } else { cout << " "; } cout << endl; // air to dangle in cout << " |" << endl; // platorm cout << "_+_______ "; // the word for (i=0;i "; cin >> letter; } while (not isalpha(letter)); letter = tolower(letter); /* decide if the guessed letter is in the word If so it will add that letter to the correct guess if not it will add the letter to the bad letters */ if (word.find(letter) != string::npos) { i = word.find(letter); while (i != string::npos) { solvedWord[i] = word[i]; i = word.find(letter,i+1); } } else { badLetters += letter; } } // inform the player of a win or loss. if(badLetters.size() >= RIGHT_LEG_COUNT) { // Nasty routine to draw the gallows and the poor fellow dangling. // // This uses the fact that the number of incorrect letters determines // how far the suspect has been hung. // top of gallows cout << " +--+" << endl; cout << " | |" << endl; // arms and head if needed cout << " | "; if (badLetters.size() >= LEFT_ARM_COUNT) { cout << LEFT_ARM; } else { cout << " "; } if (badLetters.size() >= HEAD_COUNT) { cout << HEAD; } else { cout << " "; } if (badLetters.size() >= RIGHT_ARM_COUNT) { cout << RIGHT_ARM; } else { cout << " "; } cout << endl; // main body cout << " | "; if (badLetters.size() >= BODY_COUNT) { cout << BODY; } else { cout << " "; } cout << endl; // legs cout << " | "; if (badLetters.size() >= LEFT_LEG_COUNT) { cout << LEFT_LEG; } else { cout << " "; } cout << " "; if (badLetters.size() >= RIGHT_LEG_COUNT) { cout << RIGHT_LEG; } else { cout << " "; } cout << endl; // air to dangle in cout << " |" << endl; // platorm cout << "_+_______ "; // the word for (i=0;i "; cin >> answer; answer = tolower(answer); } while (answer != 'y' and answer != 'n'); done = answer != 'y'; } // end of main loop wordFile.close(); // print the final statistics. cout << endl; cout << "Games Played: " << games << endl; cout << "Games Won " << wins << endl; cout << "Thanks for playing! " << endl; return 0; }